Giter Club home page Giter Club logo

hello-world-rust-lambda's Introduction

"Hello, World" in Rust, deployed to AWS Lambda

Goals

To learn about:

  • Rust - how to build a hello world application. The basics of the Rust toolchain (cargo, etc)
  • How to deploy a non-standard language to Lambda

Disclaimer: This is my first Rust project (I wrote my first Hello World Rust program only a few hours before starting this). This is a learning project. This isn't an example of my finest coding or my amazing documentation skills. This project is for me.

Helpful posts

Getting Started

Prereqs.

  1. Install Rust/Cargo.
  2. Install the AWS CLI. Run aws configure. See "Creating an execution role in the IAM console" for details on creating the appropriate role.

Generate scaffolding

Use Cargo to generate scaffolding.

cargo new hello-world-rust-lambda

Update the code

Update src/main.rs

use lambda_runtime::{service_fn, LambdaEvent, Error};
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let func = service_fn(func);
    lambda_runtime::run(func).await?;
    Ok(())
}

async fn func(event: LambdaEvent<Value>) -> Result<Value, Error> {
    let (event, _context) = event.into_parts();
    let first_name = event["firstName"].as_str().unwrap_or("world");

    Ok(json!({ "message": format!("Hello, {}!", first_name) }))
}

Update the dependencies

Update Cargo.toml

[dependencies]
lambda_runtime = "0.8.2"
serde_json = "1.0.107"
tokio = "1.32.0"

Build

Build for AWS servers / X86 Linux (not my Apple M2 mac). By default it builds for X86.

cargo lambda build --release --output-format zip

Deploy the function to the Lambda service:

aws lambda create-function --function-name hello-rust-world --handler bootstrap --zip-file fileb://./target/lambda/hello-world-rust-lambda/bootstrap.zip --runtime provided.al2 --role arn:aws:iam::264225025302:role/lambda_execution --environment Variables={RUST_BACKTRACE=1} --tracing-config Mode=Active

Test the invocation from the CLI:

aws lambda invoke --cli-binary-format raw-in-base64-out --function-name hello-rust-world --payload '{"firstName": "Mark"}' output.json

hello-world-rust-lambda's People

Contributors

markmansour avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.