Distributed Tracing with AWS X-Ray: A Serverless Debugging Guide
Learn to use AWS X-Ray for distributed tracing to visualize your serverless architecture, debug request flows, and pinpoint latency in database calls.

Previously in this course, we built monitoring solutions by creating CloudWatch dashboards and setting up alarms to track high-level metrics. While those tools tell you that something is wrong, they don't always explain why.
Today, we dive into Distributed Tracing with AWS X-Ray. While you might have seen concepts like correlation IDs for distributed tracing or observability in other frameworks, AWS X-Ray provides a native, managed way to gain end-to-end visibility into your serverless requests.
Understanding Distributed Tracing from First Principles
In a monolithic application, you can often trace a request through a single stack trace. In serverless, a single user interaction might trigger an API Gateway call, an authentication check, a Lambda function, and multiple DynamoDB queries.
Distributed tracing works by injecting a "Trace ID" into the header of every request as it enters your system. As the request moves through AWS services, each service records its own "segment" or "subsegment" and passes the Trace ID along. AWS X-Ray collects these pieces, allowing you to see the entire lifecycle of a request on a single timeline.
Enabling X-Ray on Lambda via CDK
To start tracing, you must enable active tracing on your Lambda functions. When using the AWS CDK, this is a simple property update in your stack definition.
Update your backend stack from the previous project integration to enable tracing:
TYPESCRIPTconst myLambda = new lambda.Function(this, CE9178">'MyHandler', { runtime: lambda.Runtime.NODEJS_18_X, handler: CE9178">'index.handler', code: lambda.Code.fromAsset(CE9178">'lambda'), tracing: lambda.Tracing.ACTIVE, // Enable X-Ray });
Once deployed, your Lambda will automatically push trace data to X-Ray. If you are using the AWS SDK for JavaScript (v3), the SDK is "X-Ray aware," meaning it will automatically create subsegments for your DynamoDB calls without extra code.
Analyzing Trace Maps and Latency
Once you trigger your function, head to the X-Ray Trace Map in the AWS console. The map provides a visual graph of your services.
- Service Map: You will see a node for API Gateway, a node for your Lambda, and a node for DynamoDB.
- Color Coding: Nodes are color-coded. Green means healthy, yellow/red indicates increased latency or errors.
- Latency Analysis: Click on a trace to see the waterfall view. This view displays the duration of each segment. If a DynamoDB call takes 200ms but your function only takes 250ms total, you’ve instantly identified that the database is the primary bottleneck.
Hands-on Exercise: Pinpointing a Slow Query
Your task is to identify where your app is spending the most time:
- Deploy the CDK change above to enable
Tracing.ACTIVE. - Trigger your API endpoint 10 times to generate trace data.
- Navigate to the X-Ray Traces console.
- Find a "slow" trace (look for high duration).
- Expand the trace and look at the "Subsegments." Identify the
DynamoDBcall. - Question: Is the
Waittime or theExecutiontime of the database call the main contributor to your latency?
Common Pitfalls
- Forgetting Permissions: While
tracing: ACTIVEis easy to enable, the Lambda execution role must have thexray:PutTraceSegmentsandxray:PutTelemetryRecordspermissions. The CDKtracingproperty usually handles this, but if your role is custom, ensure these are included. - Over-Tracing: Tracing every single request in a high-traffic production app can lead to cost increases. Use the X-Ray sampling rules to record a percentage of requests rather than 100%.
- Ignoring Subsegments: Developers often look at the total duration of the Lambda function but ignore the subsegments inside it. Always expand the trace details to see if the delay is in your code logic, a network call, or a database interaction.
FAQ
Does X-Ray slow down my Lambda functions? There is a negligible overhead for the X-Ray daemon process, but it is generally imperceptible compared to the benefits of debugging visibility.
Can I trace calls to external APIs?
Yes, if you are using the aws-xray-sdk in your Node.js code, you can wrap HTTP clients to capture outgoing requests to third-party services.
How long are traces stored? X-Ray traces are stored for 30 days.
Recap
We’ve enabled X-Ray to move from "it's broken" to "I see exactly where the bottleneck is." By enabling Tracing.ACTIVE and inspecting the waterfall view in the X-Ray console, you can now correlate your infrastructure performance with your application logic.
Up next: We will look at Debugging Serverless Applications, where we combine these X-Ray traces with CloudWatch logs to perform a full root-cause analysis.



