Deploy Rust Lambda functions with .zip file archives
Note
The Rust runtime client
This page describes how to compile your Rust function, and then deploy the compiled binary to AWS Lambda using Cargo
Lambda
Sections
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
-
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. -
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
-
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
-
Before deploying your Rust function, configure AWS credentials on your machine.
aws configure
Deploying the Rust function binary with Cargo Lambda
Use the deploy
cargo lambda deploy
my-function
Deploying your Rust function binary with the AWS CLI
You can also deploy your binary with the AWS CLI.
-
Use the build
subcommand to build the .zip deployment package. cargo lambda build --release --output-format zip
-
To deploy the .zip package to Lambda, run the create-function
command. -
For
--runtime
, specifyprovided.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
\ --runtimeprovided.al2023
\ --rolearn: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.
-
Create an AWS SAM template with the resource and property definition. For
Runtime
, specifyprovided.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
-
Use the build
subcommand to compile the function. cargo lambda build --release
-
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
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.