如何使用API​​网关调用AWS Step Function [英] How to invoke an AWS Step Function using API Gateway

查看:172
本文介绍了如何使用API​​网关调用AWS Step Function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用API​​网关POST请求以及步骤函数的请求的JSON有效负载来调用AWS Step Function?

How do I invoke an AWS Step Function using an API Gateway POST request, and the request's JSON payload to the Step Function ?

推荐答案

1.创建您的步进功能

很明显.我想如果您正在阅读本文,您就会知道该怎么做.

1. Create your step function

Quite obvious. I guess that if you're reading this you know how to do it.

否则,您可以在这里查看文档:

Otherwise, you can go have a look at the documentation here: What is AWS Step Functions?.

它既可以用于所有步骤功能,也可以仅用于此功能.我们将仅介绍第一种情况,如Amazon教程中所述:

It can be for either all Step Functions, or only this one. We'll only cover the first case, as explained in an Amazon tutorial: Creating an API Using API Gateway.

创建IAM角色

To create the IAM role

  • 登录到AWS Identity and Access Management控制台.

  • Log in to the AWS Identity and Access Management console.

在角色"页面上,选择创建新角色".

On the Roles page, choose Create New Role.

在设置角色名称"页面上,键入APIGatewayToStepFunctions作为角色名称",然后选择下一步".

On the Set Role Name page, type APIGatewayToStepFunctions for Role Name, and then choose Next Step.

在选择角色类型"页面上的选择角色类型"下,选择"Amazon API Gateway".

On the Select Role Type page, under Select Role Type, select Amazon API Gateway.

在附加策略"页面上,选择下一步".

On the Attach Policy page, choose Next Step.

在审阅"页面上,记录角色ARN,例如:

On the Review page, note the Role ARN, for example:

arn:aws:iam::123456789012:role/APIGatewayToStepFunctions

要将策略附加到IAM角色

To attach a policy to the IAM role

  • 在角色"页面上,按名称(APIGatewayToStepFunctions)搜索您的角色,然后选择角色.
  • 在权限"选项卡上,选择附加策略".
  • 在附加策略"页面上,搜索AWSStepFunctionsFullAccess,选择该策略,然后选择附加策略".
  • On the Roles page, search for your role by name (APIGatewayToStepFunctions) and then choose the role.
  • On the Permissions tab, choose Attach Policy.
  • On the Attach Policy page, search for AWSStepFunctionsFullAccess, choose the policy, and then choose Attach Policy.


3.设置

3.a如果没有JSON负载

如Ka Hou Ieong在如何通过API网关调用AWS Step Functions 所述,您可以创建一个通过API网关控制台进行AWS服务集成,如下所示:


3. Setup

3.a If you don't have a JSON payload

As explained by Ka Hou Ieong in How can i call AWS Step Functions by API Gateway?, you can create an AWS Service integration via API Gateway Console, like this:

  • 集成类型: AWS服务
  • AWS服务:步骤功能
  • HTTP方法: POST
  • 操作类型:使用操作名称
  • 操作: StartExecution
  • 执行角色:角色以开始执行(我们刚刚创建的角色.只需将其粘贴为ARN)
  • 标题:

  • 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 (the one we just created. Just paste it's ARN)
  • Headers:

X-Amz-Target->'AWSStepFunctions.StartExecution'
内容类型->'application/x-amz-json-1.0'

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

身体映射模板/请求有效载荷:

Body Mapping Templates/Request payload:

{
    "input": "string" (optional),
    "name": "string" (optional),
    "stateMachineArn": "string"
}

除正文映射模板外,其他均与2.a中的相同.您要做的就是将其制成字符串.例如,使用$ util.escapeJavascript().它将整个请求的内容作为步功能的输入

Everything is the same as in 2.a, except for the body mapping template. You have to do is make it into a string. Using $util.escapeJavascript(), like this for example. It will pass your whole request's body as an input to your Step Function

    #set($data = $util.escapeJavaScript($input.json('$')))
    {
        "input": "$data",
        "name": "string" (optional),
        "stateMachineArn": "string" (required)
    }


注释

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆