Using AWS's API Gateway I configured an api resource as an AWS proxy to Kinesis' PutRecords
action. The API consumers send me a list of events I need to forward those events to a Kinesis stream.
The format in which they send the data to the API gateway look something similar to this. It contains 1 top level element that's of type Array. The object type of each array item is a JSON document:
{
"events":[
{
"time":"2017-01-01T11:43:21",
"type":"ItemSelected",
"application":"iOS Build 3654"
},{
"time":"2017-01-01:11:55:32",
"type":"ItemSelected",
"application":"iOS Build 3654"
}
]
}
What's needed is to break each separate event into a Kinesis record and send it to as a base64Encoded
string to Kinesis.
Using a Body Mapping Template I've configured the following.
{
"StreamName":"MemberApiAuditLog",
"Records":[
#foreach($elem in $input.path('$.events')){
#set($countVal=$foreach.count-1)
"Data":"$util.base64Encode($input.json('$.events[$countVal]'))",
"PartitionKey":"$input.path('$.memberid')"
}
#end
]
}
The problem I'm having is that the Mapping Template doesn't seem to have an issue with this the $countVal
variable in this code: '$.events[$countVal]'
. It somehow just doesn't recognize the $countVal
. If I replace $countVal
with 0
, it works just fine.
I need to use $input.json(x)
because the mapping template doesn't provide a different way to stringify a json object.
Questions:
- Other than
$input.json(x)
is there a way to stringify a json object in a Body Mapping Template?- I've tried JSON.stringify(object), but that didn't work.
- How can I get the code to recognize the value of
countVal
in that expression? If that can be resolved, the issue will be solved.