Integrating Amazon API Gateway's REST API to AWS Step Functions' Synchronous Express Workflow

Author

Engineering Manager @ AntStack.io

Integrating Amazon API Gateway's REST API to AWS Step Functions' Synchronous Express Workflow

July 29, 2021

serverlessapigatewaystepfunctions

Integrating Amazon API Gateway's REST API to AWS Step Functions' Synchronous Express Workflow

AWS introduced Synchronous Express Workflows for AWS Step Functions on 24 Nov 2020. Synchronous express workflows start the execution, wait until the execution is completed, and return the result. This introduction eliminated the need for asynchronous step function handler systems.

One of the patterns that came with this introduction is connecting an API Gateway’s REST API to Step Function’s Synchronous Workflow. A user sends a request to the API Gateway’s endpoint, API Gateway invokes the Express Step Function’s synchronous API (StartSyncExecution), waits for the Step function execution to complete, and sends the result of the execution to the user.

Step Function Webinar

This blog will go through the steps involved in integrating an API Gateway's REST API to a Synchronous Step Function Express Workflow. And to take it one step further, we will add an API key and send it to the Step Function. Not only that, but we are also going to use mapping templates to hardcode the Step Function ARN to avoid passing it to the API.

Here’s an example with and without the mapping templates.

Without Mapping Template

A Postman example of API Gateway to Synchronous Step Function integration without Mapping Templates

Without Mapping Template

A Postman example of API Gateway to Synchronous Step Function integration with Mapping Templates

To skip the walkthough below, refer to this repo. Follow the instructions to create a SAM package and deploy the resources in your AWS Account.

Let’s get started!

Part 1 - Express Workflow

In this part, we will walk through the steps to create an express workflow in the AWS Step Functions console.

Step 1

Part 1 - Step 1

Navigate to the Step Functions console and click “Create state machine.” In the next screen, select “Design your workflow visually,” and in type, select “Express,” as shown in the screenshot above. Click on “Next” to move to the next screen. Remember that we cannot change the “type” of a Step Function after being created. If we need to change the Step Function type, we need to delete the existing Step Function and create a new one.

Step 2

Part 1 - Step 2

This step is about using the Workflow Studio to build a Step Function State Machine. It was introduced recently (Jun 17, 2021) to help create Step Function without worrying about the Amazon States Language. Create a workflow by dragging and dropping items from the left section to the middle section, configure the step information on the right section. After designing the workflow for the State Machine, click on “Next” to move to the next screen. For more details about the workflow studio, click here.

Step 3

Part 1 - Step 3 Creating the Step Function State Machine using the Workflow Studio generates the Definition. In this step, we review the generated Definition to make any changes if required. After reviewing the Definition, click “Next”.

Step 4

In this final step, we configure the Step Function State Machine settings.

Part 1 - Step 4-1

  • Name - a unique name for the Step Function State Machine.
  • Permissions - an IAM Role for the Step Function State Machine to access the resources declared in the definition.

Part 1 - Step 4-2

  • Logging - enable logging for the Step Function State Machine.
  • Tracing - enable X-Ray tracing for the Step Function State Machine.

Part 1 - Step 4-3

  • Tags - create tags for the Step Function State Machine.

Finally, click on “Create state machine” to create the Step Function State Machine.

Part 2 - REST API

In this part, we are going to walkthrough the steps to create a REST API using the API Gateway console. We are also going to add API Keys, Usage Plan, and configure the mapping templates.

Step 1

Navigate to the API Gateway console, click on “Create API” on the right top corner. Click on “Build” under “REST API”.
Part 2 - Step 1-2

  • Choose the protocol - select “REST”
  • Create new API - select “New API”
  • Settings - provide a suitable “API name”, “Description” (optional), and ”Endpoint Type

Click “Create API” to create the base API.

Step 2

Open the newly created API. Click on “Resources” and create a “POST” method and set the “Integration Request” following as shown in figure

Part 2 - Step 2-2

  • Integration type - AWS Service
  • AWS Region - Set the appropriate region
  • AWS Service - Step Functions
  • Action - StartSyncExecution
  • Execution role - Role ARN to allow API Gateway to invoke Step Function API (IAM Action - “states:StartSyncExecution”)

Step 3

Scroll down in “Integration Request” and add a mapping template for the content type of application/json. Click on “application/json” and scroll down to create the template.

Part 2 - Step 3-1 Use the following template and replace the step function arn and click “Save” at the bottom of the screen.

#set($body= $input.json('$'))
#set($inputRoot='{ "data" :'+$body+',"apiInfo":{"httpMethod" :"'+ $context.httpMethod+'", "apiKey":"'+ $context.identity.apiKey+'"}}')
#set($apiData=$util.escapeJavaScript($inputRoot))
#set($apiData=$apiData.replaceAll("\\'","'"))
{
  "input" :"$apiData",
  "stateMachineArn": "REPLACE_STEPFUNCTION_ARN"  
}

The template takes the input data, adds “apiInfo” which contains the httpMethod and apiKey. The data is transformed as per the Step Function API input.

Once the “Integration Request” has been configured, by clicking on the created method, we will be able to see the rest of the options, similar to the screen shown below. Part 2 - Step 3-3

The “Integration Request” deals with the data structure sent while invoking the Step Function. To make API Key mandatory, we need to configure the “Method Request”. To modify the data structure of the output of API, we need to configure the “Integration Response”.

Part 2 - Step 3-2

  • Click on “Method Request” and Set the API Key Required to true in the “Method Request”.

Now, lets configure the “Integration Response” Part 2 - Step 3-4

  • Expand the default method response.
  • Expand the mapping templates section
  • Click on the “application/json” content type
  • Add the sample template given below to handle the Step Function output
#set ($bodyObj = $util.parseJson($input.body))
        
#if ($bodyObj.status == "SUCCEEDED")
    $bodyObj.output
#elseif ($bodyObj.status == "FAILED")
    #set($context.responseOverride.status = 500)
    {
        "cause": "$bodyObj.cause",
        "error": "$bodyObj.error"
    }
#else
    #set($context.responseOverride.status = 500)
    $input.body
#end
  • Save the mapping template and the default method response.

Finally, deploy the API into an appropriate stage.

Step 4

Open the newly created API.
Part 2 - Step 4

  • Click on “API Keys” in the left section.
  • Click on “Actions” and select “Create API key” in the middle section.
  • Enter an API Key parameters and click “Save” in the right section.

Step 5

Open the newly created API.

Part 2 - Step 5-1

  • Click on “Usage Plans” in the left section.
  • Click on “Create”, set the appropriate “Throttling” and “Quota”, click “Next”

Part 2 - Step 5-2

  • Click “Add API Stage”, select the appropriate API and Stage from the dropdown, click “Next”.

Part 2 - Step 5-3

  • Click “Add API Key to Usage Plan”, select the appropriate API Key from the dropdown, click “Done”

All done! Now retrieve the URL from the deployed stage from API Gateway and start consuming it.

The entire process can be pretty confusing while doing it for the first time, so here’s a repo with a sample SAM application that creates all the required resources.

“Synchronous Express Workflows are ideal to manage high-volume, short duration, synchronous workflows such as the orchestration of microservices behind Amazon API Gateway. For example, capture responses submitted through an online feedback form and return a reference number to the submitter. Express Workflows can now be synchronously invoked from Amazon API Gateway, AWS Lambda, or by directly calling the new API.”

Click here to view the complete article

If you are interested in knowing more about AWS Step Functions, join us in an upcoming webinar where we discuss expanding the horizons of applications using AWS Step Functions, exploring the AWS Step Functions’ functionality, use cases, and a live demo.

Register for the AWS Step Functions Webinar here .

Step Function Webinar

Antstack Blog Post
Antstack Blog Post

Are you planning to go to serverless? AntStack is a cloud computing service and consulting company primarily focusing on Serverless Computing. We help companies get up and running with serverless, and we’ll make sure that there are no limits.

Keep track of our socials and connect with us - LinkedIn

event bannerevent bannerevent banner