2

I have an API gateway route with a Step Function integration.

In this step function, I need the id given by the request.

With a lambda integration I understand how to do it, but in this case I do not find any ressource. Is it possible, and if yes, how ?

2 Answers 2

2

Adapt this VTL request mapping template* to add the path parameters to the state machine input.

  • Replace %STATEMACHINE% with your State Machine's ARN
  • Optionally set $includeHeaders or $includeQueryString to false.
  • Optionally set $requestContext to context key-value pairs. Use @@ instead of quotes, as in "{@@user@@:@@$context.identity.user@@}".
## Velocity Template used for API Gateway request mapping template
## "@@" is used here as a placeholder for '"' to avoid using escape characters.

#set($includeHeaders = true)
#set($includeQueryString = true)
#set($includePath = true)
#set($requestContext = '')

#set($inputString = '')
#set($allParams = $input.params())
{
    "stateMachineArn": "%STATEMACHINE%",

    #set($inputString = "$inputString,@@body@@: $input.body")

    #if ($includeHeaders)
        #set($inputString = "$inputString, @@header@@:{")
        #foreach($paramName in $allParams.header.keySet())
            #set($inputString = "$inputString @@$paramName@@: @@$util.escapeJavaScript($allParams.header.get($paramName))@@")
            #if($foreach.hasNext)
                #set($inputString = "$inputString,")
            #end
        #end
        #set($inputString = "$inputString }")
        
    #end

    #if ($includeQueryString)
        #set($inputString = "$inputString, @@querystring@@:{")
        #foreach($paramName in $allParams.querystring.keySet())
            #set($inputString = "$inputString @@$paramName@@: @@$util.escapeJavaScript($allParams.querystring.get($paramName))@@")
            #if($foreach.hasNext)
                #set($inputString = "$inputString,")
            #end
        #end
        #set($inputString = "$inputString }")
    #end

    #if ($includePath)
        #set($inputString = "$inputString, @@path@@:{")
        #foreach($paramName in $allParams.path.keySet())
            #set($inputString = "$inputString @@$paramName@@: @@$util.escapeJavaScript($allParams.path.get($paramName))@@")
            #if($foreach.hasNext)
                #set($inputString = "$inputString,")
            #end
        #end
        #set($inputString = "$inputString }")
    #end

    ## Check if the request context should be included as part of the execution input
    #if($requestContext && !$requestContext.empty)
        #set($inputString = "$inputString,")
        #set($inputString = "$inputString @@requestContext@@: $requestContext")
    #end
    
    #set($inputString = "$inputString}")
    #set($inputString = $inputString.replaceAll("@@",'"'))
    #set($len = $inputString.length() - 1)
    "input": "{$util.escapeJavaScript($inputString.substring(1,$len))}"
}

* The template is taken from the AWS Cloud Development Kit repo. It is part of the StepFunctionRestApi construct's implementation.

Sign up to request clarification or add additional context in comments.

Comments

0

If you're only interested in reading path parameters, here's a compact template you can use.

#set($inputString = "{@requestBody@: $input.json('$'), @pathParameters@: {")
#set($pathParams = $input.params().get('path'))
#foreach($paramName in $pathParams.keySet())
  #set($inputString = "$inputString @$paramName@: @$pathParams.get($paramName)@")
  #if($foreach.hasNext)
    #set($inputString = "$inputString,")
  #end
#end
#set($inputString = "$inputString }}")
#set($inputString = $inputString.replaceAll("@",'"'))
{
"input": "$util.escapeJavaScript($inputString).replaceAll("\\'","'")",
"name": "$context.requestId",
"stateMachineArn": "${ErNameIntent}"
}

This will result in a payload like:

{
  "requestBody": <requestbody>,
  "pathParameters": {
    <key1>: <value1>,
    <key2>: <value2>,
    ...
    <keyN>: <valueN>
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.