1

I'm following along: Using Amazon API Gateway as a proxy for DynamoDB

Taking the blog's example, I want to tighten what and how data is written to a dynamodb table frontend by Api Gateway. To this end I define the following mapping in Api Gateway:

{ 
    "TableName": "Comments",
    "Item": {
      "commentId": {
        "S": "$context.requestId"
       },
       "pageId": {
         "S": "$input.path('$.pageId')"
       },
       "userName": {
         "S": "$input.path('$.userName')"
       },
       "message": {
         "S": "$input.path('$.message')"
       }
    }
}

Following along, testing with the following example works just fine:

{
  "pageId": "breaking-news-story-01-18-2016",
  "userName": "Just Saying Thank You",
  "message": "I really enjoyed this story!!"
}

However, let's say I want to keep the same mapping as above but want to make message optional. How would I do that? I can't get it to work. I've tried:

  1. using the above mapping as is, but sending a body without the message-attribute. --> "One or more parameter values were invalid: An AttributeValue may not contain an empty string" 2 using the above mapping as is, but sending a body with message=null. --> "One or more parameter values were invalid: An AttributeValue may not contain an empty string"
  2. changing above mapping by omitting the definition for mapping -> passing a body now without message succeeds obviously. However, sending a body with message doesn't pass message through (which is what I expected, but wanted to exhaust all options)
  3. Don't use a mapping at all. Obviously that works, but now everything is passed-through unfiltered, which is unwanted.

Obviously I could use AWS lambda instead to do the mapping but this feels like such a common use-case, i.e.: optional attributes, that this must be possible directly in Api Gateway.

2 Answers 2

2

Courtesy of @ka:

Explatation: APi gateway auto-populates non-existing attributes with empty strings or something. This needs to be excluded from the if.

Also notice the critical , right after the if statement.

{ 
    "TableName": "Comments",
    "Item": {
      "commentId": {
        "S": "$context.requestId"
       },
       "pageId": {
         "S": "$input.path('$.pageId')"
       },
       "userName": {
         "S": "$input.path('$.userName')"
       }#if($input.path('$.description') && $input.path('$.message') != ""),
       "message": {
         "S": "$input.path('$.message')"
       }
       #end
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Would you mind trying this mapping template? If the message is NULL, it will not be added to the body.

{ 
    "TableName": "Comments",
    "Item": {
      "commentId": {
        "S": "$context.requestId"
       },
       "pageId": {
         "S": "$input.path('$.pageId')"
       },
       "userName": {
         "S": "$input.path('$.userName')"
       }#if ($.message),
       "message": {
         "S": "$input.path('$.message')"
       }
       #end
    }
}

Thanks, -Ka Hou

2 Comments

Sadly that doesn't work: Execution failed due to configuration error: Unable to transform request
Got it to work. Thanks for pointing me in the right direction. See my answer

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.