Important Note: You will create AWS resources during the exercise which will incur cost in your AWS account. It is recommended to clean-up the resources as soon as you finish the exercise to minimize the cost.

Using Amazon API Gateway in AWS Step Functions

AWS Step Functions is a serverless orchestrator to create state machine workflows. AWS Step Functions can orchestrate calls to multiple AWS services to build business applications. Amazon API Gateway is a managed service to create, publish, maintain, monitor, and secure APIs.

In this exercise, you learn how to call Amazon API Gateway APIs in AWS Step Functions state machine workflow.

The AWS Resource consumption for the exercise falls under AWS Free Tier.

Step1: Pre-Requisite


You need to have an AWS account with administrative access to complete the exercise. If you don’t have an AWS account, kindly use the link to create free trial account for AWS.

Step2: Create Lambda Function


The first step is to create the Lambda Function which works as the backend to the API in the Amazon API Gateway.

  1. Login to the AWS Console and choose Ireland as the region.

  2. In the Lambda Console, click on the Functions menu in the left and then click on the Create function button.

    Lambda

  3. On the next screen, select Author from scratch as the option. Type in dojolambdafunction as the name. Select Python 3.8 as the runtime. Select Create a new role with basic Lambda permissions as the option for the permissions. Finally click on the Create function button.

    Lambda

  4. Next, you goto the Function code area and update the code with the code provided below.

    Lambda

    import json
    
    def lambda_handler(event, context):
        print(event)
        return {
            'statusCode': 200,
            'body': json.dumps('call from step function worked')
        }
    

    `

  5. In the code above, the Lambda function receives the message from the API Gateway (which is called by the Step Function) and logs the message into CloudWatch log. It also returns a success message.

  6. Click on the Deploy button to save and upload the lambda function code.

    Lambda

  7. The Lambda function is ready. Let’s now configure REST API in API Gateway.

Step3: Configure API in API Gateway.


In this step, you configure REST API in API Gateway which calls Lambda function as the backend.

  1. In the API Gateway Console, click on the Build button for the REST API.

    API Gateway

  2. On the next screen, select New API option. Type in dojoapi for the API Name. Keep rest of the configuration to the default and click on the Create API button.

    API Gateway

  3. The API is created in no time. On the next screen, click on the Create Method option under the Action menu.

    API Gateway

  4. On the next screen, select POST as the method and click on the confirmation icon.

    API Gateway

  5. The method is created in no time. On the next screen, select Lambda Function option for the integration type and select dojolambdafunction for the Lambda Function. Click on the Save button.

    API Gateway

  6. It will throw a popup asking API Gateway permission to call the Lambda function. Click on the OK button.

    API Gateway

  7. The method is updated. On the next screen, click on the Deploy API option under the Action menu.

    API Gateway

  8. It will throw popup to configure the deployment stage. Select [New Stage] as the deployment stage. Type in prod for the stage name and click on the Deploy button.

    API Gateway

  9. The API is deployed to the prod stage. Make note of the Invoke URL - especially the part highlighted in the picture below. You will need it later when configuring the Step Functions.

    API Gateway

  10. The API is deployed and ready. In the next step, configure state machine workflow in AWS Step Functions which calls the API.

Step4: Configure and Run Workflow in Step Functions


You create state machine workflow in AWS Step Functions which call the REST API created in the previous step.

  1. In the AWS Step Functions Console, click on the State machines option in the left menu and then click on the Create state machine button.

    Step Functions

  2. On the next screen, select Author with code snippets option. Select Standard for type.

    Step Functions

  3. On the same screen, in the Definition section, replace the state machine configuration with the json given below. Note: Replace the ApiEndpoint value with the API invoke URL you made note of in the previous step.

    {
      "Comment": "Sample API Call",
      "StartAt": "CallAPI",
      "States": {
        "CallAPI": {
          "Type": "Task",
          "Resource": "arn:aws:states:::apigateway:invoke",
          "Parameters": {
            "Method": "POST",
            "RequestBody": {
              "Payload.$": "$.data"
            },
            "AuthType": "NO_AUTH",
            "ApiEndpoint": "kf1tjzty1k.execute-api.eu-west-1.amazonaws.com",
            "Stage.$": "$.stage"
          },
          "End": true
        }
      }
    }
    

    `

    Step Functions

  4. In the configuration above, you are calling the API endpoint. The method is POST. The authentication type is NO_AUTH. The stage of the API will be passed by the input parameter to the workflow. Similarly, Request Body to API is also passed by the input parameter to the workflow. Click on the Next button.

  5. On the next screen, type in dojostatemachine for the state machine name. Select Create new role option. Keep rest of the configuration to the default and click on the Create state machine button.

    Step Functions

  6. The state machine workflow is saved. On the next screen, click on the Start execution button.

    Step Functions

  7. On the execution popup window, copy-paste input parameter shown below. You are passing data for the request body and stage for the API stage as these two configuration are parameterized in the state machine workflow. Click on the Start execution button.

    {
        "data": "sample input data",
        "stage": "prod"
    }
    

    `

    Step Functions

  8. The state machine workflow executes successfully. You can see the response from the API / Lambda function in the output window.

    Step Functions

    Step Functions

  9. If you go back to check CloudWatch log for the dojolambdafunction Lambda Function, you can see the parameter passed by the state machine workflow logged there.

    Step Functions

  10. This finishes the exercise. Follow the next step to clean-up the resources so that you don’t incur any cost post the exercise.

Step5: Clean up


Delete dojolambdafunction Lambda Function.

Delete dojoapi API in API Gateway.

Delete dojostatemachine state machine workflow in Step Functions.

Thanks and hope you enjoyed the exercise.


Back to the Exercises