Api gateway get output results from step function?

Viewed 15599

I followed tutorial on creating and invoking step functions

I'm getting output in my GET request of api as

 {
  "executionArn": "arn:aws:states:ap-northeast-1:123456789012:execution:HelloWorld:MyExecution",
  "startDate": 1.486772644911E9
}

But, instead of above response I want my step functions output, which is given by end state as below.

{
   "name":"Hellow World"
}

How to achieve this?

5 Answers

Use Express Step Functions instead. This type of Step Functions can be called synchronously. Go to your API Gateway and in the Integration Request section make sure you have the StartSyncExecution action:

enter image description here

After that, go a bit lower in the same page to the Mapping Templates:

enter image description here

and include the following template for the application/json Content-Type:

#set($input = $input.json('$'))
{
   "input": "$util.escapeJavaScript($input)",
   "stateMachineArn": "arn:aws:states:us-east-1:your_aws_account_id:stateMachine:your_step_machine_name"
}

After that, go back to the Method Execution and go to the Integration Response and then to the Mapping Templates section:

enter image description here

And use the following template to have a custom response from your lambda:

#set ($parsedPayload = $util.parseJson($input.json('$.output')))
$parsedPayload

My testing Step Function is like this:

enter image description here

And my Lambda Function code is:

enter image description here

Deploy your API Gateway stage.

Now, if you go to Postman and send a POST request with any json body, now you have a response like this:

enter image description here

Related