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 Antwort
1
Akzeptierte Antwort

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
EXPERTE
beantwortet vor einem Jahr
  • 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.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen