【问题标题】:Create an API Gateway Proxy Resource using SAM使用 SAM 创建 API Gateway 代理资源
【发布时间】:2019-06-20 11:44:35
【问题描述】:
我有一个显然很简单的要求,即在 SAM(亚马逊的无服务器应用程序模型)中创建代理资源。事实上如此简单,以至于亚马逊的文档似乎将此作为exercise 留给读者!
我想创建一个 AWS API Gateway 包罗万象的端点,以将所有内容代理到另一个 HTTP 服务。
在 AWS 控制台中,我尝试构建的配置如下所示:
我看到了 Christian Johansen 撰写的出色的 post 和他的相关 Stack Overflow 问题 here,了解如何在 CloudFormation 中做同样的事情。我想我可以在 SAM 中使用该代码,但是,SAM 有其隐式和显式 API,所以如果“正确的方法”是引用隐式资源,我想避免创建显式资源。
有人知道怎么做吗?
【问题讨论】:
标签:
amazon-web-services
aws-serverless
aws-sam
serverless-application-model
【解决方案1】:
在玩了很多之后,我相信我在一个例子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