6

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

1

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed using Lambda I was able to put the item without mapping it using put instead of putitem if I remember well. However I would really like to avoid lambda just because of a mapping issue, especially since we want to handle a large number of entries and we would like to avoid triggering as many lambda processes to run
@Seb did you ever manage to get a solution to this? I have a similar situation where we're just trying to use DynamoDB as a storage area for the raw data, before it gets processed elsewhere. I'm trying to avoid lambda for that first step.
-1

You can enable CloudWatch log to verify the payload after transformation is expected. You are also able to use the test invoke feature from AWS API Gateway console to find out how your mapping works.

Here is the blog for using Amazon API Gateway as a proxy for DynamoDB. https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.