aws-lambda-rust-runtime

repository·main·Indexed 25 days ago

https://github.com/aws/aws-lambda-rust-runtime

A collection of Rust libraries providing a high-performance, type-safe runtime for AWS Lambda functions. It includes support for HTTP, extensions, and standard Lambda events via the aws_lambda_events package (v1.2.0). The runtime supports ARM 64 architecture and integrates with tools like cargo-lambda for building and deployment, as well as AWS CDK for infrastructure management.

Tokens
46K
Snippets
120
Records
261
Agent score
81%

What's inside aws-lambda-rust-runtime

  1. Overview of AWS Lambda Rust Runtime crates

    main

    The aws-lambda-rust-runtime repository provides several specialized crates for building AWS Lambda functions in Rust:

    • lambda-runtime: The core library providing the runtime for Rust applications.
    • lambda-http: For writing HTTP-based Lambda functions (supports API Gateway, ALB, Lambda Function URLs, and VPC Lattice).
    • lambda-extension: For writing Lambda Runtime Extensions.
    • lambda-events: Provides strongly-typed Rust structs for various Lambda event types.
    • lambda-runtime-api-client: A shared library used by both the runtime and extension libraries to communicate with the AWS Lambda Runtime API.
  2. Use the AWS Lambda Runtime API Client

    main

    The lambda-runtime-api-client crate provides low-level building blocks for interacting with the AWS Lambda Runtime API via REST requests.

    Note: Most users should use lambda_runtime or lambda_extension instead of using this crate directly.

    use http::{Method, Request};
    use hyper::Body;
    use lambda_runtime_api_client::{build_request, Client, Error};
    
    fn register_request(extension_name: &str, events: &[&str]) -> Result<Request<Body>, Error> {
        let events = serde_json::json!({ "events": events });
    
        let req = build_request()
            .method(Method::POST)
            .uri("/2020-01-01/extension/register")
            .header("Lambda-Extension-Name", extension_name)
            .body(Body::from(serde_json::to_string(&events)?));
    
        Ok(req)
    }
    
    #[tokio::main]
    async fn main() -> Result<(), Error> {
        let client = Client::builder().build()?;
        let request = register_request("my_extension", &["INVOKE"])?;
    
        client.call(request).await
    }
  3. Use lambda-http to handle HTTP traffic in AWS Lambda

    main

    The lambda-http crate provides an abstraction that converts HTTP-shaped payloads from various AWS services into standard http objects. This allows you to write a single handler that works across different triggers.

    Supported triggers include:

    • API Gateway: REST, HTTP, and WebSockets APIs
    • ALB: Application Load Balancer
    • Lambda function URLs
    • VPC Lattice

    The handler components are:

    • Request: Represents an incoming HTTP request.
    • IntoResponse: A future that converts a type implementing IntoResponse into a LambdaResponse.
  4. Build and deploy a streaming response Lambda function

    main

    To build and deploy a Lambda function that supports streaming responses, follow these steps:

    1. Install cargo-lambda.
    2. Build the function in release mode using cargo lambda build --release.
    3. Deploy the function to AWS Lambda using cargo lambda deploy --enable-function-url --iam-role YOUR_ROLE.
    4. Crucial Step: In the AWS Lambda console, you must enable streaming response by changing the Function URL's invoke mode to RESPONSE_STREAM.
    5. Verify the streaming behavior by calling the function URL with curl. The output should stream back with a 0.5 second pause between each word.
  5. Build and deploy an AWS Lambda function using cargo-lambda

    main

    To build and deploy the example Lambda function, you must first install cargo-lambda. Once installed, use the following commands to build the release binary and deploy it to AWS Lambda by specifying your target IAM role.

    1. Install cargo-lambda (refer to the cargo-lambda installation guide).
    2. Build the function: cargo lambda build --release
    3. Deploy the function: cargo lambda deploy --iam-role YOUR_ROLE
  6. Add an SQS trigger to the consumer function via AWS CLI

    main

    After deploying the functions, you can link the SQS queue to the consumer function by creating an event source mapping using the AWS CLI. You will need the function name, the AWS region, the SQS queue ARN, and a desired batch size.

    aws lambda create-event-source-mapping \
    --function-name consumer \
    --region <region> \
    --event-source-arn <your-SQS-queue-ARN> \
    --batch-size 1