cargo-lambda
cargo-lambda is a Cargo subcommand to help you work with AWS Lambda.
This subcommand compiles AWS Lambda functions natively and produces artifacts which you can then upload to AWS Lambda or use with other echosystem tools, like SAM Cli or the AWS CDK.
Installation
Install this subcommand on your host machine with Cargo itself:
cargo install cargo-lambda
Usage
Within a Rust project that includes a Cargo.toml
file, run the cargo lambda build
command to natively compile your Lambda functions in the project. The resulting artifacts such as binaries or zips, will be placed in the target/lambda
directory. This is an example of the output produced by this command:
❯ tree target/lambda
target/lambda
├── delete-product
│ └── bootstrap
├── dynamodb-streams
│ └── bootstrap
├── get-product
│ └── bootstrap
├── get-products
│ └── bootstrap
└── put-product
└── bootstrap
5 directories, 5 files
Usage - Output Format
By default, cargo-lambda produces a binary artifact for each Lambda functions in the project. However, you can configure cargo-lambda to produce a ready to upload zip artifact.
The --output-format
paramters controls the output format, the two current options are zip
and binary
with binary
being the default.
Example usage to create a zip.
cargo lambda build --output-format zip
Usage - Architectures
By default, cargo-lambda compiles the code for Linux X86-64 architectures, you can compile for Linux ARM architectures by providing the right target:
cargo lambda build --target aarch64-unknown-linux-gnu
Usage - Compilation Profiles
By default, cargo-lambda compiles the code in debug
mode. If you want to change the profile to compile in release
mode, you can provide the right flag.
cargo lambda build --release
When you compile your code in release mode, cargo-lambda will strip the binaries from all debug symbols to reduce the binary size.
How does cargo-lambda work?
cargo-lambda uses Zig and cargo-zigbuild to compile the code for the right architecture. If Zig is not installed in your host machine, the first time that your run cargo-lambda, it will guide you through some installation options. If you run cargo-lambda in a non-interactive shell, the build process will fail until you install that dependency.