This is an AWS CDK project in TypeScript building two AWS Lambda functions developed using Rust. It's meant as a showcase to share my experience of using Rust with AWS Lambda. I am interested in Rust because it is a very secure and efficient programming language.
The two Lambda functions are in the /resources
folder:
-
my-function
is a Lambda function packaged using the ZIP format and written in Rust with the AWS Lambda Rust Runtime. This function uses Lambda function URLs to receive and respond to web requests. -
my-web-app
is a Lambda function packaed as a container image using the AWS Lambda Web Adapter to run a Rust web app built using the Actix Web framework. This function is also using Lambda function URLs.
Rust is a very efficient programming language that allows the two functions to run with very little memory and CPU usage. For example, they are both configured with the minimum allowed memory (128 MB). Because Lambda allocates CPU power in proportion to the amount of memory configured, this is also the minimum configurable CPU power. Both functions are are still very performant with a response time of about 1-1.5ms and an initialization time (cold start) of ~20ms for my-function
and ~400ms for my-web-app
.
The description of the stack is in the /lib/my-project-stack.ts
file. This includes instructions to build my-function
using a bundling container.
The instructions to build my-web-app
are in the /resources/my-web-app/Dockerfile
. The Dockerfile uses a multi-stage build to create an executable that is then passed to the final image. The only difference here to make this container image run in AWS Lambda is the COPY
command from the Lambda Web Adapter image.
The cdk.json
file tells the CDK Toolkit how to execute the app.
To deploy this app (including the two Lambda functions) you need:
-
AWS CDK, see the getting started guide. The CDK needs access to AWS credentials that have the necessary permissions to do the deployment. I used the
AdministratorAccess
AWS managed policy. -
Node.js with the
npm
command to install CDK dependencies.
Rust and its compiler are not a requirement because duing the building process they are used inside containers.
Use npm install
to install CDK dependencies.
Then run cdk deploy
and follow the interactive instructions.
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template