简体   繁体   English

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

[英]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 网关获取名为val1val2的输入参数(通过路径、标头或查询参数传递),并将它们作为val1val2发送到事件数据中的 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.

相关问题 如何使用 AWS CDK 中的 InvokeLambda 将 JSON 传递给 AWS StepFunction 中的 lambda - How do I pass JSON to lambda in AWS StepFunction using InvokeLambda in AWS CDK 如何在代理和 API 网关后面将 API 请求 (AWS SigV4) 签名到 Lambda? - How do I sign API requests (AWS SigV4) to Lambda behind Proxy & API Gateway? 如何使用 AWS Lambda 安装 Git? - How do I install Git using AWS Lambda? 如何使用 Rust 在 lambda 中序列化 AWS HTTP 请求 - How do I serialize an AWS HTTP request in a lambda using Rust 如何使用 Lambda 批量加载 AWS Neptune? - How do i Bulk Load AWS Neptune using a Lambda? 如何使用 lerna 将 monorepo 代码部署到 AWS Lambda? - How do I deploy monorepo code to AWS Lambda using lerna? AWS Lambda 函数如何在执行期间获取依赖项 - How AWS Lambda functions get the dependencies during execution 如何获取在 s3 中上传的最新 object 名称并使用 AWS Lambda 将该数据(json)推送到不同的 api(点击 api)? - How do I get the latest object name uploaded in s3 and push that data(json) to a different api (hit an api) using AWS Lambda? AWS Lambda function 如何得到并行函数的结果? - How can the AWS Lambda function get the results of parallel functions? 如何使用 aws cli 获取现有的 aws lambda 源代码 - how to get a existent aws lambda source code using aws cli
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM