Skip to content

Instantly share code, notes, and snippets.

@mattgillard
Last active April 2, 2023 00:22
Show Gist options
  • Save mattgillard/c0b5f28e3254b52d484e3767515f9e6a to your computer and use it in GitHub Desktop.
Save mattgillard/c0b5f28e3254b52d484e3767515f9e6a to your computer and use it in GitHub Desktop.
AWS API Gateway VTL for flattening array to SendMessageBatch
## Input is like:
## curl --request POST "https://xxx.execute-api.ap-southeast-2.amazonaws.com/Prod/resource?MessageGroupId=1114" -H "Content-Type: application/json" --data-raw "{\"mykey\":[\"A\",\"B\"]}"
##
## If you get this error:
## {
## "Error": {
## "Code": "AccessDenied",
## "Message": "Access to the resource https://sqs.ap-southeast-2.amazonaws.com/123456789012/myqueue.fifo is denied.",
## "Type": "Sender"
## },
## "RequestId": "68e6bd7a-xxx"
##}
## You have a problem with your payload (make sure it begins with Action=SendMessageBatch). Make sure what you are sending looks something like this in the API gw logs:
## Action=SendMessageBatch&SendMessageBatchRequestEntry.1.Id=test_msg_1&SendMessageBatchRequestEntry.1.MessageBody=A&SendMessageBatchRequestEntry.1.MessageGroupId=1119&SendMessageBatchRequestEntry.2.Id=test_msg_2&SendMessageBatchRequestEntry.2.MessageBody=B&SendMessageBatchRequestEntry.2.MessageGroupId=1119
##
#set($finalUrl = "Action=SendMessageBatch")
#set($data = $input.path('$.mykey'))
#foreach ($item in $data)
#set($finalUrl = $finalUrl + "&SendMessageBatchRequestEntry." + $foreach.count + ".Id=test_msg_" + $foreach.count + "&SendMessageBatchRequestEntry." + $foreach.count + ".MessageBody=$util.urlEncode($item)&SendMessageBatchRequestEntry." + $foreach.count + ".MessageGroupId=" + $input.params('MessageGroupId'))
#end
$finalUrl
## Alternately use SendMessage (see below)
##Action=SendMessage&MessageBody=$util.urlEncode($input.body)&MessageGroupId=$input.params('MessageGroupId')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment