We have an AWS API Gateway endpoint who's only purpose is to create a message in an SQS topic that is ultimately handled by a Lambda function. So:
API Gateway Endpoint Handler -> SQS Topic Message -> SQS Lambda Handler
We'd like to start correlating activity between the API Gateway endpoint handler and the Lambda by logging the context.aws_request_id coming from the API Gateway Handler at the SQS Lambda Handler. The problem is how to best pass the API Gateway's aws_request_id to the SQS Lambda?
We have one solution where we are passing the aws_request_id as part of the MessageBody when we call the sqs send_message function:
msg_body = [some data]
msg_body['aws_request_id'] = context.aws_request_id
response = queue.send_message(MessageBody=msg_body, MessageGroupId=msg_group_id, MessageDeduplicationId=msg_deduplication_id)
Finally in the SQS Lambda handler we can retrieve the gateway's request_id:
def my_sqs_handler(event, context):
records = event.get('Records')
for record in records:
log.info('Processing SQS message: %s', record['messageId'])
body = json.loads(record.get('body'))
apigw_request_id = json.loads(record.get('aws_request_id'))
log.info('API Gateway request-id: %s', apigw_request_id)
...
This works, but doesn't seem clean as the request_id is being embedded inside the sqs message body. It seems as though there should be a cleaner mechanism for getting this via the event or context objects being passed to the sqs lambda handler. Has anyone managed to find a way of embedding extra 'event' or 'context' information at the API Gateway handler, and then ultimately read by an SQS lambda handler?
Thanks
CodePudding user response:
This is pretty much a textbook use case for Request Transformations on REST apis. Basically you can apply a template transform to the incoming request that could, for instance, add a key to the message body that includes the header values so they continue to make it it through the sqs to the lambda
See https://docs.aws.amazon.com/apigateway/latest/developerguide/rest-api-data-transformations.html for more info