Image by Author
Image by Author

Debugging AWS Lambda Functions with AWS X-Ray

Eden Hare
Level Up Coding
Published in
12 min readJan 18, 2021

--

AWS X-Ray is a method of collecting data about the work going on within a distributed application to identify performance issues and execution errors.

When execution errors happen in obscure cases, X-Ray can help identify those cases by providing a consolidated trace of each execution. This is invaluable in a distributed, event-driven or serverless application.

AWS X-Ray integrates with

  • Amazon Elastic Compute Cloud (Amazon EC2);
  • Amazon Elastic Container Service (Amazon ECS);
  • Elastic Load Balancing;
  • Amazon API Gateway;
  • AWS Lambda; and,
  • AWS Elastic Beanstalk.

Additionally, the AWS X-Ray SDK can capture metadata and requests to

  • MySQL and PostgreSQL databases (self-hosted, Amazon RDS, Amazon Aurora);
  • Amazon DynamoDB;
  • Amazon Simple Queue Service; and,
  • Amazon Simple Notification Service.

The SDK is available for Java, Go, Node.js, Python, Ruby, and .NET languages.

When working with AWS Lambda, the only thing we can see is what gets written in our CloudWatch logs when our Lambda function is running. However, solving problems with the function when we can’t see what is happening between API Gateway, Lambda and other functions or services can be a challenge. By instrumenting our Lambda function and its related services with X-Ray, we improve our ability to solve issues with our functions.

This article is going to discuss how to trace your Lambda functions using AWS X-Ray. The files used in this sample are available on GitHub using Python as the Lambda function language.

Our simple Lambda Function

To illustrate how we will enable X-Ray tracing, I am going to use a Serverless Application Model (SAM) application with an HTTP API Gateway, Step Functions, and several Lambda Functions.

The template builds out this architecture:

--

--

Eden is the co-author of seven books and author of more than 100 articles and book chapters in technical, management, and information security publications.