如何在没有 lambda 的无服务器框架配置(适用于 AWS)中实现重定向(301 代码)模拟
How to implement redirect (301 code) mock in serverless framework config (for AWS) without lambda
我希望我的 API 的根路径重定向 (301) 到另一个带有文档的站点。所以我有一个 lambda 在例如 /function1
路径和 /
应该 return 代码 301 与另一个位置。我想在没有另一个 lambda 的情况下完成它。
这正是 described here,但通过 aws 命令行工具。我尝试了这种方法 - 它工作得很好,但我想通过无服务器框架配置来配置这样的 API 网关模拟。
幸运的是,您链接到的一系列 CLI 命令可以在 CloudFormation 中复制,然后可以将其放入 Serverless 模板的 Resources
部分。
在此示例中,GET
到 /function1
将调用 lambda 函数,而 GET
到 /
将 return 301 到 a知名搜索引擎。
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 Gateway Stage 资源的逻辑名称事件。
相关 CloudFormation 文档
ApiGateway::Method Integration
编辑
没有那么冗长,它使用 http
事件而不是 Resources
部分。我还没有测试过,但它也可能对你有用。
通过引用一个函数两次来设法实现它。但另请参阅 Mike Patrick 的回复 - 看起来更通用
...
events:
- http:
path: main/root/to/the/function
method: get
cors: true
- http:
path: /
method: get
integration: mock
request:
template:
application/json: '{"statusCode": 301}'
response:
template: redirect
headers:
Location: "'my-redirect-url-note-the-quotes-they-are-important'"
statusCodes:
301:
pattern: ''
我希望我的 API 的根路径重定向 (301) 到另一个带有文档的站点。所以我有一个 lambda 在例如 /function1
路径和 /
应该 return 代码 301 与另一个位置。我想在没有另一个 lambda 的情况下完成它。
这正是 described here,但通过 aws 命令行工具。我尝试了这种方法 - 它工作得很好,但我想通过无服务器框架配置来配置这样的 API 网关模拟。
幸运的是,您链接到的一系列 CLI 命令可以在 CloudFormation 中复制,然后可以将其放入 Serverless 模板的 Resources
部分。
在此示例中,GET
到 /function1
将调用 lambda 函数,而 GET
到 /
将 return 301 到 a知名搜索引擎。
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 Gateway Stage 资源的逻辑名称事件。
相关 CloudFormation 文档
ApiGateway::Method Integration
编辑
http
事件而不是 Resources
部分。我还没有测试过,但它也可能对你有用。
通过引用一个函数两次来设法实现它。但另请参阅 Mike Patrick 的回复 - 看起来更通用
...
events:
- http:
path: main/root/to/the/function
method: get
cors: true
- http:
path: /
method: get
integration: mock
request:
template:
application/json: '{"statusCode": 301}'
response:
template: redirect
headers:
Location: "'my-redirect-url-note-the-quotes-they-are-important'"
statusCodes:
301:
pattern: ''