If you’ve ever dealt with the dreaded ‘cold start’ in AWS Lambda using Java or Python, you know the pain. In my experience, moving performance-critical functions to Rust is the single biggest win for reducing latency and lowering your AWS bill. But if you’re wondering how to deploy Rust to AWS Lambda without spending hours fighting the toolchain, you’re in the right place.

Rust isn’t natively supported in the same way Node.js is, but thanks to the Custom Runtime (provided by AWS) and the community-driven cargo-lambda tool, the process has become remarkably streamlined. Whether you’re building a high-throughput API or a background worker, Rust’s memory safety and speed make it an ideal choice for serverless.

Prerequisites

Before we dive into the deployment steps, make sure you have the following installed on your machine:

If you are new to the ecosystem, I highly recommend reading my Rust for DevOps engineers guide to get comfortable with the language basics before jumping into serverless.

Step 1: Initialize Your Project

Forget the standard cargo new. To ensure your project structure is compatible with AWS Lambda from day one, use the cargo lambda new command.

cargo lambda new my-rust-lambda
cd my-rust-lambda

This creates a project with the necessary dependencies, including lambda_runtime and tokio. Since Lambda functions are asynchronous by nature, using an async runtime is non-negotiable. If you want to dive deeper into how the async magic works, check out my Rust concurrency tutorial on Tokio.

Step 2: Writing the Handler

Open src/main.rs. You’ll see a basic handler. Let’s implement a simple API response that handles a JSON payload. I’ve found that using serde for serialization is the gold standard here.

use lambda_runtime::{service_fn, LambdaEvent, Error};
use serde::{Deserialize, Serialize};

#[derive(Deserialize)]
struct Request {}

#[derive(Serialize)]
struct Response {
    message: String,
}

async fn function_handler(_event: LambdaEvent) -> Result {
    Ok(Response {
        message: "Hello from Rust on AWS Lambda!".to_string(),
    })
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    let func = service_fn(function_handler);
    lambda_runtime::run(func).await?
    Ok(())}

Step 3: Compiling for the AWS Environment

The biggest hurdle when learning how to deploy Rust to AWS Lambda is the target architecture. Your local Mac or Windows machine won’t produce a binary that runs on Amazon Linux. This is where cargo-lambda shines by handling cross-compilation automatically.

Run the following command to build a production-ready binary optimized for ARM64 (Graviton2), which offers better price-performance than x86:

cargo lambda build --release --arm64

As shown in the image below, the build process will compile your code and package it into a bootstrap zip file located in target/lambda/.

Terminal output showing cargo-lambda build process and resulting bootstrap zip file
Terminal output showing cargo-lambda build process and resulting bootstrap zip file

Step 4: Deploying to AWS

You can upload the zip file manually via the AWS Console, but in my setup, I always use the CLI for repeatability. You can deploy directly using cargo lambda:

cargo lambda deploy
--function-name my-rust-lambda
--env-var KEY=VALUE

If this is your first time, the tool will prompt you to create the necessary IAM role. I recommend creating a dedicated role with the AWSLambdaBasicExecutionRole policy attached.

Pro Tips for Production

Troubleshooting Common Issues

“Runtime.ExitError: Process exited before completing request”

This usually happens if your handler panics or if you have a version mismatch between the lambda_runtime and the AWS environment. Ensure you are using the latest version of the runtime crate.

Permission Denied on Deployment

Double-check that your local AWS profile has lambda:CreateFunction and iam:PassRole permissions. If you’re using a CI/CD pipeline, ensure the Github Action or GitLab Runner has an attached IAM role with these specific permissions.

What’s Next?

Now that you’ve mastered how to deploy Rust to AWS Lambda, you can start optimizing. I suggest exploring AWS Lambda Extensions for monitoring or implementing a Rust-based API Gateway using axum for more complex routing. If you’re looking to scale this, look into the AWS CDK (Cloud Development Kit) to manage your Rust infrastructure as code.