Step Functions Made Easy - Connect with API Gateway
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.
How to create a Synchronous Express step function workflow ?
- Navigate to AWS Webconsole and Select Step Function
- Click on the Create State Machine
- Select "Design your workflow visually" and the type as "Express"
- Select the services that you want to be orchestrated and create the step function
- 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.
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,
- Navigate to API Gateway and click on Create API
- Choose the api type as REST API in this page and click on the Build
- Select New API option and enter the API name under the settings. Choose the endpoint type as Regional.
- On the next page , click on the Actions -> Create Method and select the method as POST.
- 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.
- Leave the AWS Subdomain blank and select HTTP method as POST
- 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.
- 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.
- Select the content Handling as Passthrough and Use Default timeout.
- Once the API is created you can view the options as below which shows the api gateway integration with step function.
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.