Define Lambda function handler in Rust - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Define Lambda function handler in Rust

Note

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

The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler method. Your function runs until the handler returns a response, exits, or times out.

Write your Lambda function code as a Rust executable. Implement the handler function code and a main function and include the following:

Example — Rust handler that processes JSON events

The following example uses the serde_json crate to process basic JSON events:

use lambda_runtime::{service_fn, LambdaEvent, Error}; use serde_json::{json, Value}; async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> { let payload = event.payload; let first_name = payload["firstName"].as_str().unwrap_or("world"); Ok(json!({ "message": format!("Hello, {first_name}!") })) } #[tokio::main] async fn main() -> Result<(), Error> { lambda_runtime::run(service_fn(handler)).await }

Note the following:

  • use: Imports the libraries that your Lambda function requires.

  • async fn main: The entry point that runs the Lambda function code. The Rust runtime client uses Tokio as an async runtime, so you must annotate the main function with #[tokio::main].

  • async fn handler(event: LambdaEvent<Value>) -> Result<Value,Error>: This is the Lambda handler signature. It includes the code that runs when the function is invoked.

    • LambdaEvent<Value>: This is a generic type that describes the event received by the Lambda runtime as well as the Lambda function context.

    • Result<Value, Error>: The function returns a Resulttype. If the function is successful, the result is a JSON value. If the function is not successful, the result is an error.

Using shared state

You can declare shared variables that are independent of your Lambda function's handler code. These variables can help you load state information during the Init phase, before your function receives any events.

Example — Share Amazon S3 client across function instances

Note the following:

  • use aws_sdk_s3::Client: This example requires you to add aws-sdk-s3 = "0.26.0" to the list of dependencies in your Cargo.toml file.

  • aws_config::from_env: This example requires you to add aws-config = "0.55.1" to the list of dependencies in your Cargo.toml file.

use aws_sdk_s3::Client; use lambda_runtime::{service_fn, Error, LambdaEvent}; use serde::{Deserialize, Serialize}; #[derive(Deserialize)] struct Request { bucket: String, } #[derive(Serialize)] struct Response { keys: Vec<String>, } async fn handler(client: &Client, event: LambdaEvent<Request>) -> Result<Response, Error> { let bucket = event.payload.bucket; let objects = client.list_objects_v2().bucket(bucket).send().await?; let keys = objects .contents() .map(|s| s.iter().flat_map(|o| o.key().map(String::from)).collect()) .unwrap_or_default(); Ok(Response { keys }) } #[tokio::main] async fn main() -> Result<(), Error> { let shared_config = aws_config::from_env().load().await; let client = Client::new(&shared_config); let shared_client = &client; lambda_runtime::run(service_fn(move |event: LambdaEvent<Request>| async move { handler(&shared_client, event).await })) .await }