我希望我的API的根路径完全重定向(301)到带有文档的另一个站点。因此,我在例如/function1
path处有一个lambda,/
应该在另一个位置返回代码301。而且我想没有另一个lambda。
这正是此处描述的内容,但是通过aws命令行工具进行了描述。我尝试了这种方法-效果很好,但是我想通过无服务器框架配置来配置此类API网关模拟。
幸运的是,您链接到的一系列CLI命令可以在CloudFormation中复制,然后可以将其放入无服务器模板的Resources
部分中。
在此示例中,GET
to/function1
将调用lambda函数,而GET
to/
将301返回到众所周知的搜索引擎。
service: sls-301-mock
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: us-east-1
functions:
hello:
handler: handler.hello
events:
- http:
path: function1
method: get
resources:
Resources:
Method:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: GET
ResourceId:
!GetAtt ApiGatewayRestApi.RootResourceId
RestApiId:
Ref: ApiGatewayRestApi
AuthorizationType: NONE
MethodResponses:
- ResponseModels: {"application/json":"Empty"}
StatusCode: 301
ResponseParameters:
"method.response.header.Location": true
Integration:
Type: MOCK
RequestTemplates:
"application/json": "{\n \"statusCode\": 301\n}"
IntegrationResponses:
- StatusCode: 301
ResponseParameters:
"method.response.header.Location": "'https://google.com'"
经过测试:
Framework Core: 1.62.0
Plugin: 3.3.0
SDK: 2.3.0
Components Core: 1.1.2
Components CLI: 1.4.0
笔记
ApiGatewayRestApi
按照约定,是由Serverless根据http
事件创建的API网关阶段资源的逻辑名称。
相关CloudFormation文档
编辑
这个答案并不那么冗长,http
而是使用一个事件而不是本Resources
节。我尚未测试过,但它可能也对您有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句