How to include context variables in AWS API-EventBridge Integration Detail body


I have an ApiGatewayv2 EventBridge-PutEvents Integration running. The POST body is sent into event bridge as the event Detail.

The Integration request parameters are as below:

    'Source': '',
    'DetailType': 'ApiEvent',
    'Detail': $request.body,
    'EventBusName': BUS_NAME,

If I post {"foo": "bar"} to the API endpoint, I end up with an event in my bus with {"foo": "bar"} as the Detail. So far, all straightforward.

I have also enabled an authorizer on the API GW and I want to pass the context from that authorizer into the event Detail as well.

I'm testing with an IAM authorizer for now, but would like to use Cognito.

I can update the request parameters to change the DetailType to $context.identity.caller and I get my Access Key in the DetailType of the Event.

This isn't what I want, but it shows that I can access these context variables in my integration.

What I want is for that to be in the Detail though - not the DetailType.

So, when I POST {"foo": "bar"} to my API GW I get an event with Detail:

    "body": {"foo":, "bar"},
    "auth": {"user": "AAA1111ACCESSKEY"}

But, I can't use anything other than $request.body as the Detail value in the Integration's request parameters.

If I use a Detail like {"body": $request.body} I get an error on saving the integration - Invalid selection expression specified: Validation Result: warnings : [], errors : [Invalid source: {"body": $request.body} specified for destination: Detail]

And, I've tried that with stringified {"body": $request.body} as well.

How can I wrap the POST data in a key like "body" within the event's Detail, and add key/values from the context variables I get from the authorizer.

Or, some other way to inject the authorizer context into my Event. Should I just give up and use a lambda to access the ApiGw context and run PutEvent? Seems defeatist!

1 Answer

I have the same issue. Can't use $request.body in the detail only properties of the the body. Seems like a bug and very frustrating.

This works:

{"var1": "${request.header.H1}", "tid": "${request.header.H2}", "body":"${request.body.myvar}"} But this doesn't:

{"var1": "${request.header.H1}", "tid": "${request.header.H2}", "body":"${request.body}"}

or this:

{"var1": "${request.header.H1}", "tid": "${request.header.H2}", "body":$request.body"}

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions