Tracing requests with AWS X-Ray - AWS Lambda

Tracing requests with AWS X-Ray

While monitoring individual Lambda functions is useful, most serverless applications consist of multiple service integrations. Tracking performance problems or errors usually means following a transaction from the source caller through all the downstream services. While it’s possible to do this with each service’s logs, it’s faster and more convenient to use a tracing tool like AWS X-Ray.

To use X-Ray in Lambda, you can activate it in the Lambda console for a selected function or enable the service in the AWS SAM template:

Resources: GetLocations: Type: AWS::Serverless::Function Properties: Tracing: Active

You must also provide permission, which can be granted using the AWSXRayDaemonWriteAccess managed policy. For each service in your workload, you must also activate X-Ray on the workload’s resources. Once enabled, X-Ray starts collecting tracing data for events. You can instrument calls via the AWS SDK by wrapping the object instantiation with the AWS X-Ray SDK. In Node.js:

const AWSXRay = require('aws-xray-sdk-core') const AWS = AWSXRay.captureAWS(require('aws-sdk'))

This provides additional subsegment granularity and generates additional information captured for calls to DynamoDB, S3, and SQS. By tracing a request from the API endpoint through to the Lambda function and resulting service interactions, you can isolate which part of the system is causing the most latency using the X-Ray service map. X-Ray tracing can be combined with load testing to identify bottlenecks and throttled or exhausted resources.

To learn about configuring X-Ray for a serverless application, see https://docs.aws.amazon.com/xray/latest/devguide/xray-services-lambda.html.