简体   繁体   中英

How to Invoke AWS step function using API gateway?

According to Amazon's documentation, step function can be invoked using HTTP API.

Step Functions can be accessed and used with the Step Functions console, the AWS SDKs, or an HTTP API.

I tried to search the detailed information, but can't seem to find any good ones. Does anyone know how to invoke AWS step function using API gateway, similar to the way it invokes Lambda functions?

This is not the "official" AWS way -- see Erndob's answer for that.

The problem with the AWS way (sign each request with AWS credentials) is that most enterprises already have mature methods in place to manage authentication and authorization via their API gateways and (speaking as an enterpise architect) do not want to deal with the headache of duplicating this at the AWS-credential-level.

I'm sure that AWS will eventually integrate Step Functions with API Gateway but as of this writing (1/17) this is probably the simplest way to get the job done. Below is a trivial Lambda proxy function I wrote to leverage the SDK's ability to sign the requests:

 'use strict'; const AWS = require('aws-sdk'); const stepfunctions = new AWS.StepFunctions(); exports.handler = (event, context, callback) => { if(!event && event.action) callback("Error: 'action' is required."); if(!event && event.params) callback("Error: 'params' is required."); stepfunctions[event.action](event.params, function (err, data) { if (err) console.log(err, err.stack); callback(err, data); }); };

You will need to grant your Lambda privs to interact with your Step Functions. To give it full access to all operations create a new role and attach the following policies:

  • AWSLambdaBasicExecutionRole
  • AWSStepFunctionsFullAccess

Now configure the Lambda to be invoked via API gateway as normal, passing in an event with two properties:

And be sure to lock your API down! :-)

If you need to call StepFunction from API Gateway, it's now possible and described well in docs: https://docs.aws.amazon.com/step-functions/latest/dg/tutorial-api-gateway.html

  • For Integration Type, choose AWS Service
  • For AWS Service, choose Step Functions from the list
  • For HTTP Method, choose POST from the list
  • For Action Type, choose Use action name
  • For Action, type StartExecution
  • For Execution Role, type ARN of role with API Gateway trusted identity provider and attached policy AWSStepFunctionsFullAccess

It's using HTTP API, not API Gateway.

Step function endpoints follow this format:

https://states.${region}.amazonaws.com

for example:

https://states.us-east-1.amazonaws.com

And you use HTTP API (again, not API gateway) to make actions on your states.

More about HTTP API here:

http://docs.aws.amazon.com/step-functions/latest/apireference/Welcome.html

Technically you could use API gateway, to redirect to step functions API but there's not much point in that.

我最近在https://stackoverflow.com/a/59326771/6697093上发布了一个使用 CloudFormation 和 OpenApi 使其工作的示例代码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM