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

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

本文介绍了使用SAM创建API网关代理资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SAM(亚马逊的无服务器应用程序模型)中创建代理资源的要求很简单。实际上如此简单,以至于亚马逊的文档似乎将其保留为



我见过Christian Johansen撰写的这篇出色的及其相关的Stack Overflow问题。相对于SAM开箱即用的Hello World示例的差异是:

  diff --git a / sam-app / template.yaml b / sam-app / template.yaml 
索引02cd901..f349dcc 100644
--- a / sam-app / template.yaml
+++ b /sam-app/template.yaml
@@ -17,11 +17,11 @@资源:
变量:
参数1:值
事件:
- HelloWorld:
+ ProxyApiGreedy:
类型:Api
属性:
-路径:/ hello
-方法:获取
+路径:/ {proxy +}
+方法:任何
输出:
HelloWorldApi:
描述:用于Hello World函数
的Prod阶段的API网关端点URL pre>

换句话说,像这样:

 资源:
MyFunction:
类型:AWS :: Serverless :: Function
属性:
...
事件:
ProxyApiGreedy:
类型:Api
属性:
路径:/ {proxy +}
方法:任意


I have an apparently-simple requirement to create a proxy resource in SAM (Amazon's Serverless Application Model). So simple in fact that Amazon's documentation appears to leave this as an exercise to the reader!

I want to create an AWS API Gateway catch-all endpoint to proxy everything to another HTTP service.

In the AWS Console, the configuration I am trying to build looks as follows:

I have seen this excellent post by Christian Johansen and his related Stack Overflow question here for how to do the same thing in CloudFormation. I suppose I could just use that code in SAM, however, SAM has its implicit and explicit APIs, so I want to avoid creating explicit resources if the "right way" is to refer to implicit resources.

Does anyone know how to do this?

解决方案

After a lot of playing around, I believe I found the answer in an example here. The diff relative to the Hello World example that SAM sets up for you out of the box is:

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

In other words, like this:

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

这篇关于使用SAM创建API网关代理资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:47