Python 如何使用 GET 请求将参数传递给 AWS Lambda 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34386869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:55:48  来源:igfitidea点击:

How do I pass arguments to AWS Lambda functions using GET requests?

pythonamazon-web-servicesaws-lambdaaws-api-gateway

提问by capitalistcuttle

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. But how do I access them inside the function?

我已将 val1 和 val2 添加到 AWS API Gateway 上方法请求的 URL 查询字符串参数中。但是如何在函数内部访问它们呢?

回答by garnaat

After defining the query string parameters in Method Requestsection of the API Gateway, you then need to define a Mapping Template in the Method Executionsection.

在API Gateway 的Method Request部分定义查询字符串参数后,您需要在Method Execution部分定义一个映射模板。

In the Method Executionsection, select Mapping Templatesand then click Add Mapping Template. Enter application/jsonfor the Content Typeand then create a mapping template that looks something like this:

方法执行部分,选择映射模板,然后单击添加映射模板。输入application/json内容类型,然后创建一个看起来是这样的一个映射模板:

{
    "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 val1and val2and send them to the Lambda function in the event data as val1and val2.

这将告诉API网关采取所谓的输入参数(无论是通过的道路上,还是在头部,或查询参数)val1,并val2和它们发送到lambda函数在事件数据val1val2

回答by Batman

All the information can be retrieved from event object.

所有信息都可以从事件对象中检索。

For example : val1 can be retrieved as event["value1"] etc.

例如:可以将 val1 检索为 event["value1"] 等。