AWS Lambda with Rust

Reading Time: 4 minutes

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.

This image has an empty alt attribute; its file name is screenshot-203.png

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

This image has an empty alt attribute; its file name is screenshot-204.png

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

This image has an empty alt attribute; its file name is screenshot-205.png

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

This image has an empty alt attribute; its file name is screenshot-206.png

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!!!

This image has an empty alt attribute; its file name is screenshot-from-2020-06-08-11-00-35.png

References:

This image has an empty alt attribute; its file name is footer-2.jpg

Written by 

Pankaj Chaudhary is a Software Consultant at Knoldus LLP. He has 1.5+ years of experience with good knowledge of Rust, Python, Java, and C. Now he is working as Rust developer and also works on machine learning and data analysis because he loves to play with data and extract some useful information from it. His hobbies are bike riding and explore new places.