Invoke API Gateway Using Step Function

Invoke API Gateway Using Step Function

AWS Step Functions is a serverless function orchestrator used to create simple and complex workflows required for business-critical applications. In this blog post, we will expose an API using AWS API gateway. Then, we will create a step function, which will call an API Gateway directly within the workflow.

We use Amazon API Gateway to create, publish HTTP and REST APIs. For the step functions to integrate directly with the Amazon API gateway, we need to define a Task state in a Step Functions, which we can use to call the API gateway Http or Rest endpoint. All the necessary information to call the API Gateway should be defined in the Task state.

Create a Lambda Function and API Gateway

Let us create a Lambda Function and expose it through API Gateway.

Create an GreetUser Lambda function and add the below code to index.js

exports.handler = async (event) => {
  let user = event.pathParameters.user;

  const response = {
    statusCode: 200,
    body: "Hello " + user,
  return response;

The above lambda function looks into the path parameter for the user and greets the user passed into it. We can now create an API gateway MockApi with the following endpoint /greet/{user} as shown below and point the integration type to GreetUser Lambda Function.


After that, go to Actions and click on deploy and select the stage as v1. After that, we get an API endpoint for our greet user, which we will call from step function.


So, we can call https://<endpoint-id> to get the response.

Create a Step Function

We will create a step Function GreetUserStep. Choose the authoring method as Write your workflow in code. and select the Type as standard.

Add the below definition to the step function.

  "Comment": "An Example to call API Gateway from Step Function",
  "StartAt": "Call API Gateway Request",
  "States": {
    "Call API Gateway Request": {
      "Type": "Task",
      "Resource": "arn:aws:states:::apigateway:invoke",
      "Parameters": {
        "ApiEndpoint": "<your-api>",
        "Method": "GET",
        "Stage": "v1",
        "Path": "/greet/Rahul"
      "End": true

Here we have a state Call API Gateway Request of type Task. The Parameters field will contain all the required things to call an API Gateway. We are making a GET endpoint with path /greet/Rahul

Now, if click on start execution, we will get the execution result as shown below.


As we see in the above screenshot, we see that it is called the API Gateway and we can also see the ResponseBody as an indication.


This blog post explained about the Step functions and how we can call the API Gateway from Step Functions directly. For more information on Step Functions refer to the amazing documentation from Amazon here

Did you find this article valuable?

Support Rahul Lokurte by becoming a sponsor. Any amount is appreciated!