cargo-lambda is a Cargo subcommand to help you work with AWS Lambda.
This subcommand compiles AWS Lambda functions natively and prepares the compiled binaries to upload them to AWS Lambda with other echosystem tools, like SAM Cli or the AWS CDK.
Install this subcommand on your host machine with Cargo itself:
cargo install cargo-lambda
Within a Rust project that includes a Cargo.toml
file, run the cargo lambda build
command to compile your
Lambda functions present in the project. The resulting binary, or binaries, will be placed in the target/lambda
directory. This is an example of what the output of this command is:
❯ tree target/lambda
target/lambda
├── delete-product
│ └── bootstrap
├── dynamodb-streams
│ └── bootstrap
├── get-product
│ └── bootstrap
├── get-products
│ └── bootstrap
└── put-product
└── bootstrap
5 directories, 5 files
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
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.
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.