简体   繁体   中英

How to log X-Amzn-Request-id when invoking a lambda? (aws-sdk)

I see that when we invoke a lambda, there is an x-amzn-request-id for that execution:

  "resourceType": "lambda",
  "resource": "invoke",
  "output": {
    "ExecutedVersion": "$LATEST",
    "Payload": {
      "recordsReady": false
    },
    "SdkHttpMetadata": {
      "AllHttpHeaders": {
        "X-Amz-Executed-Version": [
          "$LATEST"
        ],
        "x-amzn-Remapped-Content-Length": [
          "0"
        ],
        "Connection": [
          "keep-alive"
        ],
        "x-amzn-RequestId": [ <-------------------
          "0b1198a6-2ed8-485b-b5f6-6c086ff192a1"
        ],

How would we log out that request id from the lambda using aws-sdk? Is it even possible?

context object has a couple of useful properties. One of the mis 'aws_request_id' (for python, but other sdks have it too): https://docs.aws.amazon.com/lambda/latest/dg/python-context.html That's supposed to give you the request id for the invocation request. I would check that and log it out. Eg:

def lambda_handler(event, context):
    print(context.aws_request_id)

you could do this in JS either by

  • using context object, which is the most common way. context is passed as the second argument in the handler function:
    exports.handler = (event, context, callback) => {
        console.log(`X-Amzn-Request-Id: ${context.awsRequestId}`);
        ...
    }
  • using process.env
exports.handler = (event, context, callback) => {
    console.log("X-Amzn-Request-Id: " + process.env.AWS_REQUEST_ID);
    // rest of your code
}

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-2025 STACKOOM.COM