16

In my logs I find

START RequestId: 123a1a12-1234-1234-1234-123456789012 Version: $LATEST

for every invocation of AWS lambda. Is it possible to get more information about a request, e.g. what triggered it?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

4 Answers4

15

You can get the request id and other information from the context object.

When Lambda runs your function, it passes a context object to the handler. This object provides methods and properties that provide information about the invocation, function, and execution environment. https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html

You can get further information from the process.env variable.

I wrote a short lambda function (node.js) which logs these information to the console and ends up in aws cloud watch

exports.handler = async (event, context) => {
    console.log('context:', JSON.stringify(context));
    console.log('process.env:', JSON.stringify(process.env));

    return {statusCode: 200, body: 'Hello World'};
};

Log output:

START RequestId: 6f3c103e-f0a5-4982-934c-dabedda0065e Version: $LATEST
context:
{
    "callbackWaitsForEmptyEventLoop": true,
    "functionVersion": "$LATEST",
    "functionName": "my_lambda_function_name",
    "memoryLimitInMB": "128",
    "logGroupName": "/aws/lambda/my_lambda_function_name",
    "logStreamName": "2020/10/01/[$LATEST]8adb13668eed4ac8b3e7f8d796fe4d49",
    "invokedFunctionArn": "arn:aws:lambda:us-east-1:636121343751:function:my_lambda_function_name",
    "awsRequestId": "6f3c103e-f0a5-4982-934c-dabedda0065e"
}
process.env:
{
    "AWS_LAMBDA_FUNCTION_VERSION": "$LATEST",
    "AWS_SESSION_TOKEN": "IZZ3eS6vFF...",
    "LD_LIBRARY_PATH": "/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib:/opt/lib",
    "LAMBDA_TASK_ROOT": "/var/task",
    "AWS_LAMBDA_LOG_GROUP_NAME": "/aws/lambda/my_lambda_function_name",
    "AWS_LAMBDA_RUNTIME_API": "127.0.0.1:9001",
    "AWS_LAMBDA_LOG_STREAM_NAME": "2020/10/01/[$LATEST]8adb13668eed4ac8b3e7f8d796fe4d49",
    "AWS_EXECUTION_ENV": "AWS_Lambda_nodejs12.x",
    "AWS_LAMBDA_FUNCTION_NAME": "my_lambda_function_name",
    "AWS_XRAY_DAEMON_ADDRESS": "169.254.79.2:2000",
    "PATH": "/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin",
    "AWS_DEFAULT_REGION": "us-east-1",
    "PWD": "/var/task",
    "AWS_SECRET_ACCESS_KEY": "5nQrpKhBwF...",
    "LAMBDA_RUNTIME_DIR": "/var/runtime",
    "LANG": "en_US.UTF-8",
    "NODE_PATH": "/opt/nodejs/node12/node_modules:/opt/nodejs/node_modules:/var/runtime/node_modules:/var/runtime:/var/task",
    "AWS_REGION": "us-east-1",
    "TZ": ":UTC",
    "AWS_ACCESS_KEY_ID": "ASIAZNANWY3473BTADSB",
    "SHLVL": "0",
    "_AWS_XRAY_DAEMON_ADDRESS": "169.254.79.2",
    "_AWS_XRAY_DAEMON_PORT": "2000",
    "AWS_XRAY_CONTEXT_MISSING": "LOG_ERROR",
    "_HANDLER": "index.handler",
    "AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "128",
    "_X_AMZN_TRACE_ID": "Root=1-6f75f4e5-5801599f17fd63e74ecd0833;Parent=73fb93812cdbf83f;Sampled=0"
}
END RequestId: 6f3c103e-f0a5-4982-934c-dabedda0065e
REPORT RequestId: 6f3c103e-f0a5-4982-934c-dabedda0065e
Duration: 21.71 ms  Billed Duration: 100 ms 
Memory Size: 128 MB Max Memory Used: 64 MB  Init Duration: 132.35 ms    
RenRen
  • 10,550
  • 4
  • 37
  • 39
4

You need to get this information from your lambda function context .

    RequestId=context.aws_request_id

You will get following result RequestId- "00cfaafe-5018-4be0-9668-b98daf5a7312" Version: $LATEST

vaquar khan
  • 10,864
  • 5
  • 72
  • 96
3

Not sure I understand what you mean by what triggered it? If it's connected to the API Gateway, then requests are proxied through the Gateway to your Lambda. In that case the Gateway triggered it, even though it was a proxied request.

Additionally and you can augment your Gateway to pass additional request info to your Lambda. See how

And also, to attach the request details to the RequestID you're seeing, you can check the context.awsRequestId.

I'd assume you want to do some form of log monitoring, in which case I think you can bundle up these request information(headers, query, params, body) and send to your log aggregator along with the awsRequestId. Let me know if that helps

Further reference

oreoluwa
  • 5,553
  • 2
  • 20
  • 27
  • I want to know it it was triggered by me clicking on "test" or by something else. – Martin Thoma Feb 16 '18 at 12:39
  • in that case I think my response should be able to answer that. I mean you can log the requestId and crosscheck that with what you have in the cloudwatch logs, unless you want to augment what's in Cloudwatch. Moreover, clicking on "test" is a delibrate action, so you can just easily check the logged requestID and compare with the records on Cloudwatch – oreoluwa Feb 16 '18 at 13:28
  • Just don't click "Test" at all. That's method of testing is very easily misunderstood. (Not your fault. It's AWS' bad UI/UX). – Noel Llevares Feb 18 '18 at 15:57
  • 1
    When invoked from API Gateway, the context object contains a requestId property that is the API Gateway request ID. This does not match the Lambda request ID that Lambda writes to the CloudWatch logs. API Gateway returns its own request ID in the response headers, but this is not useable to find the corresponding Lambda logs for that request. I would like to return the Lambda request ID in the response headers from my application. – Andrew Jul 08 '20 at 05:21
3

A lambda function has two parameters: Event and context.

A normal invocation looks like this:

{
    "event": {
        "version": "0",
        "id": "abcdefgh-1234-5678-1234-abcdefghijkl",
        "detail-type": "Scheduled Event",
        "source": "aws.events",
        "account": "123456789012",
        "time": "2018-01-01T12:00:00Z",
        "region": "us-east-1",
        "resources": [
            "arn:aws:events:us-east-1:123456788901:rule/foo"
        ],
        "detail": {}
    },
    "context": "<__main__.LambdaContext object at 0x123456ax1234>"
}

The test invocation has the elements you get.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 2
    My event invocations (calling lambda functions from appsync) do not have the `id` field. However, my context does have an `aws_request_id` field, which is that UUID that is displayed in the logs. re: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html – Mike Fogel Oct 25 '19 at 16:28
  • Hi @Martin Thoma, I have to get a message-id and request-id when we use SNS in lambda. how to verify message is send or not by using message and request-id – Vishal Sharma Nov 29 '19 at 21:56