Step Functions Made Easy - Connect with API Gateway

aws apigateway

Unlock the power of AWS with API Gateway integration into Step Functions


In November 2020, AWS unveiled support for AWS API Gateway's seamless integration with Synchronous Express Step Function workflows. This integration simplifies microservices orchestration, as Synchronous Express workflows patiently await execution completion, streamlining the response process. This eliminates the need for extra polling services typically necessary for asynchronous Step Function workflows. It empowers you to effortlessly initiate a step function from API Gateway as a REST API.

API Gateway and Step Function Integration

With the api gateway integration with step function, users can invoke a step function execution using an rest api request and get the response back. This blog will cover the steps about the how to call step function from api gateway and automation of the infra creation and setup using the serverless. api gateway step function

How to create a Synchronous Express step function workflow ?

  1. Navigate to AWS Webconsole and Select Step Function
  2. Click on the Create State Machine
  3. Select "Design your workflow visually" and the type as "Express" step function visual design
  4. Select the services that you want to be orchestrated and create the step function
  5. Review the ASL generated,provide the permission for the step function to use the service(IAM Role) and complete the creation process by clickin the Create state machine button. step function final page

Connect Step Function from AWS API Gateway

First step to invoke a step function using the aws api gateway is to integrate the created step function with an api gateway end point. Read through the below steps for api gateway creation and integrating the step function,

  1. Navigate to API Gateway and click on Create API
  2. Choose the api type as REST API in this page and click on the Build api gateway home page
  3. Select New API option and enter the API name under the settings. Choose the endpoint type as Regional.
  4. On the next page , click on the Actions -> Create Method and select the method as POST.
  5. In the API setup select the integration type as AWS Service, then select the AWS region where the step function was created along with the AWS Servuce as Step functions.
  6. Leave the AWS Subdomain blank and select HTTP method as POST
  7. Select the Ation Type as use action name and enter the Action as "StartSyncExecution". If you enter this value as "StartExecution", this integrate the step function as asynchronous way with step function.
  8. Provide the Execution role which provide the api gateway to trigger the step function.Ensure role have "states:StartSyncExecution" action as allow for the api gateway. api gateway setup
  9. Select the content Handling as Passthrough and Use Default timeout.
  10. Once the API is created you can view the options as below which shows the api gateway integration with step function. step function + api gateway

API Gateway and Step Function integration using Serverless Step Functions Plugin

Serverless started supporting the api gateway and step function integration using their plugin serverless-step-functions. This plugin need to be installed as dev dependency for us to configure the api gateway and step function.

Once the plugin is configured, in the serverlss.yml add the step function state machine along with the events section as below,

stepFunctions:
  validate: true
  stateMachines:
    myStepfn:
      type: EXPRESS
      loggingConfig:
        level: ALL
        includeExecutionData: true
        destinations:
          - Fn::GetAtt: [StepFunctionLogGroup, Arn]
      name: myStepfn-${opt:stage}
      events:
        - http:
            path: /my-end-point
            method: post
            cors: true
            action: StartSyncExecution

Serverless expose the API Gateway action attribute which we can use to set the custom step functions actions. In the above example,the action is set for StartSyncExecution which is equivalent to the synchronous execution of the step function. Incase we dont need the synchronous execution action value can be set as StartExecution. Please refer the serverless.yml for additional details around the serverless.yml configuration for stepfunction and apigateway integration.

AWS Step Function and API Gateway Integration Benefits & Limitations

With the api gateway integration to step function, it is easy to orchestrate multiple lambda microservices and still leverage it as a rest api. This also makes it easy to make calls to AWS Elastic Beanstalk, Amazon EC2, or web services outside of AWS and orchestrate everything with retry and error handling. All the benefits of the AWS API gateway like, the request validation, setting up authorization and handling of error is also available in this integration.

Currently API Gateway has a max time out of the 30seconds and if the step function execution is going to take anything more than 30seconds, api gateway is going to return the 504 status. In this case, it is better to integrate the step function as asynchronous and the api gateway will return the response immediately after the step function is invoked , without waiting for the step function response.