Skip to content

cargo lambda build

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

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 parameter 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

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

ℹ️ Starting in version 0.6.2, you can use the shortcut --arm64 to compile your functions for Linux ARM architectures:

cargo lambda build --arm64

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.

Extensions

cargo-lambda can also build Lambda Extensions written in Rust. If you want to build a extension, use the flag --extension to put the output under target/lambda/extensions, so you don't mix extensions and functions.

cargo lambda build --release --extension

If you want to create a zip file with the structure that AWS Lambda expects to find extensions in, add the --output-format flag to the previous command, and cargo-lambda will zip the extensions directory with your extension inside.

cargo lambda build --release --extension --output-format zip

Compiler backends

Cargo Lambda has an internal abstraction to work with different ways to compile functions.

The default compiler is cargo-zigbuild. This compiler uses Zig to cross compile any Rust project to a Linux target on your own OS, without the need to a virtual machine or a Linux container. 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.

Cargo Lambda also supports building Rust projects without Zig as the target linker. This compiler is identifed as just cargo. A disadvantage of this is that it's up to you to guarantee that the binary works on Linux. An advantage is that if you always build functions on Linux, you don't need to install Zig to use Cargo Lambda.

To switch compilers, you can use the flag --compiler with the name of the compiler to use when you run cargo lambda build. For example:

cargo lambda build --compiler cargo

You can also use an environment variable to select the compiler:

export CARGO_LAMBDA_COMPILER=cargo
cargo lambda build

Additionally, you can also add this option in your project's Cargo.toml metadata. Add the snippet below if you want to use Cargo without Zig as linker in your project:

[package.metadata.lambda.build.compiler]
type = "cargo"

Additional compilers

The concept of compilers on Cargo Lambda is an abstraction on top of different shell commands. If you want to add an additional compiler, you need to implement Compiler trait. The command to execute needs to follow Rust compilations' convenctions, for example, if the user wants to build an Arm64 binary with the release profile, Cargo Lambda will expect that the resulting binary is in target/aarch64-unknown-linux-gnu/release/.

Released under the MIT License.