Serverless Rust on AWS Lambda
"for fun and profit"

Luciano Mammino (@loige)

2024-03-19

👋 I'm Luciano (🇮🇹🍕🍝🤌)

👨‍💻 Senior Architect @ fourTheorem

📔 Co-Author of Node.js Design Patterns  👉

Let's connect!

linktr.ee/loige

$ ~ whoami

Grab the slides

Always re-imagining

We are a pioneering technology consultancy focused on AWS and serverless

✉️ Reach out to us at  hello@fourTheorem.com

😇 We are always looking for talent: fth.link/careers

We can help with:

Cloud Migrations

Training & Cloud enablement

Building high-performance serverless applications

Cutting cloud costs

𝕏 loige

𝕏 loige

What is Rust? 🦀

  • A (relatively) new programming language

  • Most loved... for 7 years in a row!

  • Low-level, yet general-purpose

  • Performant & memory-safe

𝕏 loige

Why do I like it ❤️

  • A lovely mascot

  • Strongly typed with a really good type-system

  • Takes inspiration from Haskell, C++, OCaml, JavaScript, Ruby

  • Great (built-in) package manager (Cargo)

  • Good ecosystem of libraries

  • Pattern matching

  • No null, Option & Result types

𝕏 loige

use std::env;

fn main() {
    let region = env::var("AWS_REGION");
}





Result<String, VarError>

𝕏 loige

😀 Happy path

🥺 Sad path

use std::env;

fn main() {
    let region = env::var("AWS_REGION");
    
    match region {
        Ok(region) => println!("Selected region: {}", region),
        Err(_) => println!("Error: AWS_REGION not set"),
    }
}

𝕏 loige

😀 Happy path

🥺 Sad path

use std::env;

fn main() {
    let region = env::var("AWS_REGION")
        .expect("AWS_REGION environment variable not set");
}




String

𝕏 loige

If you cannot get the value, panic!

use std::env;

fn main() {
    let region = env::var("AWS_REGION")
        .unwrap_or_else(|_| "eu-west-1".to_string());
}




Rust makes it very hard for you to ignore possible errors or the absence of values.

𝕏 loige

if you cannot get the value, use a default value!

Serverless, in a nutshell 🥜

  • A way of running applications in the cloud

  • Of course, there are servers... we just don't have to manage them

  • We pay (only) for what we use

  • Small units of compute (functions), triggered by events

𝕏 loige

Serverless... with benefits 🎁

  • More focus on the business logic (generally)

  • Increased team agility (mostly)

  • Automatic scalability (sorta)

  • Not a universal solution, but it can work well in many situations!

𝕏 loige

AWS Lambda

Serverless FaaS offering in AWS

Can be triggered by different kinds of events

  • HTTP Requests
  • New files in S3
  • Jobs in a Queue
  • Orchestrated by Step Functions
  • On a schedule
  • Manually invoked

𝕏 loige

(some) Limitations 😖

  • Maximum execution time is 15 minutes...
  • Payload size (request/response) is limited
  • Doesn't have a GPU option (yet)

... so again, it's not a silver bullet for all your compute problems! 🔫

𝕏 loige

AWS Lambda Pricing 💸

Cost = Allocated Memory 𝒙 time

𝕏 loige

         💰                    🏋️‍♂️                   ⏱️

AWS Lambda Pricing 💸

Cost = Allocated Memory 𝒙 time

𝕏 loige

         💰                    🏋️‍♂️                   ⏱️

AWS Lambda... what about CPU? 🙄

You don't explicitly configure it:

CPU scales based on memory

𝕏 loige

AWS Lambda... what about CPU? 🙄

You don't explicitly configure it:

CPU scales based on memory

Memory vCPUs
128 - 3008 MB 2
3009 - 5307 MB 3
5308 - 7076 MB 4
7077 - 8845 MB 5
8846+ MB 6

𝕏 loige

🏃‍♂️ Lambda execution model

  • It's serverless: it should run only when needed
  • Lambda code is stored in S3
  • event-based: an event can trigger a lambda execution
  • if no instance is available, one is created on the fly (cold-start)
  • if an instance is available and ready, use that one
  • if an instance is inactive for a while, it gets destroyed

𝕏 loige

🏃‍♂️ Lambda execution model

in detail

𝕏 loige

🏃‍♂️ Lambda execution model

Runtime

Handler (logic)

in detail

𝕏 loige

🏃‍♂️ Lambda execution model

Runtime

Handler (logic)

Poll for events

in detail

𝕏 loige

🏃‍♂️ Lambda execution model

Runtime

Handler (logic)

Poll for events

event (JSON)

in detail

𝕏 loige

🏃‍♂️ Lambda execution model

Runtime

Handler (logic)

Poll for events

event (JSON)

execute

in detail

𝕏 loige

🏃‍♂️ Lambda execution model

Runtime

Handler (logic)

Poll for events

event (JSON)

execute

response or
error

in detail

𝕏 loige

🏃‍♂️ Lambda execution model

Runtime

Handler (logic)

Poll for events

event (JSON)

execute

response or
error

response (JSON)
or error

in detail

𝕏 loige

Why Rust + Lambda = ❤️

  • Performance + Efficient memory-wise = COST SAVING 🤑
  • Very fast cold starts! (proof) ⚡️
  • Multi-thread safety 💪
  • No null types + Great error primitives = fewer bugs 🐞

𝕏 loige

Supported Lambda runtimes

  • Node.js

  • Python

  • Java

  • .NET

  • Go

  • Ruby

  • Custom

𝕏 loige

Supported Lambda runtimes

  • Node.js

  • Python

  • Java

  • .NET

  • Go

  • Ruby

  • Custom

RUST?!

𝕏 loige

Rust Runtime for Lambda

𝕏 loige

OK, Let's do this!

𝕏 loige

𝕏 loige

Closing notes

  • Lambda is great (most of the time)
  • Writing Lambdas in Rust is fun and it can be very cost-efficient
  • Still not very common to write Lambdas in Rust, but the tooling is already quite good (Cargo Lambda + SAM)
  • Go, have fun, share your learnings!

𝕏 loige

BONUS: SAM + Cargo Lambda
a complete example

𝕏 loige

BONUS 2: another complete example

𝕏 loige

Thanks to @gbinside, @conzy_m, @eoins, and @micktwomey for kindly reviewing this material!

THANKS!

Grab these slides!

𝕏 loige

Serverless Rust su AWS Lambda "for fun and profit" - Codemotion Workshop Fest 2024

By Luciano Mammino

Serverless Rust su AWS Lambda "for fun and profit" - Codemotion Workshop Fest 2024

Esploriamo l’incrocio tra la flessibilitá di AWS Lambda e le performance di Rust, scoprendo perché Rust è la scelta perfetta per dominare questo ambiente innovativo e dinamico. Durante la sessione scriveremo la prima funzione Lambda “Hello World” utilizzando Rust, scopriremo poi come testarla localmente e come rilasciarla in produzione. Esploreremo poi un caso d’uso piú concreto sviluppando un’API in Rust da rilasciare con SAM per gestire la definizione dell’infrastruttura secondo la metodologia IaC (Infrastructure as Code). Questo workshop, oltre alle conoscenze pratiche, darà l’ispirazione per trasformare il modo di sviluppare applicazioni puntando a pratiche e tecnologie innovative come Serverless e Rust.

  • 1,190