API Gateway to Step Functions transform and enrich payload in a mapping template

0

Right now I'm struggling to create the following input for a step function:

#set($data = {
"auth": {
    "tenantId": "foo",
    "userId": "bar",
    "groups": "baz"
},
"data": $input.path('$')
})   
{ 
    "stateMachineArn": "myArn",     
    "input": $util.escapeJavaScript($data).replaceAll("\\'","'")
}

Where foo, bar, baz are replaced by $context... elements which are not available in the test area of API Gateway afaik. In the API Gateway Method Test I can see that this payload is generated:

Endpoint request body after transformations: { 
    "stateMachineArn": "myArn",
    "input": {auth={tenantId=foo, userId=bar, groups=baz}, data={Test=Test}}
}

Based on my template I expected a output like this: "{\"auth\":{\"tenantId\":\"foo\",\"userId\":\"bar\",\"groups\":\"baz\"},\"data\":{\"test\":\"test\"}}" for the "input".

Why is that not happening?

1 Answer
1
Accepted Answer

Hi,

Take a look at the following implementation example, I hope it will help you.

Code

#set($data='{ "auth":{"tenantId":"foo", "userId":"bar", "groups":"baz"}, "data" :'+$input.json('$')+'}')
#set($data=$util.escapeJavaScript($data))
{
  "input" :"$data",
  "stateMachineArn": "myArn"
}

Input

{
  “Test”: “Test"
}

Output

{
  "input" :"{ \"auth\":{\"tenantId\":\"foo\", \"userId\":\"bar\", \"groups\":\"baz\”},  \”data\”: { \”Test\":\”Test\”}}",
  "stateMachineArn": "myArn"
}
profile picture
EXPERT
answered a year ago
  • Thanks Mikel, that works! Didn't know that I can just concatenate my json string that way. There is no way to convert my VTL object to a json string?

  • Unfortunately the $util variable does not contain a utility functions for that, and I don't know if there is any other elegant way.

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