> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Instrumenting an App With X ray

> Guide to instrument an AWS Lambda function with AWS‑X‑Ray to enable active tracing, generate trace data, and inspect traces, metrics, and CloudWatch logs for debugging and performance analysis.

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

* [AWS X-Ray Developer Guide](https://docs.aws.amazon.com/xray/latest/devguide/aws-xray.html)
* [AWS Lambda Developer Guide — Monitoring and Troubleshooting](https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html)

## 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.](https://mintcdn.com/kodekloud-c4ac6d9a/FoKJ_KwuI1oFSFo9/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/aws-lambda-xray-demo-success-banner.jpg?fit=max\&auto=format\&n=FoKJ_KwuI1oFSFo9\&q=85\&s=628a67b161f477c3d90e7025e7f8ddeb)

## 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.](https://mintcdn.com/kodekloud-c4ac6d9a/FoKJ_KwuI1oFSFo9/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/aws-lambda-cloudwatch-xray-console.jpg?fit=max\&auto=format\&n=FoKJ_KwuI1oFSFo9\&q=85\&s=d28ecd24d5e2d40b6e7ff1329d339c30)

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:

```json theme={null}
{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}
```

* Save the test event and invoke the function from the console several times to generate trace segments.

A sample function ARN format (for reference):

```text theme={null}
arn:aws:lambda:us-east-2:635573991785:function:xray-demo
```

Sample response returned by a simple Lambda function:

```json theme={null}
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}
```

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.](https://mintcdn.com/kodekloud-c4ac6d9a/FoKJ_KwuI1oFSFo9/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/aws-lambda-test-success-banner.jpg?fit=max\&auto=format\&n=FoKJ_KwuI1oFSFo9\&q=85\&s=5d6feb882151dc64531c5857d6739bb1)

## 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.](https://mintcdn.com/kodekloud-c4ac6d9a/FoKJ_KwuI1oFSFo9/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/aws-cloudwatch-xray-trace-map-lambdas.jpg?fit=max\&auto=format\&n=FoKJ_KwuI1oFSFo9\&q=85\&s=d3a6f959ae3f363b1b7883d3195bb2d5)

## 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.](https://mintcdn.com/kodekloud-c4ac6d9a/FoKJ_KwuI1oFSFo9/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/cloudwatch-trace-map-latency-requests-5xx.jpg?fit=max\&auto=format\&n=FoKJ_KwuI1oFSFo9\&q=85\&s=10259cdb76fb667ee289be0a7879ff2c)

![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.](https://mintcdn.com/kodekloud-c4ac6d9a/FoKJ_KwuI1oFSFo9/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/aws-cloudwatch-xray-lambda-response-time.jpg?fit=max\&auto=format\&n=FoKJ_KwuI1oFSFo9\&q=85\&s=c7eb2d77f50ff5c47f2159a6ef8384c3)

## 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:

```text theme={null}
service(id(name: "xray-demo", type: "AWS::Lambda::Function", account.id: "635573991785"))
```

![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.](https://mintcdn.com/kodekloud-c4ac6d9a/FoKJ_KwuI1oFSFo9/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/aws-cloudwatch-traces-list-ok-200.jpg?fit=max\&auto=format\&n=FoKJ_KwuI1oFSFo9\&q=85\&s=66ef452bb6d75a967663f1d2a14f8cda)

![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.](https://mintcdn.com/kodekloud-c4ac6d9a/SSy1aL2mVSdeEi1p/images/AWS-Certified-DevOps-Engineer-Professional/Domain-4-Monitoring-and-Logging/Demo-Instrumenting-an-App-With-X-ray/xray-demo-lambda-cloudwatch-traces.jpg?fit=max\&auto=format\&n=SSy1aL2mVSdeEi1p\&q=85\&s=4c80d1bc8e3843d12f7bff1bb2ee05ff)

## 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):

```text theme={null}
635573991785:/aws/lambda/xray-demo  2026-09-29T23:53:17.046Z  START RequestId: c883835f-b0c2-482b-8728-b96b73de6858 Version: $LATEST
635573991785:/aws/lambda/xray-demo  2026-09-29T23:53:17.048Z  REPORT RequestId: c883835f-b0c2-482b-8728-b96b73de6858  Duration: 1.23 ms  Billed Duration: 100 ms  Memory Size: 128 MB  Max Memory Used: 20 MB
635573991785:/aws/lambda/xray-demo  2026-09-29T23:53:17.048Z  END RequestId: c883835f-b0c2-482b-8728-b96b73de6858
```

## Quick reference — X-Ray UI checklist

| UI area | Purpose | What to look for |
| - | - | - |
| Trace map | Visual topology of services | Nodes for Lambda, downstream services, error/fault edges |
| Metrics | Performance overview | Latency, request rate, 5xx faults |
| Traces list | Per-invocation segments | Response codes, durations, trace IDs |
| Trace details | Timeline & segment annotations | Subsegment timing, downstream calls, exceptions |
| Logs | Application context | START/REPORT/END and application logs to correlate events |

## 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](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/15ed9473-60f4-466a-b7b2-3d9cb829e847/lesson/6e1d982d-9e79-4eb0-b9b1-57a1fb68cc74)**

> **[Practice Lab](https://learn.kodekloud.com/user/courses/aws-certified-devops-engineer-professional/module/15ed9473-60f4-466a-b7b2-3d9cb829e847/lesson/bfc472c2-7289-44a4-8c4e-96fbd4ed56ae)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.