AWS SAM 模板 - 设置集成响应映射模板

AWS SAM template - set Integration response mapping template

我有一个 AWS SAM 模板,它创建一个连接到 Step Function 的 API 网关。

一切正常,但我需要在 Step Functions 返回的响应中添加 Integration Response Mapping Template

我看不出这可以用 SAM 模板实现吗?

我为它找到了相关的 Cloud Formation 模板:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apitgateway-method-integration-integrationresponse.html

但看起来我必须创建整个 AWS::ApiGateway::Method / Integration / IntegrationResponses 链 - 然后我不确定你如何从其他部分引用它SAM 模板。

我读到可以用 openAPI / Swagger 定义来完成 - 这是唯一的方法吗?或者是否有更简洁的方法来简单地添加此模板?

这是我刚才要演示的内容的简化版本...

Transform: AWS::Serverless-2016-10-31
Description: My SAM Template
Resources: 

  MyAPIGateway:
    Type: AWS::Serverless::Api
    Properties:
      Name: my-api
      StageName: beta
      Auth:
        ApiKeyRequired: true 
        UsagePlan: 
          CreateUsagePlan: PER_API
          UsagePlanName: my-usage-plan
          Quota:
            Limit: 1000
            Period: DAY
          Throttle:
            BurstLimit: 1000
            RateLimit: 1000
      
  MyStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      Name: my-state-machine
      DefinitionUri: statemachines/my-state-machine.asl.json
      Events:
        MyEvent:
          Type: Api
          Properties:
            Path: /myApiMethod
            Method: post
            RestApiId: !Ref MyAPIGateway

          # TODO: how to we define this Integration Response Template ?
          # IntegrationResponse:
          #   Template:
          #     application/json: |
          #         ## parse arn:aws:states:REGION:ACCOUNT:execution:STATE_MACHINE:EXECUTION_NAME
          #         ## to get just the name at the end
          #         #set($executionArn = $input.json('$.executionArn'))
          #         #set($arnTokens = $executionArn.split(':'))
          #         #set($lastIndex = $arnTokens.size() - 1)
          #         #set($executionId = $arnTokens[$lastIndex].replace('"',''))
          #         {
          #           "execution_id" : "$executionId",
          #           "request_id" : "$context.requestId",
          #           "request_start_time" : "$context.requestTimeEpoch"
          #         }
  
    

现在您正在状态机中使用 AWS SAM 事件为您构建 API,这是构建 API 的一种非常简单的方法。但是,API 的某些方面不能以这种方式构建。

您仍然可以使用 AWS SAM,但是在使用 DefinitionBody attribute of the AWS::Serverless::Api (or the DefinitionUri). This allows you to specify the API using the OpenAPI specification with the OpenAPI extensions.

时构建具有所有高级功能的 API

您仍然需要在 StateMachine 中定义事件,因为这还将确保为您的 API 配置正确的权限以调用您的其他服务。如果您不指定事件,则必须自行修复权限。