user6189315
user6189315

Reputation:

AWS API Gateway & Kinesis Firehose Integration: Adding Additonal data

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
}

Upvotes: 4

Views: 3498

Answers (1)

user6189315
user6189315

Reputation:

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.

Upvotes: 9

Related Questions