使用 SAM 创建 API 网关代理资源

Create an API Gateway Proxy Resource using SAM

我有一个看似简单的要求,即在 SAM(亚马逊的无服务器应用程序模型)中创建代理资源。事实上如此简单,亚马逊的文档似乎将其作为 exercise 留给了 reader!

我想创建一个 AWS API 网关 catch-all 端点来将所有内容代理到另一个 HTTP 服务。

在 AWS 控制台中,我尝试构建的配置如下所示:

我已经看到关于如何在 CloudFormation 中做同样事情的精彩 post by Christian Johansen and his related Stack Overflow question here。我想我可以在 SAM 中使用该代码,但是,SAM 有其隐式和显式 APIs,所以如果 "right way" 是指隐式资源,我想避免创建显式资源。

有人知道怎么做吗?

经过大量尝试,我相信我在示例中找到了答案here。与 SAM 为您设置的开箱即用的 Hello World 示例相关的差异是:

diff --git a/sam-app/template.yaml b/sam-app/template.yaml
index 02cd901..f349dcc 100644
--- a/sam-app/template.yaml
+++ b/sam-app/template.yaml
@@ -17,11 +17,11 @@ Resources:
         Variables:
           PARAM1: VALUE
       Events:
-        HelloWorld:
+        ProxyApiGreedy:
           Type: Api
           Properties:
-            Path: /hello
-            Method: get
+            Path: /{proxy+}
+            Method: ANY
 Outputs:
   HelloWorldApi:
     Description: API Gateway endpoint URL for Prod stage for Hello World function

换句话说,像这样:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      ...
      Events:
        ProxyApiGreedy:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY