Make a call to API Gateway - AWS Step Functions
AWS services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with AWS services in China.

Make a call to API Gateway

This sample project demonstrates how to use Step Functions to make a call to API Gateway and checks whether the call succeeded. This sample project creates the following:

  • An Amazon API Gateway REST API that is called by the state machine.

  • Related AWS Identity and Access Management (IAM) roles.

For more information about API Gateway and Step Functions service integrations, see the following:

Create the State Machine and Provision Resources

  1. Open the Step Functions console and choose Create a state machine.

  2. Choose Sample Projects, and then choose Start an API Gateway query.

    The state machine Code and Visual Workflow are displayed.

    
            Training model workflow.
  3. Choose Next.

    The Deploy resources page is displayed, listing the resources that will be created. For this sample project, the resources include:

    • A state machine

    • A call to the API Gateway REST API

    • Related IAM roles

  4. Choose Deploy Resources.

    Note

    It can take up to 10 minutes for these resources and related IAM permissions to be created. While the Deploy resources page is displayed, you can open the Stack ID link to see which resources are being provisioned.

Start a New Execution

  1. Open the Step Functions console.

  2. On the State machines page, choose the ApiGatewayWorkflowStateMachine state machine that was created by the sample project, and then choose Start execution.

  3. On the New execution page, enter an execution name (optional), and then choose Start Execution.

  4. (Optional) To help identify your execution, you can specify an ID for it in the Enter an execution name box. If you don't enter an ID, Step Functions generates a unique ID automatically.

    Note

    Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.

  5. (Optional) Go to the newly created state machine on the Step Functions Dashboard, and then choose New execution.

  6. When an execution is complete, you can select states on the Visual workflow and browse the Input and Output under Step details.

Example State Machine Code

The state machine in this sample project integrates with API Gateway by calling the API Gateway REST API and passing any necessary parameters.

Browse through this example state machine to see how Step Functions interacts with API Gateway.

For more information about how AWS Step Functions can control other AWS services, see Using AWS Step Functions with other services.

{ "Comment": "Calling APIGW REST Endpoint", "StartAt": "Add Pet to Store", "States": { "Add Pet to Store": { "Type": "Task", "Resource": "arn:aws:states:::apigateway:invoke", "Parameters": { "ApiEndpoint": "<POST_PETS_API_ENDPOINT>", "Method": "POST", "Stage": "default", "Path": "pets", "RequestBody.$": "$.NewPet", "AuthType": "IAM_ROLE" }, "ResultSelector": { "ResponseBody.$": "$.ResponseBody" }, "Next": "Pet was Added Successfully?" }, "Pet was Added Successfully?": { "Type": "Choice", "Choices": [ { "Variable": "$.ResponseBody.errors", "IsPresent": true, "Next": "Failure" } ], "Default": "Retrieve Pet Store Data" }, "Failure": { "Type": "Fail" }, "Retrieve Pet Store Data": { "Type": "Task", "Resource": "arn:aws:states:::apigateway:invoke", "Parameters": { "ApiEndpoint": "<GET_PETS_API_ENDPOINT>", "Method": "GET", "Stage": "default", "Path": "pets", "AuthType": "IAM_ROLE" }, "ResultSelector": { "Pets.$": "$.ResponseBody" }, "ResultPath": "$.ExistingPets", "End": true } } }

For information about how to configure IAM when using Step Functions with other AWS services, see IAM Policies for Integrated Services.

IAM Example

These example AWS Identity and Access Management (IAM) policies generated by the sample project include the least privilege necessary to execute the state machine and related resources. We recommend that you include only those permissions that are necessary in your IAM policies.

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "execute-api:Invoke" ], "Resource": [ "arn:aws:execute-api:us-west-1:111122223333:c8hqe4kdg5/default/GET/pets", "arn:aws:execute-api:us-west-1:111122223333:c8hqe4kdg5/default/POST/pets" ], "Effect": "Allow" } ] }

For information about how to configure IAM when using Step Functions with other AWS services, see IAM Policies for Integrated Services.