How to map an API response from DynamoDB?

0

Hello community! I'm in the middle of something, trying to figure out something really simple. Hope you can help.

I'm creating a PutItem REST API on API Gateway. My goal is to allow the client to add a record in Dynamo directly, no LAMBDA, no middleware. Jusn from gateway to DynamoDB

The integration request works just fine, my records are being added to the table. However, my problem is that I want the API to respond something like this:

{
 "Status": "Record Added",
  "Data": [
    {
      "customerId": "Test123",
      "first_name": "Lukein",
      "added_date": "16/04/1990"
    }
  ]
}

I've tried several workarounds to map the recently added record to the table into the response but I've failed.

The closer I've been is by using this VTL:

#set($response = {
    "Status": "Record Added",
    "Data": [
        {
            "customerId": "$input.path('$.customerId')",
            "first_name": "$input.path('$.first_name')",
             "added_date": "$input.path('$.added_date')"
        }
    ]
})

$response

However, my response returns empty values:

"Status": "Record Added",
    "Data": [
        {
            "customerId": "",
            "first_name": "",
             "added_date": ""
        }

I appreciate your help with this

Thank you

2 Answers
0

Hi - Have you checked this blog https://aws.amazon.com/blogs/compute/using-amazon-api-gateway-as-a-proxy-for-dynamodb/. It has similar use which you are trying to build.

Also this "API Gateway REST API to DynamoDB" https://serverlessland.com/patterns/apigw-dynamodb would also help a lot.

profile pictureAWS
EXPERT
answered 10 months ago
  • Thank you. But in the example in the blogpost does no approach this from a POST request, it does it from a GET request where they retrieve the data from the DB Items. Since my POST request is not retrieving but it is creating records, I need to a way to map in the response those items that were added.

0

You are trying to take the DynamoDB response from a PutItem API call and return meaningful data in the API Gateway API Response. The problem is the data you want to return is not in the direct integration response from DynamoDB. This link shows the details around PutItem and the response. https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html

As explained in that document, the PutItem API call is limited in what it can return. The ReturnValues section lists two options: "NONE" (no return values) & "ALL_OLD" (data that was overwritten). Other API calls support options like "ALL_NEW" and "UPDATED_NEW" which might work for your use case.

If you use UpdateItem in place of PutItem and then include "ReturnValues": "ALL_NEW" or "ReturnValues": "UPDATE_NEW" in your integration request, the response will contain either the new or updated item attributes in the "body-json" as "Attributes". https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html

profile picture
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions