Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get information about AWS Lambda request IDs, e.g. the trigger?

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?

like image 811
Martin Thoma Avatar asked Feb 16 '18 10:02

Martin Thoma


1 Answers

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    
like image 158
RenRen Avatar answered Oct 03 '22 06:10

RenRen