Cargo Lambda

website·Indexed 34 days ago

https://www.cargo-lambda.info

A 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.

Tokens
13.3K
Snippets
91
Records
106
Agent score
71%

What's inside Cargo Lambda

  1. Introduction to Cargo Lambda for AWS Lambda development

    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.
  2. Configure custom rustflags in Cargo Lambda

    Cargo Lambda automatically sets the target-cpu flag 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 define rustflags as an array of strings. Using a single string will prevent the target-cpu flag from being merged correctly.
    # Correct: Use an array to ensure target-cpu is merged
    [build]
    rustflags = ["--cfg", "tracing_unstable"]
  3. Understand Cargo Lambda default release optimizations

    When building with the --release flag, 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=1 to disable parallel compilation, allowing for a more optimized binary at the cost of longer compilation times.
    • Panic Behavior: Uses panic=abort to remove unwinding behavior, reducing the binary size since unwinding is not useful in AWS Lambda.
  4. Deploy a Rust function to AWS Lambda

    The cargo lambda deploy subcommand 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 the cargo lambda build command before deploying.

    Basic usage:

    • To deploy a function using default settings: cargo lambda deploy
    • To use a specific AWS profile: use -p or --profile.
    cargo lambda deploy
  5. Inspect Zig installation and project configuration

    Use the cargo lambda system (or cargo lambda config) command to retrieve information about the current Zig installation and the project's configuration. By default, the output is in text format, but it can be switched to json using the --output-format flag. For projects using a workspace, you can target a specific package using the --package flag.
    # 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 json
  6. Build for ARM64 architecture

    By default, cargo-lambda compiles for Linux X86-64. To compile for Linux ARM architectures, use the --arm64 shortcut (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-gnu
  7. Cross-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.
  8. 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 via cargo-zigbuild, which is recommended for most use cases and pure Rust functions because it simplifies the compilation of crates with C components.
  9. Test Lambda Function URLs locally

    The emulator supports Lambda function URLs. Local URLs are served under the /lambda-url path at http://localhost:9000.

    Requirements:

    • The function must respond to Request events using lambda_http or raw ApiGatewayV2httpRequest events.
    • The lambda_http dependency MUST have the apigw_http feature enabled (the apigw_rest format 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 --package or --bin flags 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
  10. 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_NAME
  11. Create a new Rust AWS Lambda function

    The new command 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 the aws_lambda_events crate.
    cargo lambda new PACKAGE-NAME