简体   繁体   中英

How to trace a lambda invokes a lambda with AWS X-Ray?

I have an AWS Lambda function which is triggered by an API Gateway event. The API Gateway is configured to use X-Ray.

As the Lambda tracing configuration defaults to PassTrough it is also shown in X-Ray (service map, etc.).

The invoked Lambda uses the node.js aws-sdk to invoke another lambda. If I understand correctly the Tracing ID has to be passed on to the next Invocation in order to show this Lambda also in X-Ray. In the API of the SDK I found no option for this.

const result = await lambda
        .invoke(lambdaParamsCreateUser)
        .promise()

How can I achieve this? How can I trace also the invocation of the original request?

With the tips of @Balu Vyamajala I changed the AWS-SDK import to the following:

import AWS from "aws-sdk";

import AwsXRay from "aws-xray-sdk-core";
const aws = AwsXRay.captureAWS(AWS);
export default aws;

I use it when I invoice my second function like this:

import AWS from "aws";
const Lambda = AWS.Lambda;
// ...
const lambda = new Lambda({ region: "eu-central-1" });
const lambdaPromise = lambda
        .invoke({
            FunctionName: AUTH_CREATE_USER_FUNC,
            InvocationType: "RequestResponse",
            Qualifier: AUTH_CREATE_USER_FUNC_VERSION,
            Payload: JSON.stringify({
                eMail: eMail,
                device: device,
                customerId: customerId,
            }),
            LogType: "Tail",
        })
        .promise()

But in X-Ray there is no invocation chain:-(

https://imgur.com/wDMlNzb

Do I make a mistake?

if we enable X-Ray for both Lambda functions, trace-id is automatically passed and will be same for both Lambdas.

In the code, we can enable X-Ray simply by wrapping it around aws-sdk

JavaScript:

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

Typescript:

import AWSXRay from 'aws-xray-sdk';
import aws from 'aws-sdk';
const AWS = AWSXRay.captureAWS(aws)

Here is a sample test to confirm.

balu-test >> sample-test

Lambda 1 (balu-test):

const AWSXRay = require("aws-xray-sdk-core");
const AWS = AWSXRay.captureAWS(require("aws-sdk"));    
const lambda = new AWS.Lambda();

exports.handler = async function (event, context) {
  var params = {
    FunctionName: "sample-test",
    InvocationType: "RequestResponse",
    Payload: '{ "name" : "foo" }',
  };
  
  const response = await lambda.invoke(params).promise();
  console.log('response',response);

  return "sucess";
};

Lambda 2(sample-test):

const AWSXRay = require("aws-xray-sdk-core");
const AWS = AWSXRay.captureAWS(require("aws-sdk"));    
let region = "us-east-1"
let secretName = "SomeSecret"
let secret
let decodedBinarySecret    
var client = new AWS.SecretsManager({
  region: region,
});

exports.handler = (event, context, callback) => {
  client.getSecretValue({ SecretId: secretName }, function (err, data) {
    if (err) {
      callback(err);
    } else {
      if ("SecretString" in data) {
        secret = data.SecretString;
      } else {
        let buff = new Buffer(data.SecretBinary, "base64");
        decodedBinarySecret = buff.toString("ascii");
      }
      callback(null, secret);
    }
  });
};

TraceId is same and X-Ray points to same graph for both Lambda invocations. Same thing happens when first api is called from Api-Gateway. First time trace-id is generated and is passed along as http header to downstream processes.

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM