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

别说谁变了你拦得住时间么 提交于 2020-01-01 03:57:04

问题


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

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}

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?


回答1:


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.

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')"
}

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.




回答2:


All the information can be retrieved from event object.

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



来源:https://stackoverflow.com/questions/34386869/how-do-i-pass-arguments-to-aws-lambda-functions-using-get-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!