无法在Lambda函数中获取API网关`RequestId`

时间:2018-06-29 16:28:07

标签: python amazon-web-services aws-lambda aws-api-gateway

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

{"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):

{'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替换它吗?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您可以使用“事件”轻松获取 API 网关的 requestId。

 def lambda_handler(event, context):
   request = event["requestContext"]
   request_id = request["requestId"]