Cant get API Gateway `RequestId` in Lambda function

馋奶兔 提交于 2019-12-11 15:16:04

问题


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

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}

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?


回答1:


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



来源:https://stackoverflow.com/questions/51105990/cant-get-api-gateway-requestid-in-lambda-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!