Skip to content

Instantly share code, notes, and snippets.

@avendasora
Last active September 20, 2022 05:00
Show Gist options
  • Select an option

  • Save avendasora/1c77287ef4552d5c6fdc3907c9081835 to your computer and use it in GitHub Desktop.

Select an option

Save avendasora/1c77287ef4552d5c6fdc3907c9081835 to your computer and use it in GitHub Desktop.
AWS API Gateway body mapping template to convert "application/x-www-form-urlencoded" into "application/json" for DynamoDB
#set ($keyValues = {})
#set($prarametersString = $input.body)
#set($parameters = $prarametersString.split("&"))
#foreach($parameter in $parameters)
#set($keyValue = $parameter.split("="))
$keyValues.put($util.urlDecode($keyValue[0]),$util.urlDecode($keyValue[1]))
#end
{
"TableName": "table_name",
"Item": {
"request_id": {
"S": "$context.requestId"
},
#foreach($key in $keyValues.keySet())
#set($value = $keyValues.get($key))
#set($class = $value.class)
##$class
#if($class == "class java.lang.Integer"
|| $class == "class java.lang.Double"
|| $class == "class java.lang.Float"
|| $class == "class java.lang.BigDecimal"
|| $class == "class java.lang.BigInteger"
)
#set($type = "N")
#elseif($class == "class java.lang.Boolean")
#set($type = "BOOL")
#else
#set($type = "S")
#end
"$key": {
"$type": "$value"
}#if($foreach.hasNext),#end
#end
}
}
@srbentley65

Copy link
Copy Markdown

This works great! I'm new to DynamoDB/Lambda. Could you tell me how one would use this output to insert the item into the table from Lambda?

{'TableName': 'LogTable', 'Item': {'request_id': {'S': '2784d15a-328d-4f4f-a78f-bc167abb58b7'}, 'ErrorCode': {'S': '21610'}, 'SmsSid': {'S': 'SM9f63e7fba09f4f42bd58fb2d8d616218'},
'SmsStatus': {'S': 'failed'}, 'MessageStatus': {'S': 'failed'}, 'To': {'S': '+17702456616'}, 'MessagingServiceSid': {'S': 'MG820856a3362280c32f5635b0f6527503'},
'MessageSid': {'S': 'SM9f63e7fba09f4f42bd58fb2d8d616218'}, 'AccountSid': {'S': 'ACc5211e9f2f87ac208bf23b46e355334a'}, 'From': {'S': '+17226704377'}, 'ApiVersion': {'S': '2010-04-01'}}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment