How to analyze, debug and trace AWS Lambda function using AWS X-Ray?

Reading Time: 9 minutes

AWS X-Ray helps developers to analyze and debug production and distributed applications. It helps developers in understanding how applications and their underlying services are performing to identify and troubleshoot the root cause of performance issues and errors. In this blog, we will explore how we can make use of AWS X-Ray to monitor, trace and debug Lambda functions eliminating the need for manual intervention of each execution. We will test the trace map for both — the successful and failed executions of the Lambda function. Let’s explore!

In this blog, we will cover:

  • What is AWS X-Ray?
  • AWS X-Ray components
  • Features of AWS X-Ray
  • Companies using AWS X-Ray
  • Hands-on 
  • Conclusion

What is AWS X-Ray?

Debugging applications is the most critical part of the software development process. Developers always require some good tools which can help in debugging applications. AWS X-Ray is the developer’s buddy and helps developers analyze and debug applications using distributed tracing. Using X-Ray developers can pinpoint and understand the root causes of issues, errors, and bottlenecks of the applications.

With X-Ray, developers can identify which part of the application is causing the issue and see the details of the requests being made to the app such as their latency, errors, HTTP response status, etc. Developers can easily understand where mistakes are occurring thanks to X-visual Ray’s map of the application’s architecture. Apps running on EC2 or in containers via the EC2 container service are straightforward to analyze with X-Ray. It can also determine the impact of faults on the application’s users. It can display the application’s service call graph.

As shown in the above image, AWS X-Ray has two main components X-ray SDK and X-ray daemon. There are X-ray SDKs for Java,.NET and Node.js. It allows you to get up and running quickly without having to manually direct your application code to log request metadata. The X-ray daemon collects metrics generated by the SDK and runs on your instances, whether they’re on-premises, in AWS, or even in a Lambda function. It operates as a local buffer and gets data from the SDK over UDP.

The X-Ray SDK provides:

  • Interceptors — trace incoming HTTP requests
  • Client handlers — call other AWS services
  • HTTP client — calls to other HTTP web services, both internal and external

The SDK also includes functionality like instrumenting SQL database calls, automatic AWS SDK client instrumentation, and more.

X-Ray creates a detailed service graph using trace data from AWS resources that support cloud applications.

As shown in the below image, the service graph shows the client, front-end service, and backend services that front-end service calls to process requests and persist data.

The service graph can be used by developers to discover bottlenecks, latency spikes, and other issues that need to be addressed in order to improve the performance of their applications.

Consider the situation where you are pushing data into DocumentDB and it’s not scaling quickly, which is getting you throttled and you want to figure out the requests that are getting throttled. It is very easy to do. 

You have a Trace ID or a series of them, and you can add “throttled” in brackets on the DocumentDB service or in the name of your document, and it will find all of the throttled traces. These traces can be associated with a variety of metadata.

AWS X-Ray components

Features of AWS X-Ray

Companies using AWS X-Ray

Hands-on

In this hands-on, we will implement the execution of a Lambda Function by monitoring and tracing its execution using AWS X-Ray.

To implement this, we will do the following:

  1. Sign in to your AWS console.
  2. Create a Lambda function that is to be monitored by AWS X-Ray.
  3. Enable Active Tracing for your Lambda Function.
  4. Create a test event for your Lambda Function.
  5. Test your Lambda Function using the test event.
  6. Navigate to the AWS X-Ray console and wait for a few minutes for the service to compute a traced map.
  7. View the traces for further details.
  8. Create a test scenario for a failed execution and repeat the steps.

The following image represents the flow of the implementation

Login to your AWS console and navigate to the AWS Lambda console. Click on ‘Functions’ mentioned in the left navigation pane.

AWS X-Ray

Click on ‘Create Function’. Select ‘Author from scratch’, enter the function name, select the runtime as ‘Python 3.7’.

Scroll down and under the Execution role, select ‘Create a new role with Basic Lambda Permissions’. Once done, click on ‘Create Function’.

AWS X-Ray

Upon clicking on ‘Create function’, you will be navigated to the function console with the success message.

AWS X-Ray

Now scroll down to the function code in the editor pane and click on ‘lambda_function.py’.

AWS X-Ray

You will see some sample code in the editor pane for the same function.

Now scroll down to the Monitoring tools section on the Lambda dashboard and click on edit.

AWS X-Ray

You will be navigated to the Edit monitoring tools dashboard. Enable the switch on the left of Active Tracing under the AWS X-Ray section.

Scroll down and click on Save to let AWS X-Ray monitor and trace the actions you perform on the Lambda function.

AWS X-Ray

You will notice the ‘Active tracing’ option switched to Enabled.

AWS X-Ray

Now navigate back to the top of the Lambda Dashboard and click on Test.

A modal to set the configuration for the test event appears. Enter a name for the test event.

AWS X-Ray

Scroll down and click on Create.

Now navigate back to the editor pane of the lambda function.

AWS X-Ray

On the top, search for X-Ray.

Right-click on the service and click on the Open link in the new tab.

AWS X-Ray

You will be navigated to the AWS X-Ray dashboard. Click on ‘Service map’ in the left navigation pane. You won’t see any data since you haven’t performed any action on your lambda function yet.

AWS X-Ray

Navigate back to the Lambda functions editor pane and click on ‘Test’.

You will see the Execution results in a new tab. Now, hit on ‘Test’ multiple times so that AWS X-Ray can trace your actions and form a computing map.

Navigate back to the AWS X-Ray dashboard. Wait for a few minutes and hit Refresh.

After a few minutes, you will see a Computing map on the screen with a loader.

Once loaded, you will see the traced map on the dashboard.

AWS X-Ray

On the bottom right of the screen, to see the services icons, enable the Service icons option.

You can also search for the service for which you have enabled active tracing as shown below.

AWS X-Ray

To see further trace details, click on the service on the map. On the arrows, you will see the success/error rate. As you can see below, the success rate is 100% for the function execution.

The further details for the Lambda function.

Now, scroll down on the ‘Service details’ modal and click on View traces.

AWS X-Ray

You will see the execution results in the Trace list for your lambda function.

Now, to get the details of the latest execution, under the Trace list, click on the arrow on the right of it to filter and get the latest execution.

AWS X-Ray

Now click on the ID of the latest execution from the Trace list section.

Below you can see the further details for your lambda function. Under the Trace map, you can view the response rate as well if you click on the function in the trace map.

To view the response in JSON format, click on the Raw data tab.

AWS X-Ray

Let’s test a negative scenario. Navigate back to the Lambda Function. Alter the function as shown below.

Once done, click on Deploy to deploy the function and click on Test.

You will see that the execution will fail.

AWS X-Ray

Now, navigate back to the AWS X-Ray dashboard and hit on Refresh.

Under the Service map, the color changes for the Lambda function. Select it and scroll down in the Service details modal. In the Service details modal, you will see that the Error rate shows 100% now. Click on View traces.

On this dashboard, you can see all the details for the execution of your Lambda function. In the details pane below, you will see the status shown as a warning sign.

When you hover over the warning sign, you will see a message ‘Error (Click for details)’ as shown below. Click on it to see further details.

A modal will appear with the details for your Lambda function execution. Under Errors & Faults, you will see the Error’s value as true.

AWS X-Ray

This is how AWS X-Ray can monitor and trace all the actions you perform on your Lambda Function and provide each and every minute detail for the same.

Conclusion

In this blog, you have seen how to use AWS X-Ray to instrument your lambda function. Using AWS X-Ray, you can easily monitor, trace and debug your lambda functions without the burden of monitoring your executions or debugging your lambda functions manually. We will discuss more of AWS X-Ray and its other implementations in our upcoming blog. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.

Meanwhile …

Keep Exploring -> Keep Learning -> Keep Mastering

This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.

Back To Top