11

I would like to know how to make API Gateway call a Step Function and execute it.

temoto
  • 5,394
  • 3
  • 34
  • 50
Filipe Santana
  • 361
  • 4
  • 12
  • 1
    duplicate: http://stackoverflow.com/questions/41113666/how-to-invoke-aws-step-function-using-api-gateway – Brandon Dec 26 '16 at 19:11
  • I think this question is more valuable than others because there is a explanation about how to create a aws sf invocation from api gateway – omalave Mar 06 '17 at 15:55
  • Possible duplicate of [How to Invoke AWS step function using API gateway?](https://stackoverflow.com/questions/41113666/how-to-invoke-aws-step-function-using-api-gateway) – Shamal Perera Jul 12 '17 at 16:40

4 Answers4

11

API Gateway added support for Step Functions currently. Now you can create an AWS Service integration via API Gateway Console.

  • Integration Type: AWS Service
  • AWS Service: Step Functions
  • HTTP method: POST
  • Action Type: Use action name
  • Action: StartExecution
  • Execution role: role to start the execution
  • Headers:

    X-Amz-Target -> 'AWSStepFunctions.StartExecution'
    Content-Type -> 'application/x-amz-json-1.0'

  • Body Mapping Templates/Request payload:

    { "input": "string", "name": "string", "stateMachineArn": "string" }

Ka Hou Ieong
  • 5,835
  • 3
  • 20
  • 21
  • Do you have any clue about this error? { "__type": "com.amazonaws.swf.service.v2.model#InvalidArn", "message": "Invalid Arn: 'Invalid ARN prefix: string'" } – omalave Mar 06 '17 at 23:32
  • I guess the ARN you define in ```stateMachineArn``` is not invalid. Can you verify that? – Ka Hou Ieong Mar 07 '17 at 21:59
  • Well, the arn is ok and when I test it in the api gateway console, sending the same json via post all works ok but in postman said that error and in my app too, any idea? – omalave Mar 08 '17 at 13:46
  • Can you enable CloudWatch Log for debugging? I wonder you don't have the right accpet/content-type headers on the request. – Ka Hou Ieong Mar 15 '17 at 00:01
  • Sure, I have from postman to API Gateway application/json and in API Gateway integration request X-Amz-Target -> 'AWSStepFunctions.StartExecution' Content-Type -> 'application/x-amz-json-1.0' Any clue? @Ka – omalave Mar 22 '17 at 14:18
  • Any idea how this can be defined via CloudFormation? – Kwhitejr Dec 18 '18 at 20:29
4

You can create an API Gateway Endpoint with Integration type: AWS Service and set it up to invoke the required Step Function.

In case you want to use API Gateway so that you can control the exposure of your Step Functions endpoint, you can create a new IAM user (programmatic access only) with a policy that only grants access to this endpoint, eg:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "execute-api:Invoke"           
      ],
      "Resource": [
        "arn:aws:execute-api:us-east-1:my-aws-account-id:my-api-id/my-stage/GET/my-resource-path"
      ]
    }
  ]
}  
David Salamon
  • 2,361
  • 25
  • 30
  • This approach did work for me. Can you explain the down vote? Please consider adding a comment if you think this post can be improved. – David Salamon Jan 19 '17 at 11:07
  • How did you configure an HTTP integration to call the Step Function API? AFAIK, this would not work as the request would not be signed with SigV4. This would be the correct answer if you change integration type to AWS. – RyanG Jan 20 '17 at 23:01
  • 1
    Thanks Ryan, edited my answer. This is great news! At the time of writing we didn't have the option to directly integrate Step Functions with API Gateway, this should do it. – David Salamon Jan 23 '17 at 08:16
3

Consider creating an AWS Lambda function that backs the APIGw endpoint and having it call AWS StepFunctions via code. We use this approach because our use case allows the API endpoint parameters to direct which of several StepFunctions we need to execute.

Admittedly, it is "more code"; we're hoping AWS elaborates StepFunctions such that they can be triggered by the whole host of AWS resource events.

CSSian
  • 1,621
  • 13
  • 19
1

I think you can use API Gateway Proxy Integration to AWS service. Look: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-method-settings-console.html