How to Invoke AWS step function using API gateway?

后端 未结 4 1228
天涯浪人
天涯浪人 2021-01-18 13:34

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

Step Functions can be accessed and used with the Step Functions c

4条回答
  •  我在风中等你
    2021-01-18 14:08

    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:

    • action (the Step Functions method name, like "startExecution")
    • params (the params needed for that method. Ref here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/StepFunctions.html)

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

提交回复
热议问题