【问题标题】:cloudformation does not attach the requested path param in api gatewaycloudformation 未在 api 网关中附加请求的路径参数
【发布时间】:2018-07-24 00:44:20
【问题描述】:

我正在尝试在 cloudformation 中创建 api 网关。一切都很好,除了当我指定路径参数 url 时,我可以在创建的 api 网关中看到它。这是我的cfn代码:

GetMethod:
Type: AWS::ApiGateway::Method
Properties:
  AuthorizationType: NONE
  HttpMethod: GET
  Integration:
    Type: HTTP
    IntegrationHttpMethod: GET
    Uri:
      Fn::Join:
      - ''
      - - "http://"
        - Fn::ImportValue: !Sub ${project}-${EnvironmentApp}-bn-user-endpoint-url
        - "/users"
        - "/{users}"
    IntegrationResponses:
    - StatusCode: 200
      ResponseParameters:
        method.response.header.Access-Control-Allow-Origin: "'*'"
      ResponseTemplates:
        application/json: ''
    RequestTemplates:
      application/json: ''
  RequestParameters:
    method.request.path.users: true
  ResourceId: !Ref UsersPathParam
  RestApiId:
    Ref: RestApi
  MethodResponses:
  - StatusCode: 200
    ResponseParameters:
        method.response.header.Access-Control-Allow-Origin: true

如果您在上面的代码中注意到,我特别要求提供名为 users 的路径参数:

RequestParameters:
    method.request.path.users: true

此外,您还可以在所附图像中看到创建的 API 网关没有设置路径参数。 有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    RequestParameters属性有两个:一个属于方法,一个属于集成。 key 和 value 的用途略有不同,这可能会造成混淆。

    AWS documentation 用于方法属性(已添加重点):

    API Gateway 接受的请求参数。将请求参数指定为键值对(字符串到布尔映射),以源作为键,以布尔作为值布尔值指定是否需要参数。 源必须匹配格式 method.request.location.name,其中位置是查询字符串、路径或标头,名称是有效的唯一参数名称.

    AWS documentation 用于集成属性(已添加重点):

    API Gateway 与后端请求一起发送的请求参数。将请求参数指定为键值对(字符串到字符串的映射),以目的地为键,以源为值

    使用以下模式 integration.request.location.name 指定目标,其中 location 是查询字符串、路径或标头,name 是有效的唯一参数名称。

    来源必须是现有的方法请求参数或静态值。您必须将静态值括在单引号中,并根据请求中的目标对这些值进行预编码。

    因此,当前接受的答案在技术上是有效的,但可能会导致将静态值 true 发送到集成参数,而不是将方法参数值传递给集成。您更有可能希望提供对方法参数的引用。

    所以,为了解释键,方法RequestParameter key 定义了在哪里找到值 在方法请求中,集成RequestParameter key 定义了在哪里放置值 在集成请求中。如果您愿意,这允许您将请求参数映射到完全不同的集成参数(例如,将请求路径参数放入集成查询字符串中,将名为 foo 的请求参数更改为名为 bar 的集成参数等)

    您也可能需要也可能不要求方法参数存在,因此将方法参数布尔值设置为truefalse,具体取决于您是否要强制该值必须包含在方法请求中:

    GetMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        RestApiId: !Ref RestApi
        ResourceId: !Ref Resource
        AuthorizationType: NONE
        HttpMethod: GET
        RequestParameters:
          - method.request.path.foo: true|false
        Integration:
          Type: HTTP
          IntegrationHttpMethod: GET
          Uri: https://example.com
          RequestParameters:
            - integration.request.path.foo: method.request.path.foo
    

    【讨论】:

    【解决方案2】:

    由于需要在API的“集成请求”部分应用,所以必须在参数前加上integration.这样:

    RequestParameters:
      integration.method.request.path.users: "'true'"
    

    另外,请注意单引号,我必须添加它们才能在其中添加字符串文字,但 YMMV。

    编辑: 看起来您的 RequestParameters 没有在正确的位置缩进。它应该在Integration: 之下,因为这是您要在该级别添加的内容。

    编辑 2: 我已经使用这种方法进行了测试,并且效果很好:

    ProxyMethod:
      Type: AWS::ApiGateway::Method
      Properties:
        ResourceId: !Ref ProxyResource
        RestApiId: !Ref RestApi
        AuthorizationType: AWS_IAM
        HttpMethod: ANY
        RequestParameters:
          method.request.path.proxy: true
        Integration:
          IntegrationHttpMethod: ANY
          Type: HTTP_PROXY
          Uri: !Sub ${BaseUrl}/{proxy}
          RequestParameters:
            integration.request.path.user: "'true'"
    

    【讨论】:

    • 非常感谢您的回答。当我添加集成 cfn 失败并出现错误:指定的映射表达式无效:验证结果:警告:[],错误:[指定的映射表达式无效:integration.method.request.path.users]
    • 我在某处工作。我会更新一个更完整的答案。很抱歉造成混乱
    • 感谢您的帮助。我是这样添加的:Integration: Type: HTTP RequestParameters: integration.method.request.path.users: "'true'" IntegrationHttpMethod: GET 但仍然是同样的错误。我是按照你的意思添加的吗?
    • 您能否确保您的模板遵循与我的代码示例相同的结构?如果您仍有问题,请告诉我。
    • Woooowthanx 很多人。这篇文章价值100000个赞。你太棒了:)
    猜你喜欢
    • 2020-07-15
    • 2016-03-24
    • 2012-04-11
    • 1970-01-01
    • 2019-09-06
    • 2020-01-14
    • 2021-09-09
    • 2012-07-20
    • 2016-02-11
    相关资源
    最近更新 更多