Hello, folks! your wait is over, we have come up with a new blog. In this blog, we will discuss how we can write AWS Lambda functions using Rust as a programming language with the help of a sample example. I hope you will enjoy the blog.
AWS Lambda:
AWS Lambda is a compute service that lets you run code without provisioning or managing servers. It executes your code only when needed and scales automatically, from a few requests per day to thousands per second.
You can use AWS Lambda to run your code in response to events, such as changes to data in an Amazon S3 bucket or an Amazon DynamoDB table to run your code in response to HTTP requests using Amazon API Gateway or invoke your code using API calls made using AWS SDKs.
You can also build serverless applications composed of functions that are triggered by events and automatically deploy them using CodePipeline and AWS CodeBuild.
In this guide, we’ll be creating a function which will generate a response from the inputs of the request and return them to the client.
Lambda function with Rust
First, we have to make a new Rust project for the lambda function. So I recommend clone over the content of the directory from the Github repository. I’ll go through each relevant part of the code.
This is the directory structure of our Rust code:
- src
- main.rs
- Cargo.lock
- Cargo.toml
- Readme.md
In these files, the cargo.lock is the auto-generated file of the rust code. And the cargo.toml file is the file which can have all the dependencies related to our Rust code for Lambda function.
[dependencies]
lambda_runtime = "0.1"
serde = "1.0.82"
serde_derive = "1.0.82"
serde_json = "1.0.33"
The Lambda Handler:
AWS Lambda function essentially receives the HTTP request’s (JSON) payload, computes the result, and returns it as an HTTP response (JSON) payload back to the client.
Here, we have defined the structures for the body of the request.
#[derive(Deserialize, Clone)]
struct Body {
#[serde(rename = "firstName")]
first_name: Option<String>,
}
#[derive(Serialize, Clone)]
struct CustomOutput {
body: String,
}
impl CustomOutput {
fn new(body: String) -> Self {
CustomOutput {
body,
}
}
}
So we will request this lambda with this body structure.
Sample Request:
{
"queryStringParameters": {
"firstName": "Pankaj"
},
"body": "{ \"firstName\": \"Chaudhary\" }"
}
The main function will be called for each incoming request, which can further call the my_handler function that can check the request and return the response as per the request.
fn main() -> Result<(), Box<dyn Error>> {
lambda!(my_handler);
Ok(())
}
Building the Project:
If you are building the project for the first time then before building the project we need to set up all the dependencies which are required to build this application. You can follow these commands:
sudo apt install musl-tools
rustup target add x86_64-unknown-linux-musl
Now your system is ready to build the project. To Build the rust code run this command:
cargo build --release --target x86_64-unknown-linux-musl
Your project is built successfully. Then we have to make the zip file of this code to run it in the AWS Lambda.
zip -j rust.zip ./target/x86_64-unknown-linux-musl/release/bootstrap
Now we have the zip file of our lambda function which we need to upload in the AWS Lambda. For that first, we have to make a lambda on the AWS console lambda section.

After building the lambda function you have to upload the zip file(which we have built above) into this Lambda.



Then you have to trigger the lambda function by the test request where you can provide your sample request.



When you run the test, the lambda will trigger and return the response.



You have successfully built the sample application of Lambda function using the Rust programming language.
Note: I hope our blogs help you to enhance your learning. I’ll post more blogs on Rust. Stay Tuned.
If you want to read more content like this? Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7 .
Happy learning!!!
References:
- https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html
- https://aws.amazon.com/es/blogs/opensource/rust-runtime-for-aws-lambda/