无法在Lambda函数中获取API网关`RequestId` [英] Cant get API Gateway `RequestId` in Lambda function

查看:110
本文介绍了无法在Lambda函数中获取API网关`RequestId`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发了一条帖子,并从亚马逊API网关得到答复:

I sent a post and get the response from amazon API Gateway as:

{"MD5OfMessageBody": "bc709a65868c21309969a07cfeeac12f", "MD5OfMessageAttributes": "8a66ae91e9109cb75717520c287736eb", "MessageId": "b70a5453-5f30-4914-8006-b6cf7fa4ff93", "ResponseMetadata": {"RequestId": "1a38fbb5-859a-53f9-9551-a805d864768c", "HTTPStatusCode": 200, "HTTPHeaders": {"server": "Server", "date": "Fri, 29 Jun 2018 16:18:06 GMT", "content-type": "text/xml", "content-length": "459", "connection": "keep-alive", "x-amzn-requestid": "1a38fbb5-859a-53f9-9551-a805d864768c"}, "RetryAttempts": 0}}

当我查看传递给lambda的处理函数的事件对象时: def lambda_handler(event, context):

When I look in the event object passed into the handler function in lambda: def lambda_handler(event, context):

{'resource': '/python_job_to_sqs', 'path': '/python_job_to_sqs', 'httpMethod': 'POST', 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'CloudFront-Forwarded-Proto': 'https', 'CloudFront-Is-Desktop-Viewer': 'true', 'CloudFront-Is-Mobile-Viewer': 'false', 'CloudFront-Is-SmartTV-Viewer': 'false', 'CloudFront-Is-Tablet-Viewer': 'false', 'CloudFront-Viewer-Country': 'US', 'Content-Type': 'application/json', 'Host': '5p1xcqn2y5.execute-api.us-east-1.amazonaws.com', 'User-Agent': 'python-requests/2.14.2', 'Via': '1.1 c7db0c4b178dd73a64add79be10805c2.cloudfront.net (CloudFront)', 'X-Amz-Cf-Id': 'QkdNKg7VrrB6tJ0wynZwYvCxSYpLMTGkm9CK6CsYhw-jLDqTI0-x6g==', 'X-Amzn-Trace-Id': 'Root=1-5b365bbd-9dd143fc6325d468485f44a0', 'X-Forwarded-For': '38.77.16.218, 204.246.180.73', 'X-Forwarded-Port': '443', 'X-Forwarded-Proto': 'https'}, 'queryStringParameters': None, 'pathParameters': None, 'stageVariables': None, 'requestContext': {'resourceId': 'i42qmv', 'resourcePath': '/python_job_to_sqs', 'httpMethod': 'POST', 'extendedRequestId': 'JQNFqFZjoAMFf8g=', 'requestTime': '29/Jun/2018:16:18:05 +0000', 'path': '/production/python_job_to_sqs', 'accountId': '130491730454', 'protocol': 'HTTP/1.1', 'stage': 'production', 'requestTimeEpoch': 1530289085866, 'requestId': '012c013f-7bb8-11e8-bed9-1ff9514513cd', 'identity': {'cognitoIdentityPoolId': None, 'accountId': None, 'cognitoIdentityId': None, 'caller': None, 'sourceIp': '38.77.16.218', 'accessKey': None, 'cognitoAuthenticationType': None, 'cognitoAuthenticationProvider': None, 'userArn': None, 'userAgent': 'python-requests/2.14.2', 'user': None}, 'apiId': '5p1xcqn2y5'}, 'body': '{"input": [{"value": "test.co", "fieldName": "tld"}]}, 'isBase64Encoded': False}

在其他框架中,似乎requestContext['requestId'](这里是012c013f-7bb8-11e8-bed9-1ff9514513cd)似乎应该是从API网关传入的请求ID.但是012c013f-7bb8-11e8-bed9-1ff9514513cd != 1a38fbb5-859a-53f9-9551-a805d864768c.这是他们的框架中的一个错误,他们正在用lambda函数ID替换它吗?

It seems in other frameworks the requestContext['requestId'] which here is 012c013f-7bb8-11e8-bed9-1ff9514513cd is supposed to be the request ID passed in from the API Gateway. But 012c013f-7bb8-11e8-bed9-1ff9514513cd != 1a38fbb5-859a-53f9-9551-a805d864768c. Is this a bug in their framework that they're replacing this with the lambda function ID?

推荐答案

您可以使用映射模板将requestId传递给lambda函数

You can use mapping template to pass requestId to lambda function

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference

这篇关于无法在Lambda函数中获取API网关`RequestId`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆