Log and monitor Rust Lambda functions
Note
The Rust runtime client
Amazon Lambda automatically monitors Lambda functions on your behalf and sends logs to Amazon CloudWatch. Your Lambda function comes with a CloudWatch Logs log group and a log stream for each instance of your function. The Lambda runtime environment sends details about each invocation to the log stream, and relays logs and other output from your function's code. For more information, see Using CloudWatch Logs with Lambda. This page describes how to produce log output from your Lambda function's code.
Creating a function that writes logs
To output logs from your function code, you can use any logging function that writes
to stdout
or stderr
, such as
the println!
macro. The following example uses println!
to
print a message when the function handler starts and before it finishes.
use lambda_runtime::{service_fn, LambdaEvent, Error}; use serde_json::{json, Value}; async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> { println!("Rust function invoked"); let payload = event.payload; let first_name = payload["firstName"].as_str().unwrap_or("world"); println!("Rust function responds to {}", &first_name); Ok(json!({ "message": format!("Hello, {first_name}!") })) } #[tokio::main] async fn main() -> Result<(), Error> { lambda_runtime::run(service_fn(handler)).await }
Implementing advanced logging with the Tracing crate
Tracingsubscriber
before implementing the function handler. Then, you can use
tracing macros like debug
, info
, and error
, to specify the level of logging that you want for each scenario.
Example — Using the Tracing crate
Note the following:
-
tracing_subscriber::fmt().json()
: When this option is included, logs are formatted in JSON. To use this option, you must include thejson
feature in thetracing-subscriber
dependency (for example,tracing-subscriber = { version = "0.3.11", features = ["json"] }
). -
#[tracing::instrument(skip(event), fields(req_id = %event.context.request_id))]
: This annotation generates a span every time the handler is invoked. The span adds the request ID to each log line. -
{ %first_name }
: This construct adds thefirst_name
field to the log line where it's used. The value for this field corresponds to the variable with the same name.
use lambda_runtime::{service_fn, Error, LambdaEvent}; use serde_json::{json, Value}; #[tracing::instrument(skip(event), fields(req_id = %event.context.request_id))] async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> { tracing::info!("Rust function invoked"); let payload = event.payload; let first_name = payload["firstName"].as_str().unwrap_or("world"); tracing::info!({ %first_name }, "Rust function responds to event"); Ok(json!({ "message": format!("Hello, {first_name}!") })) } #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt().json() .with_max_level(tracing::Level::INFO) // this needs to be set to remove duplicated information in the log. .with_current_span(false) // this needs to be set to false, otherwise ANSI color codes will // show up in a confusing manner in CloudWatch logs. .with_ansi(false) // disabling time is handy because CloudWatch will add the ingestion time. .without_time() // remove the name of the function from every log entry .with_target(false) .init(); lambda_runtime::run(service_fn(handler)).await }
When this Rust function is invoked, it prints two log lines similar to the following:
{"level":"INFO","fields":{"message":"Rust function invoked"},"spans":[{"req_id":"45daaaa7-1a72-470c-9a62-e79860044bb5","name":"handler"}]} {"level":"INFO","fields":{"message":"Rust function responds to event","first_name":"David"},"spans":[{"req_id":"45daaaa7-1a72-470c-9a62-e79860044bb5","name":"handler"}]}