如何使用GET请求将参数传递给AWS Lambda函数? [英] How do I pass arguments to AWS Lambda functions using GET requests?

查看:372
本文介绍了如何使用GET请求将参数传递给AWS Lambda函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说从Api网关端点向Lambda函数发出GET请求时,我想在URL字符串中传递val1和val2:

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:

https://xyz.execute-api.amazonaws.com/prod/test?val1=5&val2=10

我有一个简单的函数,将两个输入val1和val2相加:

And I have a simple function that sums the two inputs, val1 and val2:

def lambda_handler(event, context):
    # How do I get at val1 and val2??
    return {'result': val1 + val2}

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

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?

推荐答案

在API网关的方法请求部分中定义了查询字符串参数后,您需要在方法执行部分.

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.

方法执行部分中,选择映射模板,然后单击添加映射模板.输入application/json作为 Content Type ,然后创建一个如下所示的映射模板:

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:

{
    "va1": "$input.params('val1')",
    "val2": "$input.params('val2')"
}

这将告诉API Gateway接受称为val1val2的输入参数(在路径上,在标头中或在查询参数中传递),并将它们作为val1val2.

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.

这篇关于如何使用GET请求将参数传递给AWS Lambda函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆