【问题标题】:CloudFormation Template for API Gateway with Lambda Proxy and MethodResponse Headers带有 Lambda 代理和 MethodResponse 标头的 API 网关的 CloudFormation 模板
【发布时间】:2019-07-15 14:28:36
【问题描述】:

这是一个 CloudFormation 难题,如果没有您的帮助,我想我无法解决。我正在尝试使用 CloudFormation (YAML) 为 REST API 创建模板。 API 有一个 Lambda 代理,并且必须有一个 MethodResponse,如附图所示。

到目前为止,这是我的模板,在构建堆栈时会产生以下错误:

PostMethod:属性 ResponseParameters 的值必须是 对象

      ApiGatewayAccount: 
        Type: "AWS::ApiGateway::Account"
        Properties: 
            CloudWatchRoleArn: "some role"
      RestApi:
          Type: "AWS::ApiGateway::RestApi"
          Properties:
            Description: "some rest api"
            EndpointConfiguration:
              Types:
                - REGIONAL
            Name: RestApi

      SomeResource:
        Type: "AWS::ApiGateway::Resource"
        Properties:
          ParentId: 
            Fn::GetAtt: 
              - "RestApi"
              - "RootResourceId"
          PathPart: part
          RestApiId: 
            Ref: "RestApi"

      SomeSubResource:
        Type: "AWS::ApiGateway::Resource"
        Properties:
          ParentId: 
            Ref: "SomeResource"
          PathPart: count
          RestApiId: 
            Ref: "RestApi"

      SomeResponseModel:
        Type: "AWS::ApiGateway::Model"
        Properties:
          ContentType: "text/html"
          Description: "Empty text/html response."
          Name: someresponse
          RestApiId: 
            !Ref RestApi
          Schema: {}

      PostMethod:
        Type: "AWS::ApiGateway::Method"
        Properties:
          HttpMethod: POST
          Integration:
            IntegrationHttpMethod: POST
            Type: AWS_PROXY
            Uri: 
              Fn::Join:
                - ""
                - - "arn:aws:apigateway:"
                  - "some-region"
                  - ":lambda:path/2015-03-31/functions/"
                  - "some-arn-of-lambda-function"
          MethodResponses:
            -
              ResponseModels:
                Key: "application/x-www.form-urlencoded"
                Value: 
                  !Ref SomeResponse
              ResponseParameters:
                  - method.response.header.Content-Length: true
                  - method.response.header.Content-Type: true
                  - method.response.header.Connection: true
              StatusCode: 200
          OperationName: SomeName
          ResourceId:
            !Ref "SomeSubResource"
          RestApiId: 
            !Ref "RestApi"

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation aws-api-gateway


    【解决方案1】:

    根据documentation,这似乎不应该是键值对的列表,而只是一个对象,其中每个条目是只是另一个键/值。

    我希望以下方法可行:

    ResponseParameters:
      method.response.header.Content-Length: true
      method.response.header.Content-Type: true
      method.response.header.Connection: true
    

    【讨论】:

    • 你是对的 - 谢谢!尽管如此,在纠正这个错误时,模板似乎不正确。我现在得到以下信息: PostMethod:检测到验证错误:'putMethodInput.authorizationType' 处的值为 null 未能满足约束:成员不得为空(服务:AmazonApiGateway;状态代码:400;错误代码:ValidationException;请求 ID ...
    • 查看文档。它说 AuthorizationType 不能为空,我在您的 AWS::ApiGateway::Method 对象中看不到它。 docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…我猜你希望它是AuthorizationType: "NONE"
    • 没错。我必须更改的另一件事是 ResponseModels 的结构,它现在适用于状态代码 200 和 400 的以下行:ResponseModels: "application/x-www.form-urlencoded": !Ref SomeResponse ResponseParameters: method.response.header.Content-Length: true method.response.header.Content-Type: true method.response.header.Connection: true StatusCode: 200
    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2019-07-16
    • 2021-11-17
    • 1970-01-01
    • 2018-04-24
    • 2020-02-10
    相关资源
    最近更新 更多