0

When trying to set up an endpoint that receives data as application/x-www-form-urlencoded and starts executing a Step Function, the best mapping template I could come up with was:

#set($body = {
  "context": {
    "proxy": {
      "source_ip": "$context.identity.sourceIp",
      "http_method": "$context.httpMethod",
      "request_id": "$context.requestId",
      "url": "$context.domainName"
    }
  },
  "payload": {
    #foreach($token in $input.path('$').split('&'))
      #set($keyVal = $token.split('='))
      #set($key = $util.urlDecode($keyVal[0]))
      #set($val = $util.urlDecode($keyVal[1]))
      "$key": "$val"#if($foreach.hasNext),#end
    #end
  }
})
{
  "input": "$util.escapeJavaScript($body)",
  "stateMachineArn": "arn:aws:states:us-east-1:12345:stateMachine:name"
}

This seems to be the right approach, based on what I've researched, but it keeps failing to transform:

Execution failed due to configuration error: Unable to transform request

I've been playing with the mapping template for a few hours now, and I'm not sure what could be the problem. Without debugging tools, it's hard to pinpoint exactly what might be going wrong, although I did deploy multiple times, with different pieces of the template shared above.

It seems that there are two bugs:

  1. Looping through the body of the request in a variable (similar code can be found here).
  2. Trying to escape the body object. AWS's VTL documentation is lacking, and I'm not sure what I'm doing wrong here.

I'd appreciate any assistance, more specifically to help me figure out what exactly is breaking the transformation.

preceptor
  • 1
  • 2
  • I've also tried escaping the object manually, with no luck (`com.amazon.coral.service#SerializationException`). – preceptor Mar 03 '23 at 00:15

1 Answers1

0

If anyone searching online end up here, I ended up “fixing” it by stringifying and escaping the object manually:

{ 
    "input": "{ \"context\": { \"proxy\": {[...]) \"$key\": \"$val\"#if($foreach.hasNext),#end #end } }",
    "stateMachineArn": "${arn}"
}

I'm sure there are better ways, but this gets the job done.

preceptor
  • 1
  • 2
  • If anyone has a better solution, post it! This thing I came up with is not ideal, and I can't wait to fix it. – preceptor Mar 03 '23 at 22:11