Deploy Rust Lambda functions with .zip file archives - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

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 Amazon Lambda using Cargo Lambda. It also shows how to deploy the compiled binary with the Amazon Command Line Interface and the Amazon 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 Amazon 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 Amazon 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 Amazon CLI

You can also deploy your binary with the Amazon CLI.

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

    cargo lambda build --release --output-format zip
  2. Deploy the .zip package to Lambda. For --role, specify the ARN of the execution role.

    aws lambda create-function --function-name my-function \ --runtime provided.al2 \ --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 Amazon SAM CLI

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

  1. Create an Amazon SAM template with the resource and property definition. For more information, see AWS::Serverless::Function in the Amazon 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.al2 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 Amazon SAM CLI, see Building Rust Lambda functions with Cargo Lambda in the Amazon 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 Amazon CLI

You can also use the Amazon 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 Amazon CLI version 2. To make this the default setting, run aws configure set cli-binary-format raw-in-base64-out. For more information, see Amazon CLI supported global command line options.