Deploy Rust Lambda functions with .zip file archives
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.
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, a third-party open-source extension to the Cargo command-line tool that simplifies building and deploying Rust Lambda functions.
-
Install Cargo Lambda, a third-party open-source extension to the Cargo command-line tool that simplifies building and deploying Rust Lambda functions:
cargo install cargo-lambdaFor other installation 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 newmy-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
--arm64flag 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 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 deploymy-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-namemy-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 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-namemy-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.
Warning Javascript is disabled or is unavailable in your browser.
To use the Amazon Web Services Documentation, Javascript must be enabled. Please refer to your browser's Help pages for instructions.