如何将 APIGateway 方法设置为 return 来自步进函数的值?

How can I set up an APIGateway method to return a value from a step function?

我有一个由几个 lambda 组成的状态机,我使用 Cloudformation 模板设置了这些 lambda,该模板进行一些处理并最终将文件保存到 S3;除非有要求,否则我不会在这里添加它,因为它可以正常工作。 step 函数的最后一部分是一个 lambda,它输出以下内容:

{
   "output": {
      "statusCode": "200",
      "body": "{\"dataset-name\": \"inserted\"}",
      "headers": {
           "Content-Type": "application/json"
       }
    }
}

这是 cloudformation 模板,我必须设置一个调用此步骤函数的 API 方法:

PostDatasetMethod:
Type: AWS::ApiGateway::Method
DependsOn: Resource
Properties:
  ApiKeyRequired: false
  AuthorizationType: NONE
  HttpMethod: POST
  Integration:
    Credentials: !Ref 'IamRoleArn'
    IntegrationHttpMethod: POST
    PassthroughBehavior: NEVER
    Type: AWS
    Uri: 'arn:aws:apigateway:us-east-1:states:action/StartExecution'
    IntegrationResponses:
      - StatusCode: 200
    RequestTemplates:
      application/json: !Sub |
        {
          "input": "$util.escapeJavaScript($input.json('$'))",
          "stateMachineArn": "${StateMachine}"
        }
  ResourceId: !Ref 'Resource'
  RestApiId: !Ref 'Api'

但是,我遇到了错误

Execution failed due to configuration error: No match for output mapping and no default output mapping configured

当我调用 API 时,即使步骤函数成功完成,我认为是因为我不确定如何在 CloudFormation 模板中设置方法和集成响应。我只需要按原样传递上面的 json,类似于 lambda 的默认情况。为了让 API 按预期工作,我还需要在 CloudFormation 模板中做什么?

Step Functions 是异步调用的,没有自然 方法来进行同步调用。这意味着您无法使 API 网关方法 return 成为 Step Functions 的输出。

有人提出了各种方法来实现这一点,也许讨论最多的是这个: 创建一个 lambda 函数,其唯一目的是启动 Step Functions 的执行,然后开始轮询 Step Functions 的结果。

Here's some info on getting Step Functions' 结果。请注意,由于 API 网关的执行限制,这仅在 Step Functions 的持续时间少于 30 秒时才有效。