Skip to content

API Gateway Mapping Template to Kinesis Firehose – Unable to Include Header in Records, 500 InternalServerError

0

Hi AWS team,

I’m trying to send POST requests from API Gateway to a Kinesis Firehose delivery stream. The request body is a JSON array, and I also need to include a request header (source-system) in each Firehose record.

Here’s what I currently have working without the header:

{
  "DeliveryStreamName": "<delivery_stream_name>",
  "Records": [
    #foreach($elem in $input.path('$'))
        {
          #set($jsonPath = "$[$foreach.index]")
          "Data": "$util.base64Encode($input.json($jsonPath))"
        }#if($foreach.hasNext),#end
      #end
  ]
}

This works fine, each array element is sent to Firehose successfully.

However, when I try to add the header, like this:

 {
  "DeliveryStreamName": "<delivery_stream_name>",
  "Records": [
    #foreach($elem in $input.path('$'))
      {
        "Data": "$util.base64Encode(
          {
            \"source-system\": \"$input.params('source-system')\",
            \"event\": $input.json('$[$foreach.index]')
          }
        )"
      }#if($foreach.hasNext),#end
    #end
  ]
}

I get a 500 InternalServerError with logs: Execution failed due to configuration error: Unable to transform request

Request details:
HTTP Method: POST
Request body: JSON array of objects (see below)
Header: source-system: RM
Content type: application/json
Example request body:
[
  {
    "m_event": "e_KI",
    "m_time": 1754611200000,
    "a_name": "Mobile"
  }
]

Questions: How can I modify the mapping template so that each Firehose record contains both the event object and the source-system header?

Are there any common pitfalls in JSON formatting or escaping in mapping templates that could cause this 500 error?

Any guidance or example templates would be greatly appreciated.

Thanks, Yash

2 Answers
0

hi,

to add the header value (source-system) to each record in your mapping template, the mapping template transformation fails.

can you share the CloudWatch "Unable to transform request" for detailed transformation error?

best

answered a year ago

  • Hi,

    I am getting this error only.

    (7f5b4d90-069f-4a79-b7b1-630ec82ae1a0) Execution failed due to configuration error: Unable to transform request

0

hi,

means API Gateway couldn't parse your mapping template into valid JSON before sending to Firehose. Header value (source-system) gets injected into every record for firehose to see.

in you code with header--- raw JSON inside $util.base64Encode(...), but that function only accepts a string.

in you code :-

API Gateway fails transformation, hence Execution failed due to configuration error.

in API Gateway mapping templates - template for application/json.

try replacing header, like this: need to add the $util.toJson

{
  "DeliveryStreamName": "<delivery_stream_name>",
  "Records": [
    #foreach($elem in $input.path('$'))
      {
        "Data": "$util.base64Encode($util.toJson({
          \"source_system\": \"$input.params('source-system')\",
          \"event\": $elem
        }))"
      }#if($foreach.hasNext),#end
    #end
  ]
}

and than test API Gateway with header.

hope it helps, not sure if I was able to expressed it well. Best,

answered a year 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.