[英]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:我看到当我们调用 lambda 时,该执行有一个 x-amzn-request-id:
"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?我们如何使用 aws-sdk 从 lambda 注销该请求 ID? Is it even possible?
有可能吗?
context object has a couple of useful properties. context object 有几个有用的属性。 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.
其中一个 mis 'aws_request_id'(对于 python,但其他 sdk 也有): https://docs.aws.amazon.com/lambda/latest/dg/python-context.html这应该给你请求 id对于调用请求。 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您可以通过以下方式在 JS 中执行此操作
context
object, which is the most common way.context
object,这是最常见的方式。 context
is passed as the second argument in the handler function: context
作为处理程序 function 中的第二个参数传递: exports.handler = (event, context, callback) => {
console.log(`X-Amzn-Request-Id: ${context.awsRequestId}`);
...
}
process.env
process.env
exports.handler = (event, context, callback) => {
console.log("X-Amzn-Request-Id: " + process.env.AWS_REQUEST_ID);
// rest of your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.