Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS API Gateway store JSON to DynamoDB

I have an issue trying to use API Gateway as a proxy to DynamoDB.

Basically it works great if I know the structure of the data I want to store but I cannot manage to make it dynamic regardless of the payload structure.

There are many websites explaining how to use API Gateway as a proxy to DynamoDB. None that I found explains how to store a JSON object though.

Basically I send this JSON to my API endpoint:

{
    "entryId":"abc",
    "data":{
        "key1":"123",
        "key2":123
    }
}

If I map using the following template, the data gets put in my database properly

{ 
    "TableName": "Events",
    "Item": {
        "entryId": {
            "S": "abc"
        },
        "data": {
            "M": {
              "key1": {
                "S": "123"
              },
              "key2": {
                "N": "123"
              }
            }
        }
    }
}

However, I don't know the structure of "data" hence why I want the mapping to be dynamic, or even better, I would like to avoid any mapping at all.

I managed to make it dynamic but all my entries are of type String now:

      "data": { "M" : {
      #foreach($key in $input.path('$.data').keySet())
      "$key" : {"S": "$input.path('$.data').get($key)"}#if($foreach.hasNext),#end
      #end }
              }

Is it possible to get the type dynamically? I am not quite sure how API Gateway mapping works yet.

Thank you for you help.

Seb

like image 218
Seb Avatar asked Oct 31 '22 04:10

Seb


1 Answers

You aren't going to avoid some sort of mapping when inserting into Dynamodb. I would recommend using a Lambda function instead of a service proxy to give you more control and flexibility in mapping the data to your Dynamodb schema.

like image 173
Mark B Avatar answered Nov 11 '22 18:11

Mark B