API Gateway to Step Functions

22 July, 2021
Back

You can trigger a workflow from API Gateway to Step Functions. You would need to set these up:

  1. Create an API Gateway.
  2. Create a state machine in Step Functions.
  3. POST data from the API Gateway endpoint.

1. Create an API Gateway

You can configure a POST /route with these configs.

api gateway step functions setup

You must enter StartExecution in the Actions field. This is just one of the things you won't know what to enter until you find the docs.

2. Create a state machine in Step Functions

Here's an example of one that I've created on Workflow Studio.

TakeInput will receive a JSON in this format:

{
    "route":"triple",
    "value": 10
}

If the route says triple, it will go left. Otherwise, the default route will multiple the given value by 2.

step function workflow studio

3. POST data from the API Gateway endpoint

Step Functions is fussy with the data format you send to it. The format for StartExecution look like this:

{
    "input":"{\"route\":\"triple\", \"value\":9}",
    "stateMachineArn":"arn:aws:states:ap-southeast-1:495578919953:stateMachine:RouteTripleOrDouble"
}

You need to put this in the body of your POST request to the API endpoint.

The API Gateway endpoint would look something like this: https://xx88xx88xx.execute-api.ap-southeast-1.amazonaws.com/prd/route

I didn't know how to escape JSON, so I used an online format tool. All it really does is to send route:"triple" and value:9 to the RouteTripleOrDouble Step Function.

And finally this is the result:

step function execution result

Tutorial


Back