skip to Main Content

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


  1. To my understanding, AWS API Gateway’s EventBridge integration does not directly support passing request headers as part of the Detail field. The Detail 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:

    1. Use a Lambda Proxy Integration: Instead of using the IntegrationSubtype: EventBridge-PutEvents, you can use a regular AWS_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.

    2. 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.

    3. 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.

    Login or Signup to reply.
  2. You can definitely do that.
    From the documentation: https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEventsRequestEntry.html

    Detail:
    A valid JSON object. There is no other schema imposed. The JSON object may contain fields and nested sub-objects.

    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:

    request_templates={
                    'application/json': """
                                            #set($context.requestOverride.header.X-Amz-Target ="AWSEvents.PutEvents")
                                            #set($context.requestOverride.header.Content-Type ="application/x-amz-json-1.1")
                                            {"Entries": 
                                                [
                                                    {"DetailType": "putEvent",
                                                    "Detail": "{"RequestBody": $util.escapeJavaScript($input.json('$')),
                                                                "SomeRequestHeader": "$input.params('SomeRequestHeader')"}", 
                                                    "Source": "async-eventbridge-api",
                                                    "EventBusName": "%s"
                                                    }
                                                ]
                                            }
                                        """ % event_bus.event_bus_names}
    

    Now EventBridge both has access to the request body through RequestBody and the specific header SomeRequestHeader through SomeRequestHeader.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search