[英]How do I pass arguments to AWS Lambda functions using GET requests?
Say I want to pass val1 and val2 in the URL string when making a GET request from my Api gateway endpoint to my Lambda function:假设我想在从我的 Api 网关端点向我的 Lambda 函数发出 GET 请求时在 URL 字符串中传递 val1 和 val2:
https://xyz.execute-api.amazonaws.com/prod/test?val1=5&val2=10
And I have a simple function that sums the two inputs, val1 and val2:我有一个简单的函数,可以对两个输入 val1 和 val2 求和:
def lambda_handler(event, context):
# How do I get at val1 and val2??
return {'result': val1 + val2}
I've added val1 and val2 to URL Query String Parameters on the Method Request on the AWS API Gateway.我已将 val1 和 val2 添加到 AWS API 网关上方法请求的 URL 查询字符串参数中。 But how do I access them inside the function?
但是我如何在函数内部访问它们呢?
After defining the query string parameters in Method Request section of the API Gateway, you then need to define a Mapping Template in the Method Execution section.在 API 网关的方法请求部分定义查询字符串参数后,您需要在方法执行部分定义一个映射模板。
In the Method Execution section, select Mapping Templates and then click Add Mapping Template .在“方法执行”部分中,选择“映射模板”,然后单击“添加映射模板”。 Enter
application/json
for the Content Type and then create a mapping template that looks something like this:输入
application/json
作为Content Type ,然后创建一个如下所示的映射模板:
{
"va1": "$input.params('val1')",
"val2": "$input.params('val2')"
}
This will tell API Gateway to take the input parameters (either passed on the path, or in headers, or in query parameters) called val1
and val2
and send them to the Lambda function in the event data as val1
and val2
.这将告诉 API 网关获取名为
val1
和val2
的输入参数(通过路径、标头或查询参数传递),并将它们作为val1
和val2
发送到事件数据中的 Lambda 函数。
All the information can be retrieved from Event
object.所有信息都可以从
Event
对象中检索。
For example: The value of a variable foo can be retrieved from the event as: event["foo"]
.例如:变量 foo 的值可以从事件中检索为:
event["foo"]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.