如何将 Post 参数从 API 网关传递到 Lambda
How to pass Post Parameter from API gateway to Lambda
我正在创建一个用于学习无服务器框架的演示项目。
我有一个 Lambda 函数通过以下方式从事件对象中输入两个参数:
operandOne = event["operandOne"]
operandTwo = event["operandTwo"]
我创建了一个 API 网关,它使用此参数调用此 lambda 函数。
我一直在关注这个 link 直到现在我的答案,但仍然没有想出如何实施。 -
上面给出的答案有三种传递参数的方法,我对第三种方法感兴趣。 "3) 直接在事件对象上"
这可以借助 AWS 控制台的映射模板来完成。但是我正在尝试找到一种方法来使用云形成(在yaml文件中编写映射模板)用于相同的学习目的。
But I am trying to find a way to use cloud formation(Writing mapping template in yaml file) for the same purpose for learning.
为此,您必须在 AWS::ApiGateway::Method
的 RequestTemplates 中提供您的模板:
A map of Apache Velocity templates that are applied on the request payload. The template that API Gateway uses is based on the value of the Content-Type header that's sent by the client.
您提供的 link 中的一个通用示例是:
RequestTemplates:
application/json: {"hello": $input.params('$hello')}
当您有来自 API 网关的 post 请求连接到 Lambda 函数时,您可以通过 event.body
访问事件对象中的所有正文参数。如果正文是使用 JSON 进行字符串化的,您可以这样做:
const body = JSON.parse(event.body)
const operandOne = body.operandOne
const operandTwo = body.operandTwo
不需要任何模板
我对 API 网关的输入如下:
{
operandOne: 2
operandTwo: 3
}
API 网关需要一个正文映射模板来为 lambda 函数映射此参数并将它们发送到事件对象中。可以有两种方式
- 使用 AWS 控制台
- 使用云形成模板
我想使用第二种方法。
这是我在cloud_formation.template.yml文件
中写的
APIGatewayDefinition:
Type: 'AWS::Serverless::Api'
Properties:
basePath: /
paths:
/insert:
post:
summary:
description:
consumes:
- application/json
produces:
- application/json
x-amazon-apigateway-integration:
requestTemplates:
application/json: "#set ($root=$input.path('$')) { \"operandOne\": \"$root.operandOne\", \"operandOne\": \"$root.operandOne\" }"
type: aws
....
然后我可以直接从事件对象而不是从事件对象中的主体访问 Lambda 中的两个变量。
operandOne = event["operandOne"]
operandTwo = event["operandTwo"]
供参考:
我正在创建一个用于学习无服务器框架的演示项目。
我有一个 Lambda 函数通过以下方式从事件对象中输入两个参数:
operandOne = event["operandOne"]
operandTwo = event["operandTwo"]
我创建了一个 API 网关,它使用此参数调用此 lambda 函数。
我一直在关注这个 link 直到现在我的答案,但仍然没有想出如何实施。 -
上面给出的答案有三种传递参数的方法,我对第三种方法感兴趣。 "3) 直接在事件对象上"
这可以借助 AWS 控制台的映射模板来完成。但是我正在尝试找到一种方法来使用云形成(在yaml文件中编写映射模板)用于相同的学习目的。
But I am trying to find a way to use cloud formation(Writing mapping template in yaml file) for the same purpose for learning.
为此,您必须在 AWS::ApiGateway::Method
的 RequestTemplates 中提供您的模板:
A map of Apache Velocity templates that are applied on the request payload. The template that API Gateway uses is based on the value of the Content-Type header that's sent by the client.
您提供的 link 中的一个通用示例是:
RequestTemplates:
application/json: {"hello": $input.params('$hello')}
当您有来自 API 网关的 post 请求连接到 Lambda 函数时,您可以通过 event.body
访问事件对象中的所有正文参数。如果正文是使用 JSON 进行字符串化的,您可以这样做:
const body = JSON.parse(event.body)
const operandOne = body.operandOne
const operandTwo = body.operandTwo
不需要任何模板
我对 API 网关的输入如下:
{
operandOne: 2
operandTwo: 3
}
API 网关需要一个正文映射模板来为 lambda 函数映射此参数并将它们发送到事件对象中。可以有两种方式
- 使用 AWS 控制台
- 使用云形成模板
我想使用第二种方法。
这是我在cloud_formation.template.yml文件
中写的APIGatewayDefinition:
Type: 'AWS::Serverless::Api'
Properties:
basePath: /
paths:
/insert:
post:
summary:
description:
consumes:
- application/json
produces:
- application/json
x-amazon-apigateway-integration:
requestTemplates:
application/json: "#set ($root=$input.path('$')) { \"operandOne\": \"$root.operandOne\", \"operandOne\": \"$root.operandOne\" }"
type: aws
....
然后我可以直接从事件对象而不是从事件对象中的主体访问 Lambda 中的两个变量。
operandOne = event["operandOne"]
operandTwo = event["operandTwo"]
供参考: