首页 > 解决方案 > 在 / 使用 CF 为 API 网关创建方法

问题描述

如何使用 CF 在 API Gateway 的根/文件夹下创建方法?例如,我有一个如下所示的网关:

/ 选项发布

在此处输入图像描述

但是,当尝试使用 CF 执行此操作时,我得到:资源的路径部分只允许 a-zA-Z0-9._- 和开头和结尾的花括号。所以我的 PathPart 是有问题的线。

  ApiGate:
    Type: AWS::ApiGateway::Resource
    Properties:
      ParentId: !GetAtt 
        - ApiGateApi
        - RootResourceId
      PathPart: '{/}'
      RestApiId: !Ref ApiGateApi

我可以将 PathPart 更改为其他内容,但随后它将它创建为 / 下的子对象,这是我不想要的。

在此处输入图像描述

标签: amazon-cloudformationaws-api-gateway

解决方案


这解决了我的问题,在我的情况下,我需要 2 种方法:1. 将响应对 root 的请求,例如https://<api-url>/prodhttps://<api-url>/prod/. 这将使用 API Gateway 的 RootResourceId:

ResourceId: !GetAtt myApiGateway.RootResourceId

  1. 这将响应任何已设置的请求https://<api-url>/prod/。可能是petstore或者如果使用,{proxy+}那么后端工作负载将尝试解决请求。它将引用模板中定义的资源类型:

ResourceId: !Ref myResource


推荐阅读