gpt4 book ai didi

amazon-web-services - 如何使用 API Gateway 调用 AWS Step Function

转载 作者:行者123 更新时间:2023-12-01 08:23:54 25 4
gpt4 key购买 nike

我如何使用 API Gateway POST 请求和请求的 JSON 负载到 Step Function 来调用 AWS Step Function ?

最佳答案

1. 创建你的阶梯函数

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

否则,您可以查看此处的文档: What is AWS Step Functions?

2. 为您的 API 创建 IAM 角色

它可以用于所有 Step Functions,也可以仅用于这一个。我们将只介绍第一种情况,如亚马逊教程中所述: Creating an API Using API Gateway

To create the IAM role

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

  • On the Roles page, choose Create New Role.

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

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

  • On the Attach Policy page, choose Next Step.

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

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

  • Choose Create Role.

To attach a policy to the IAM role

  • 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 在 How can i call AWS Step Functions by API Gateway? 中解释的那样,您可以通过 API 网关控制台创建 AWS 服务集成,如下所示:
  • 集成类型: AWS 服务
  • AWS 服务: Step Functions
  • HTTP 方法: POST
  • Action 类型: 使用 Action 名称
  • 操作: 开始执行
  • 执行角色: 开始执行的角色 (我们刚刚创建的那个。粘贴它的ARN)
  • 标题:

    X-Amz-Target -> 'AWSStepFunctions.StartExecution'
    内容类型 -> 'application/x-amz-json-1.0'
  • 正文映射模板/请求有效负载:
    {
    "input": "string" (optional),
    "name": "string" (optional),
    "stateMachineArn": "string"
    }


  • 3.b 如果你有 JSON 有效负载作为输入传递

    除了主体映射模板之外,一切都与 2.a 中的相同。你要做的就是把它变成一个字符串。使用 $util.escapeJavascript(),例如这样。它将您的整个请求正文作为输入传递给您的 Step Function
        #set($data = $util.escapeJavaScript($input.json('$')))
    {
    "input": "$data",
    "name": "string" (optional),
    "stateMachineArn": "string" (required)
    }

    笔记
  • stateMachineArn :如果您不想将 stateMachineArn 作为请求的一部分传递给 API Gateway,您可以简单地将其硬编码到您的正文映射模板中(请参阅 AWS API Gateway with Step Function )
  • name :省略 name 属性将使 API Gateway 在每次执行时为您生成不同的属性。

  • 现在,这是我的第一个“回答你自己的问题”,所以也许这不是它的完成方式,但我确实花了很多时间试图了解我的映射模板出了什么问题。希望这将有助于节省其他人的头发和时间。

    关于amazon-web-services - 如何使用 API Gateway 调用 AWS Step Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42914487/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com