Skip to main content
This guide shows how to instrument an AWS Lambda function with AWS X-Ray to collect distributed tracing data. Follow these steps to create a simple Lambda function, enable active X-Ray tracing, invoke the function to generate trace segments, and inspect traces and metrics in the X-Ray console. What you’ll learn
  • Create a basic Lambda function for tracing
  • Enable X-Ray tracing for the function
  • Generate trace data by invoking the function
  • Inspect traces, metrics, and related CloudWatch Logs in X-Ray
Useful links

1. Create a basic Lambda function

  1. Open the Lambda console and choose Create function.
  2. Select Author from scratch, give the function a name (for example, xray-demo), and choose a Python runtime.
  3. Leave other settings as defaults and create the function.
A screenshot of the AWS Lambda console showing a green success banner confirming the newly created "xray-demo" function. The Function overview panel is visible with buttons like Copy ARN, Actions, and Export to Infrastructure Composer.

2. Enable X-Ray tracing for the function

  1. Open your function and go to the Configuration tab.
  2. Under Monitoring and operations tools, choose Edit.
  3. In Additional monitoring tools enable Lambda service traces (this enables active X-Ray tracing for the function).
  4. Save the change.
A screenshot of the AWS Lambda console showing the "CloudWatch Application Signals and AWS X‑Ray" monitoring tools page for a function, with "Application Signals" unchecked and "Lambda service traces" enabled. The page also lists supported runtimes and includes the AWS header and navigation elements.
When you enable tracing from the Lambda console, Lambda will add the necessary IAM permissions to the function’s execution role so the function can send trace segments to X-Ray. After the first invocations, traces may take a short time to appear in the X-Ray console.

3. Create and run a test event

  • Switch to the Test tab and create a new test event (for example, X-Ray test).
  • Use a simple sample JSON payload such as:
  • Save the test event and invoke the function from the console several times to generate trace segments.
A sample function ARN format (for reference):
Sample response returned by a simple Lambda function:
Invoke the function a few times so that X-Ray has multiple segments to display.
A screenshot of the AWS Lambda console on the "Test" tab showing a green banner that says "Executing function: succeeded" with links to logs. The lower part shows the "Test event" settings and a highlighted orange "Test" button.

4. Open X-Ray and inspect traces

  • Open the X-Ray console (also available via CloudWatch > X-Ray).
  • Use the Trace map to view the topology of services and clients. Toggle between the map and the list view depending on whether you prefer a visual topology or a condensed list of services and segments.
A screenshot of the AWS CloudWatch X-Ray Trace Map page showing a "Nodes" list with two "xray-demo" Lambda entries and columns for latency, faults (5xx), and request rates. The top bar shows the AWS console navigation and account info.

5. View metrics and response-time distribution

X-Ray surfaces helpful metrics for the selected service:
  • Latency (p95, p50, etc.)
  • Request rate (requests per second)
  • Faults (5xx errors)
Use the response-time distribution to identify latency patterns and outliers.
Screenshot of an AWS CloudWatch Trace Map metrics dashboard showing charts for latency, request rate, and 5xx faults over time. The header also shows the AWS console navigation and account/region info.
A screenshot of the AWS CloudWatch console showing the X-Ray "xray-demo" Lambda function page with buttons like "View logs" and a "Response time distribution" chart/filter. The lower area displays a declining response-time line graph.

6. List and inspect individual traces

  • In the Traces view you can see each recorded trace (typically one per invocation when segments are emitted). Select a trace to view detailed information: status, response code, duration, and segment timelines.
  • Use these details to investigate errors, slow segments, and which downstream services contributed to latency.
Example service description shown by X-Ray:
A screenshot of the AWS CloudWatch Traces console showing a list of trace IDs with status "OK", timestamps, response codes (200) and response times. The browser toolbar and AWS account/header are visible at the top.
A screenshot of the AWS CloudWatch X-Ray trace details page showing "xray-demo" Lambda traces. It shows segment status (OK), response codes (200) and durations (e.g., 13ms, 1ms) for the listed segments.

7. View CloudWatch Logs for a trace

  • From a trace detail you can jump to the related CloudWatch Logs for the Lambda invocation. Logs typically include the START / REPORT / END lines plus any application logs you wrote.
  • Use the logs to correlate X-Ray segments with application-level events and debugging output.
Example CloudWatch Logs lines (simplified):

Quick reference — X-Ray UI checklist

Wrap-up

After enabling tracing and invoking the function, AWS X-Ray captures a segment for each invocation. Use the trace map, metrics, traces list, trace details, and CloudWatch Logs together to pinpoint performance bottlenecks and faults across services. Instrumentation with X-Ray provides the visibility needed to analyze request flows and troubleshoot distributed systems.

Watch Video

Practice Lab