jsonamazon-web-servicesaws-api-gatewayaws-step-functions

Is there a way to passthrough header and body of a request to aws step function pipeline?


I am currently stuck with a request passthrough. I need to process information from the header as well as from the body of a request with a step function pipeline. I am using a REST API Gateway that has in the integration request the following mapping template

#set($data = $util.escapeJavaScript($input.json('$')))
{
    "input": "{ \"input\": $data, \"stageVariables\" : { #foreach($key in $stageVariables.keySet()) \"$key\" : \"$util.escapeJavaScript($stageVariables.get($key))\" #if($foreach.hasNext),#end #end } }",
    "stateMachineArn": "arn:aws:states:eu-west-1:xxxxxxxxxxxx:stateMachine:StateMachine-1"
}

But this does not work. If I would access the header with a lambda event it would be event['headers']['Authorization'] But I only get the body of my request as input to my step function.

When I use the Data Flow Simulator with my whole request and then take the JsonPath into my state machine template, it still does not give me the header and body as input.

Can someone please help me out here?


Solution

  • Adapt this VTL request mapping template* to add request data to the state machine input.

    ## 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.