/rust-on-serverless

Learning Rust in public

Primary LanguageRustMIT LicenseMIT

Rust on Serverless

A short disclaimer, I am learning in public with this repository. The samples may not represent best practices for the Rust language. If you spot anything that could be done better, please reach out and let me know.

Historically, I've always worked with .NET, an object orientated language. This repo is my central place to challenge this object oriented view on the world and start working with a different language. All this learning is within the context of AWS Serverless technologies.

One of the original use cases for Rust is as an embedded systems programming language. Embedded systems are typically extremely resource contrained. When you consider the pricing and execution model of Lambda, it can also be considered a resource contrained language. Making Rust a perfect fit. That perfect performance fit can be seen in these benchmarks.

Samples

All the samples listed below use a combination of cargo lambda and AWS SAM to compile and deploy. If you want to deploy the examples into your own account, from the root of the sample directory, run make build and then sam deploy --guided. For example, if I wanted to deploy the 'HTTP Web Server on Lambda' example I would run:

cd src/axum-lambda-web
make build
sam deploy --guided

Simple Serverless API

Link

A simple API built with API Gateway and single purpose Lambda function handlers. This example also demonstrates how to unit test your serverless applications, using the TestConnection struct provided by the AWS CDK and using Mockall.

HTTP Web Server on Lambda

Link

An Axum web server running on AWS Lambda.

Server Side Rendering on Lambda

Link

An Axum web server running on AWS Lambda. Server side rendering implemented using Ructe.

This example also includes the use of a custom authorizer implemented at the API Gateway level. When deploying the stack, you can specify the password to use for the login page. Once deployed, navigate to the /login endpoint and use the specified password to login.

The login page generates and stores a session token in DynamoDB, with a TTL 5 minutes in the future. The custom authorizer checks DynamoDB to see if a valid token exists.

Event Validation Proxy

Link

A serverless API for proxying events to Amazon Event Bridge and validating the event schema against a predefined schema in DynamoDB.

Serverless ToDo API

Link

A fully serverless ToDo API implemented with API Gateway, Lambda & DynamoDB. Demonstrates how to package multiple handlers in the same project.

This example also demonstrates how hexagaonal architecture practices and domain driven design can be applied to an application written in Rust.

Step Functions Lambda

An order validation pipeline implemented using Step Functions & Rust. A look at how functional programming concepts can work with Rust & AWS Serverless technologies.

This example includes a web socket API at the server side that integrates directly with Amazon SQS. Event Bridge Pipes is then used to take the message from SQS and invoke the order validation Step Function. On completion, a Lambda function sends a message back to the client over the web sockets connection.

A CLI client exists under the client folder.

SQS -> AWS Lambda

Link

A Lambda function sourced by SQS.

Additional Resources