Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Acces variable in JSONPath API Gateway Body Mapping

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:

  1. Other than $input.json(x) is there a way to stringify a json object in a Body Mapping Template?
    1. I've tried JSON.stringify(object), but that didn't work.
  2. 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.
like image 381
JD Stuart Avatar asked May 26 '16 09:05

JD Stuart


People also ask

What is stage variable in API gateway?

Stage variables are name-value pairs that you can define as configuration attributes associated with a deployment stage of a REST API. They act like environment variables and can be used in your API setup and mapping templates.

How do I pass headers in API gateway?

To pass custom headers from an API Gateway API to a Lambda function, use a body mapping template. The API sends the updated API request to a Lambda function to process the headers. Then, the Lambda function returns one or more header values from the original API request.

How do I use a mapping template in API gateway?

You must define the model in order to have API Gateway to generate a SDK or to enable basic request validation for your API. You don't have to define any model to create a mapping template. However, a model can help you create a template because API Gateway will generate a template blueprint based on a provided model.


1 Answers

I've manage to make the template work, creating a valid json object.

{
    "streamName": "MemberApiAuditLog",
    "Records": [
        #set($inputRoot = $input.path('$.events'))
        #foreach($elem in $inputRoot) {
           #set($json = $input.json("$[$foreach.index]"))
            "Data":"$util.base64Encode($json)",
        #end
    ]
}

It works for me, hope for you too

like image 107
kanedaki Avatar answered Sep 26 '22 01:09

kanedaki