Lambda function errors 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).

Lambda function errors in Rust

Note

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

When your code raises an error, Lambda generates a JSON representation of the error. This error document appears in the invocation log and, for synchronous invocations, in the output. The Rust runtime client also writes the error in the log. The error appears in Amazon CloudWatch Logs by default. This page demonstrates how to return errors in your Lambda function's output.

Creating a function that returns errors

The following code sample shows a Lambda function that returns an error. The Rust Runtime handles this error directly.

use lambda_runtime::{service_fn, Error, LambdaEvent}; use serde_json::{json, Value}; async fn handler(_event: LambdaEvent<Value>) -> Result<Value, String> { Err("something went wrong!".into()) } #[tokio::main] async fn main() -> Result<(), Error> { lambda_runtime::run(service_fn(handler)).await }

This code returns the following error payload:

{ "errorType": "&alloc::string::String", "errorMessage": "something went wrong!" }

For a more advanced error handling example, see the sample application in the Amazon Labs GitHub repository.