Rust in AWS Lambda: a magic ingredient to reduce cost and increase performance?

Rust in AWS Lambda: a magic ingredient to reduce cost and increase performance?

Rust and serverless is a magic combination, according to a presentation at the recent AWS re:Invent conference – potentially several times cheaper than using Python.

Lambda functions and big runtimes are a poor combination – because of both slower cold starts, and higher memory requirements, than native code. On the other hand, many developers are more productive in Java and Python than in system languages like Rust. 

Speaking at re:Invent, Efi Merdler-Kravitz, head of engineering at cloud consultancy CloudEX, based in 
Tel Aviv, Israel, explained that rewriting an entire Lambda function from Python to Rust might not be necessary. He gave an example of a Lambda function which lists S3 (Simple Storage Service) buckets, using Python. It turns out that Boto3, the AWS SDK for Python, is bloated, and that by replacing functions that require Boto3 with Rust there are huge gains in cost and performance – even when Python is still in use for other code.

The key to replacing Python code with Rust is PyO3, which provides Rust bindings for Python. Attributes in Rust code expose functions as a standard Python interface.

The result, in the example Merdler-Kravitz presented, was a three times faster cold start, and much lower memory requirements. With 1GB of RAM the Rust and Boto3 code had similar performance, but with 256MB the Rust-enhanced version still performed well while the other did not.

Cost benefits of using Rust in Lambda: an example presented at re:Invent

Reducing the compute resources required translates to lower cost, especially on a serverless platform. “Rust on average is three to four times cheaper compared to Python,” he claimed.

During re:Invent AWS declared that its SDK for Rust was production-ready. 

Earlier in November the cloud giant added support for Amazon Linux 2023 to Lambda, based on a “minimal container image,” stating that one of its key uses was “when using languages which are compiled to native code, such as Go or Rust.”

There is also an experimental Rust runtime for AWS Lambda, which simplifies running functions in Rust, and includes crates to support Lambda events, extensions, and the Lambda Runtime API. A related project called Cargo Lambda supports local development and testing with the Lambda emulator, and cross-compiling for Linux when developing on Windows or macOS, with a choice of ARM64 and x86-64 regardless of what is running on the developer’s PC. Building Lambda functions with Rust is officially documented here.

Experimental code is not intended for production use, so it is still early days for Rust on Lambda – but it looks like the rewards can be substantial.