I have been using AWS_PROXY
integration from API Gateway to trigger AWS Lambda functions many times and in the event I always receive 'headers'
along with 'body'
and many other parameters.
Now I try to use the Integration Subtype EventBridge-PutEvents
like this.
Type: AWS::ApiGatewayV2::Integration
Properties:
...
IntegrationType: AWS_PROXY
IntegrationSubtype: EventBridge-PutEvents
RequestParameters:
Source: SOME_TEXT
DetailType: SOME_TEXT
Detail: $request.body
It works, but if I try to include in Detail
not just the body, but the headers as well it cries with:
"Invalid source: $request.headers specified for destination: Detail"
How can I pass/map the request.headers
to the EventBridge message (Detail) without creating a custom Lambda in between API Gateway and EventBridge myself?
2
Answers
To my understanding, AWS API Gateway’s EventBridge integration does not directly support passing request headers as part of the
Detail
field. TheDetail
field is primarily meant to pass the request payload (body) as the detail of the EventBridge event.If you want to include both the request headers and body in the EventBridge event without creating a custom Lambda function, you have a few options:
Use a Lambda Proxy Integration: Instead of using the
IntegrationSubtype
: EventBridge-PutEvents, you can use a regularAWS_PROXY
integration that points to a Lambda function. In this Lambda function, you can access the request headers and body and then manually put the event on the EventBridge bus using the AWS SDK with both headers and body included in the event’s detail.Use a Custom EventBridge Event Transformer: If you want to avoid writing a custom Lambda function, you can consider using AWS services like AWS AppFlow or AWS DataBrew to preprocess the events before they are sent to EventBridge. These services allow you to transform and manipulate event data before sending it to its destination.
Create a Lambda function with EventBridge-PutEvents Integration: While you mentioned that you want to avoid creating a custom Lambda function, this option is worth considering as it can be the most flexible and customizable solution. You can create a simple Lambda function that receives the API Gateway request, extracts the necessary data from the headers and body, and then puts a custom EventBridge event with the desired structure.
You can definitely do that.
From the documentation: https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEventsRequestEntry.html
The trick is to apply a mapping so that you combine header and body values into the
Details
field using a request template. Here is a recent example I did using CDK:Now EventBridge both has access to the request body through
RequestBody
and the specific headerSomeRequestHeader
throughSomeRequestHeader
.