Deploy Rust Lambda functions with .zip file archives - AWS Lambda

Deploy Rust Lambda functions with .zip file archives

Note

The Rust runtime client is an experimental package. It is subject to change and intended only for evaluation purposes.

This page describes how to compile your Rust function, and then deploy the compiled binary to AWS Lambda using Cargo Lambda. It also shows how to deploy the compiled binary with the AWS Command Line Interface and the AWS Serverless Application Model CLI.

Prerequisites

Building Rust functions on macOS, Windows, or Linux

The following steps demonstrate how to create the project for your first Lambda function with Rust and compile it with Cargo Lambda.

  1. Install Cargo Lambda, a Cargo subcommand, that compiles Rust functions for Lambda on macOS, Windows, and Linux.

    To install Cargo Lambda on any system that has Python 3 installed, use pip:

    pip3 install cargo-lambda

    To install Cargo Lambda on macOS or Linux, use Homebrew:

    brew tap cargo-lambda/cargo-lambda brew install cargo-lambda

    To install Cargo Lambda on Windows, use Scoop:

    scoop bucket add cargo-lambda scoop install cargo-lambda/cargo-lambda

    For other options, see Installation in the Cargo Lambda documentation.

  2. Create the package structure. This command creates some basic function code in src/main.rs. You can use this code for testing or replace it with your own.

    cargo lambda new my-function
  3. Inside the package's root directory, run the build subcommand to compile the code in your function.

    cargo lambda build --release

    (Optional) If you want to use AWS Graviton2 on Lambda, add the --arm64 flag to compile your code for ARM CPUs.

    cargo lambda build --release --arm64
  4. Before deploying your Rust function, configure AWS credentials on your machine.

    aws configure

Deploying the Rust function binary with Cargo Lambda

Use the deploy subcommand to deploy the compiled binary to Lambda. This command creates an execution role and then creates the Lambda function. To specify an existing execution role, use the --iam-role flag.

cargo lambda deploy my-function

Deploying your Rust function binary with the AWS CLI

You can also deploy your binary with the AWS CLI.

  1. Use the build subcommand to build the .zip deployment package.

    cargo lambda build --release --output-format zip
  2. To deploy the .zip package to Lambda, run the create-function command.

    • For --runtime, specify provided.al2023. This is an OS-only runtime. OS-only runtimes are used to deploy compiled binaries and custom runtimes to Lambda.

    • For --role, specify the ARN of the execution role.

    aws lambda create-function \ --function-name my-function \ --runtime provided.al2023 \ --role arn:aws:iam::111122223333:role/lambda-role \ --handler rust.handler \ --zip-file fileb://target/lambda/my-function/bootstrap.zip

Deploying your Rust function binary with the AWS SAM CLI

You can also deploy your binary with the AWS SAM CLI.

  1. Create an AWS SAM template with the resource and property definition. For Runtime, specify provided.al2023. This is an OS-only runtime. OS-only runtimes are used to deploy compiled binaries and custom runtimes to Lambda.

    For more information about deploying Lambda functions using AWS SAM, see AWS::Serverless::Function in the AWS Serverless Application Model Developer Guide.

    Example SAM resource and property definition for a Rust binary
    AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: SAM template for Rust binaries Resources: RustFunction: Type: AWS::Serverless::Function Properties: CodeUri: target/lambda/my-function/ Handler: rust.handler Runtime: provided.al2023 Outputs: RustFunction: Description: "Lambda Function ARN" Value: !GetAtt RustFunction.Arn
  2. Use the build subcommand to compile the function.

    cargo lambda build --release
  3. Use the sam deploy command to deploy the function to Lambda.

    sam deploy --guided

For more information about building Rust functions with the AWS SAM CLI, see Building Rust Lambda functions with Cargo Lambda in the AWS Serverless Application Model Developer Guide.

Invoking your Rust function with Cargo Lambda

Use the invoke subcommand to test your function with a payload.

cargo lambda invoke --remote --data-ascii '{"command": "Hello world"}' my-function

Invoking your Rust function with the AWS CLI

You can also use the AWS CLI to invoke the function.

aws lambda invoke --function-name my-function --cli-binary-format raw-in-base64-out --payload '{"command": "Hello world"}' /tmp/out.txt

The cli-binary-format option is required if you're using AWS CLI version 2. To make this the default setting, run aws configure set cli-binary-format raw-in-base64-out. For more information, see AWS CLI supported global command line options in the AWS Command Line Interface User Guide for Version 2.