Cargo Lambda
website·Indexed 34 days ago
https://www.cargo-lambda.infoA Cargo subcommand for building, deploying, and managing Rust functions on AWS Lambda. It provides tools for cross-compiling to Linux and ARM processors using Zig, local emulation via the watch and invoke commands, and streamlined deployment to AWS.
What's inside Cargo Lambda
- Cargo Lambda is a subcommand for Cargo (the Rust package manager) specifically designed to facilitate the creation and deployment of Rust functions on AWS Lambda. It provides the necessary tools and workflows to build Rust-based Lambda functions from scratch and offers efficient mechanisms for building and deploying these functions into production environments on AWS.
Overview of Cargo Lambda subcommands
Cargo Lambda provides a suite of tools to manage the lifecycle of Rust AWS Lambda functions, from project initialization to deployment and local testing.Configure custom rustflags in Cargo Lambda
Cargo Lambda automatically sets thetarget-cpuflag based on the architecture (Neoverse N1 for ARM, Haswell for X86-64). If you need to add additional flags in.cargo/config.toml, you must definerustflagsas an array of strings. Using a single string will prevent thetarget-cpuflag from being merged correctly.# Correct: Use an array to ensure target-cpu is merged [build] rustflags = ["--cfg", "tracing_unstable"]Understand Cargo Lambda default release optimizations
When building with the
--releaseflag, Cargo Lambda automatically applies several optimizations to reduce binary size and improve performance for AWS Lambda environments:- Strip Symbols: Removes symbolic references used for debugging to reduce binary size and improve startup time.
- Link Time Optimization (LTO): Uses
lto="thin"to produce optimized code without significantly sacrificing link speed. - Parallel Code Generation Units: Uses
codegen-units=1to disable parallel compilation, allowing for a more optimized binary at the cost of longer compilation times. - Panic Behavior: Uses
panic=abortto remove unwinding behavior, reducing the binary size since unwinding is not useful in AWS Lambda.
Deploy a Rust function to AWS Lambda
The
cargo lambda deploysubcommand uploads a previously built binary to AWS Lambda to create or update a function. It automatically detects the binary's architecture. The binary must be created using thecargo lambda buildcommand before deploying.Basic usage:
- To deploy a function using default settings:
cargo lambda deploy - To use a specific AWS profile: use
-por--profile.
cargo lambda deploy- To deploy a function using default settings:
Inspect Zig installation and project configuration
Use thecargo lambda system(orcargo lambda config) command to retrieve information about the current Zig installation and the project's configuration. By default, the output is intextformat, but it can be switched tojsonusing the--output-formatflag. For projects using a workspace, you can target a specific package using the--packageflag.# Show general system and config information cargo lambda system # Show information for a specific package in a workspace cargo lambda system --package package-name # Output the configuration in JSON format cargo lambda system --output-format jsonBuild for ARM64 architecture
By default,cargo-lambdacompiles for Linux X86-64. To compile for Linux ARM architectures, use the--arm64shortcut (available since v0.6.2) or specify the full target triple.# Using the shortcut cargo lambda build --arm64 # Using the target triple cargo lambda build --target aarch64-unknown-linux-gnuCross-compile Rust functions for AWS Lambda
Use cargo-lambda to cross-compile Rust functions for Linux and ARM processors. This tool allows developers to build for the AWS Lambda environment regardless of whether the host machine is running macOS, Windows, or Linux, leveraging Zig for the cross-compilation process.Cross-compile AWS Lambda functions using Cargo Lambda
Cargo Lambda allows you to compile Rust code for Linux targets (ARM64 and X86-64) regardless of your host OS (Windows, macOS, or Linux). By default, it uses the Zig toolchain viacargo-zigbuild, which is recommended for most use cases and pure Rust functions because it simplifies the compilation of crates with C components.Test Lambda Function URLs locally
The emulator supports Lambda function URLs. Local URLs are served under the
/lambda-urlpath athttp://localhost:9000.Requirements:
- The function must respond to Request events using
lambda_httpor rawApiGatewayV2httpRequestevents. - The
lambda_httpdependency MUST have theapigw_httpfeature enabled (theapigw_restformat is not supported).
Routing:
- Single function: Access via
http://localhost:9000. - Multi-package/binary: Access via
http://localhost:9000/lambda-url/FUNCTION_NAME. - Filtering: Use
--packageor--binflags to limit which function is available at the root path.
# Start the emulator cargo lambda watch # If limiting to a specific binary cargo lambda watch --bin my-binary- The function must respond to Request events using
Deploy functions from a Rust workspace with multiple packages
When working in a workspace with multiple packages, you can explicitly specify which binary to deploy and what the resulting Lambda function should be named.# 1. Deploy using the package name as the function name cargo lambda deploy PACKAGE_NAME # 2. Deploy a specific package as a function with a different name cargo lambda deploy --binary-name PACKAGE_NAME FUNCTION_NAME # 3. Deploy a specific binary file (must be named 'bootstrap') as a function cargo lambda deploy --binary-path PATH_TO_BOOTSTRAP_FILE FUNCTION_NAMECreate a new Rust AWS Lambda function
Thenewcommand generates a new Rust package with a basic skeleton for AWS Lambda functions in a new sub-directory. By default, it uses a template that supports HTTP Lambda functions and events from theaws_lambda_eventscrate.cargo lambda new PACKAGE-NAME