AWS Lambda
Developer Guide

Instrumenting Node.js Code in AWS Lambda

In Node.js, you can have Lambda emit subsegments to X-Ray to show you information about downstream calls to other AWS services made by your function. To do so, you first need to include the the AWS X-Ray SDK for Node.js in your deployment package. In addition, wrap your AWS SDK require statement in the following manner:

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

Then, use the AWS variable defined in the preceding example to initialize any service client that you want to trace with X-Ray, for example:

s3Client = AWS.S3();

After following these steps, any call made from your function using s3Client results in an X-Ray subsegment that represents that call. As an example, you can run the Node.js function following to see how the trace looks in X-Ray:

Example index.js

var AWSXRay = require('aws-xray-sdk-core'); var AWS = AWSXRay.captureAWS(require('aws-sdk')); var s3 = new AWS.S3(); exports.handler = (event, context, callback) => { var params = {Bucket: process.env.BUCKET_NAME, Key: process.env.BUCKET_KEY, Body: process.env.BODY}; s3.putObject(params, function(err, data) { if (err) { console.log(err) } else { console.log('success!') } }); };

Following is what a trace emitted by the code preceding looks like (asynchronous invocation):