我是否可以获取有关AWS Lambda请求ID的信息,例如触发?

时间:2018-02-16 10:21:11

标签: aws-lambda amazon-cloudwatch

在我的日志中找到

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

每次调用AWS lambda。是否有可能获得有关请求的更多信息,例如什么引发了它?

3 个答案:

答案 0 :(得分:0)

我不确定{临时{1}}是什么意思?如果它连接到API网关,则通过网关将请求代理到您的Lambda。在这种情况下,即使是代理请求,网关也会触发它。

此外,您可以扩充您的Gateway以将其他请求信息传递给您的Lambda。 See how

另外,要将请求详细信息附加到您看到的RequestID,您可以查看what triggered it

我假设您想要进行某种形式的日志监控,在这种情况下,我认为您可以捆绑这些请求信息(标头,查询,参数,正文)并与awsRequestId一起发送到您的日志聚合器。 如果有帮助,请告诉我

Further reference

答案 1 :(得分:0)

lambda函数有两个参数:Event和context

正常调用如下所示:

{
    "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>"
}

测试调用包含您获得的元素。

答案 2 :(得分:0)

您可以从上下文对象中获取请求ID 和其他信息。

Lambda运行函数时,它将上下文对象传递给处理程序。该对象提供方法和属性,这些方法和属性提供有关调用,函数和执行环境的信息。 https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html

您可以从 process.env 变量中获取更多信息。

我编写了一个简短的lambda函数(node.js),该函数将这些信息记录到控制台,并最终在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'};
};

日志输出:

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