Processing HTTP events with Rust - AWS Lambda

Processing HTTP events with Rust

Note

The Rust runtime client is an experimental package. It is subject to change and intended only for evaluation purposes.

Amazon API Gateway APIs, Application Load Balancers, and Lambda function URLs can send HTTP events to Lambda. You can use the aws_lambda_events crate from crates.io to process events from these sources.

Example — Handle API Gateway proxy request

Note the following:

  • use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse}: The aws_lambda_events crate includes many Lambda events. To reduce compilation time, use feature flags to activate the events you need. Example: aws_lambda_events = { version = "0.8.3", default-features = false, features = ["apigw"] }.

  • use http::HeaderMap: This import requires you to add the http crate to your dependencies.

use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse}; use http::HeaderMap; use lambda_runtime::{service_fn, Error, LambdaEvent}; async fn handler( event: LambdaEvent<ApiGatewayProxyRequest>, ) -> Result<ApiGatewayProxyResponse, Error> { let mut headers = HeaderMap::new(); headers.insert("content-type", "text/html".parse().unwrap()); let resp = ApiGatewayProxyResponse { status_code: 200, multi_value_headers: headers.clone(), is_base64_encoded: Some(false), body: Some("Hello AWS Lambda HTTP request".into()), headers, }; Ok(resp) } #[tokio::main] async fn main() -> Result<(), Error> { lambda_runtime::run(service_fn(handler)).await }

The Rust runtime client for Lambda also provides an abstraction over these event types that allows you to work with native HTTP types, regardless of which service sends the events. The following code is equivalent to the previous example, and it works out of the box with Lambda function URLs, Application Load Balancers, and API Gateway.

Note

The lambda_http crate uses the lambda_runtime crate underneath. You don't have to import lambda_runtime separately.

Example — Handle HTTP requests
use lambda_http::{service_fn, Error, IntoResponse, Request, RequestExt, Response}; async fn handler(event: Request) -> Result<impl IntoResponse, Error> { let resp = Response::builder() .status(200) .header("content-type", "text/html") .body("Hello AWS Lambda HTTP request") .map_err(Box::new)?; Ok(resp) } #[tokio::main] async fn main() -> Result<(), Error> { lambda_http::run(service_fn(handler)).await }

For another example of how to use lambda_http, see the http-axum code sample on the AWS Labs GitHub repository.

Sample HTTP Lambda events for Rust