API gateway request modification into step function request.

0

#set($allParams = $input.params()) { "body-json" : $input.json('$'), "params" : { #foreach($type in $allParams.keySet()) #set($params = $allParams.get($type)) "$type" : { #foreach($paramName in $params.keySet()) "$paramName" : "$util.escapeJavaScript($params.get($paramName))" #if($foreach.hasNext),#end #end } #if($foreach.hasNext),#end #end }, "stage-variables" : { #foreach($key in $stageVariables.keySet()) "$key" : "$util.escapeJavaScript($stageVariables.get($key))" #if($foreach.hasNext),#end #end }, "context" : { "account-id" : "$context.identity.accountId", "api-id" : "$context.apiId", "api-key" : "$context.identity.apiKey", "authorizer-principal-id" : "$context.authorizer.principalId", "caller" : "$context.identity.caller", "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider", "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType", "cognito-identity-id" : "$context.identity.cognitoIdentityId", "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId", "http-method" : "$context.httpMethod", "stage" : "$context.stage", "source-ip" : "$context.identity.sourceIp", "user" : "$context.identity.user", "user-agent" : "$context.identity.userAgent", "user-arn" : "$context.identity.userArn", "request-id" : "$context.requestId", "resource-id" : "$context.resourceId", "resource-path" : "$context.resourcePath" } }

How do i send all these fields into step function input key.

I have tried every way it does not work.

currently I am only able to send below request.

#set($data='{ "context" : {"account-id" : "'+$context.identity.accountId+'","api-id" : "'+$context.apiId+'","api-key" : "'+$context.identity.apiKey+'","authorizer-principal-id" : "'+$context.authorizer.principalId+'","caller" : "'+$context.identity.caller+'","cognito-authentication-provider" : "'+$context.identity.cognitoAuthenticationProvider+'","cognito-authentication-type" : "'+$context.identity.cognitoAuthenticationType+'","cognito-identity-id" : "'+$context.identity.cognitoIdentityId+'","cognito-identity-pool-id" : "'+$context.identity.cognitoIdentityPoolId+'","http-method" : "'+$context.httpMethod+'","stage" : "'+$context.stage+'","source-ip" : "'+$context.identity.sourceIp+'","user" : "'+$context.identity.user+'","user-agent" : "'+$context.identity.userAgent+'","user-arn" : "'+$context.identity.userArn+'","request-id" : "'+$context.requestId+'","resource-id" : "'+$context.resourceId+'","resource-path" : "'+$context.resourcePath+'"}, "body" :'+$input.json('$')+'}') #set($data=$util.escapeJavaScript($data)) { "input" :"$data", "stateMachineArn": "my-state-machine-arn" }

Thanks for any help.

2 Antworten
1
Akzeptierte Antwort

I was able to find a solution for this.

I found it on stackoverflow, here is the link to the answer https://stackoverflow.com/a/71195539

Below is the Template

## 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 = "{@@account-id@@:@@$context.identity.accountId@@,@@api-id@@:@@$context.apiId@@,@@api-key@@:@@$context.identity.apiKey@@,@@authorizer-principal-id@@: @@$context.authorizer.principalId@@,@@caller@@:@@$context.identity.caller@@,@@cognito-authentication-provider@@: @@$context.identity.cognitoAuthenticationProvider@@,@@cognito-authentication-type@@:@@$context.identity.cognitoAuthenticationType@@,@@cognito-identity-id@@: @@$context.identity.cognitoIdentityId@@,@@cognito-identity-pool-id@@:@@$context.identity.cognitoIdentityPoolId@@,@@http-method@@:@@$context.httpMethod@@,@@stage@@: @@$context.stage@@,@@source-ip@@:@@$context.identity.sourceIp@@,@@user@@:@@$context.identity.user@@,@@user-agent@@:@@$context.identity.userAgent@@,@@user-arn@@: @@$context.identity.userArn@@,@@request-id@@:@@$context.requestId@@,@@resource-id@@:@@$context.resourceId@@,@@resource-path@@:@@$context.resourcePath@@}")

#set($inputString = '')
#set($allParams = $input.params())
{
    "stateMachineArn": "my-state-machine-arn",

    #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 @@context@@: $requestContext")
    #end
    
    #set($inputString = "$inputString}")
    #set($inputString = $inputString.replaceAll("@@",'"'))
    #set($len = $inputString.length() - 1)
    "input": "{$util.escapeJavaScript($inputString.substring(1,$len))}"
}
beantwortet vor 3 Monaten
profile picture
EXPERTE
überprüft vor 2 Monaten
0

Hi Abhay,

How about serializing entire request context, parameters, stage variables, and body into a single JSON object? This involves dynamically constructing a JSON string that incorporates all parts of the request. Below is an example which captures everything: capture everything: the context, stage variables, parameters (query string parameters, path parameters, and headers), and the request body:

#set($allParams = $input.params())
#set($contextMap = {
  "account-id" : "$context.identity.accountId",
  "api-id" : "$context.apiId",
  "api-key" : "$context.identity.apiKey",
  "authorizer-principal-id" : "$context.authorizer.principalId",
  "caller" : "$context.identity.caller",
  "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
  "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
  "cognito-identity-id" : "$context.identity.cognitoIdentityId",
  "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
  "http-method" : "$context.httpMethod",
  "stage" : "$context.stage",
  "source-ip" : "$context.identity.sourceIp",
  "user" : "$context.identity.user",
  "user-agent" : "$context.identity.userAgent",
  "user-arn" : "$context.identity.userArn",
  "request-id" : "$context.requestId",
  "resource-id" : "$context.resourceId",
  "resource-path" : "$context.resourcePath"
})
#set($stageVariables = $context.stageVariables)
#set($body = $input.json('$'))

#set($inputObject = {
  "context" : $util.toJson($contextMap),
  "stage-variables" : $util.toJson($stageVariables),
  "params" : $util.toJson($allParams),
  "body" : $body
})

#set($inputString = $util.toJson($inputObject))
{
  "input": "$util.escapeJavaScript($inputString)",
  "stateMachineArn": "my-state-machine-arn"
}

This script does the following: Constructs a map for the context and stage variables. Uses $input.params() to gather all parameters (query string, path, headers) automatically. Serializes the context, stage variables, parameters, and body into a JSON object. Converts the entire JSON object into a string to be used as the input for the Step Function. Make sure to replace "my-state-machine-arn" with the actual ARN of your state machine.

Hope this helps!

beantwortet vor 3 Monaten
  • This does not work, Because "$util.toJson()" is not a function in the API Gateway Docs. Thanks for the reply.

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