cloudformation 未在 api 网关中附加请求的路径参数

Ham*_*aee 2 amazon-web-services aws-cloudformation aws-api-gateway

我正在尝试在 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
Run Code Online (Sandbox Code Playgroud)

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

RequestParameters:
    method.request.path.users: true
Run Code Online (Sandbox Code Playgroud)

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

支持图片[1]

And*_*rew 11

有两种RequestParameters属性:一种属于方法,一种属于集成。这两个键和值服务略有不同的目的,这可以理解造成混淆。

方法属性的AWS 文档(已添加强调):

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

集成属性的AWS 文档(已添加重点):

API Gateway 随后端请求发送的请求参数。将请求参数指定为键值对(字符串到字符串的映射),目标是键,源是值

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

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

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

因此,为了解释这些键,方法RequestParameter键定义在方法请求中的何处查找值,集成RequestParameter键定义了将值放置在集成请求中的位置。这允许您根据需要将请求参数映射到完全不同的集成参数(例如,将请求路径参数放入集成查询字符串中,将名为 的请求参数更改为名为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
Run Code Online (Sandbox Code Playgroud)