AWS Lambda Functions written in Rust

Roberto Huertas
Koa Health
Published in
4 min readDec 2, 2018

AWS Lambda has recently announced the Runtime APIs and has open sourced a runtime for the Rust language that will allow us to use Rust to write our AWS Lambda Functions.

Let’s build an AWS Lambda Function!

We’re going to extend the basic example from the official repo and provide some tips to make it work with an API Gateway so we can consume our Lambda function from the Internet.

The code of the Lambda function that we’re going to build is here for you to take a look if you want to.

Creating the project

Let’s use cargo to build our project:

cargo new aws-lambda-rust

Once we have created the project, open the Cargo.toml file and make it look like this:

The executable that we have to create needs to be called bootstrap. This is very important because if we don’t stick to this convention our Lambda function won’t work.

As you know, by default, Cargo will use the project’s name for the executable. One easy way to change this behavior is to use autobins = false and explicitely set the executable’s name and path in the bin section:

[[bin]] 
name = "bootstrap"
path = "src/main.rs"

Writing our function

Open the main.rs file and use the code below. Note that this code is 2018 edition. Check out the comments to get a better understanding of what’s going on:

Building our project

AWS Lambda will execute our function in an Amazon Linux Environment.

This means that we’ll have to add a new target called x86_64-unknown-linux-musl:

rustup target add x86_64-unknown-linux-musl

Now, depending on your operating system, you will have to do different things.

You’re using Linux

If you’re using Linux you just have to install the musl-tools:

sudo apt install musl-tools

You’re using Mac OSX

If you happen to be using Mac OSX you will need to add something more.

First, we’re going to install musl-cross using Homebrew, which will be our linker:

brew install filosottile/musl-cross/musl-cross

Then, we’re going to create a .cargo folder with some config file in it:

mkdir .cargo
echo '[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"' > .cargo/config

Finally, in order to avoid some issues with the dependencies, make sure you create a symlink to the new linker:

ln -s /usr/local/bin/x86_64-linux-musl-gcc /usr/local/bin/musl-gcc

Common final step

So, provided that we have already everything set up, we are going to proceed to finally build our project by using Cargo:

cargo build --release --target x86_64-unknown-linux-musl

This will create an executable file called bootstrap in the ./target/x86_64-unknown-linux-musl/release directory.

Unfortunately, AWS Lambda expects the package to be deployed as a zip file, so one final step:

zip -j rust.zip ./target/x86_64-unknown-linux-musl/release/bootstrap

This will just create a rust.zip file in the root of our project.

Deploying the Function

Let’s create a new function by browsing to the AWS Lambda console.

Once there, make sure your screen seems like the picture below and click create function:

Then, in the Function code section, upload the .zip file and press the save button.

If you want, you can test your function. Just remember that we’re expecting an object like this:

You should see something similar to this:

Exposing the function through an API Gateway

Now we’re going to create a new API Gateway so we can access our function from the Internet.

In the Add triggers section, select API Gateway.

Then configure the triggers like this, and click Add and Save:

You should be seeing something similar to the following picture:

You can click over the name of your api (aws-rust-API in our example) and you will be redirected to the API Gateway configuration page. From there you should be able to re-test your API, do some mappings if needed and/or redeploy it.

You can also check that our API is working by browsing to this URL: https://oide37z867.execute-api.eu-west-1.amazonaws.com/default/aws-rust?firstName=READER

Conclusions

As you can see, it’s pretty simple to write an AWS Lambda Function using Rust. You don’t have any excuse now to not start using it for your serverless projects.

Remember that if you want to get access to the full code of this article you can browse to this repo.

Originally published at robertohuertas.com on December 2, 2018.

--

--

Roberto Huertas
Koa Health

A Barcelona-based web & mobile developer wandering through cyberspace. Organizer of @bcnrust. Currently working at Datadog.