I am currently using the following mapping template to pass data sent to an AWS API Gateway endpoint to AWS Kinesis Firehose stream:
{
    "DeliveryStreamName": "[STREAMNAME]",
    "Record": {
        "Data": "$util.base64Encode($input.body)"
    }
}
What I would like to do is: adding information to the $input.body that is being encoded like the $context.identity.sourceIp of the client making the request.
How can I go about this when the output being passed to Kinesis Firehose needs to be Base64-encoded? Ideally I would like my data that is being posted to Kinesis Firehose look like this:
{
  "x": 1,
  "y": 2,
  "z": 3,
  ...,                   // all the properties from the JSON-request by the client
  "clientIp": "x.x.x.x"  // property added by API-Gateway into client's object
}
                After a little bit more digging I managed to get the following to work:
#set($inputRoot = $input.path('$'))
#set($data =  "{
  #foreach($key in $inputRoot.keySet())
  ""$key"": $input.json($key),
  #end
  ""clientIP"": ""$context.identity.sourceIp"",
}")
{
    "DeliveryStreamName": "[STREAMNAME]",
    "Record": {
        "Data": "$util.base64Encode($data)"
    }
}
I was not aware that you could do a #foreach inside a #set. Note that you also have to use double-quotes to get this right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With