例如,如果我们想用

得到-用户?name =鲍勃

or

获取/用户/鲍勃

如何将这两个例子作为参数传递给Lambda函数?

我在文档中看到了一些关于设置“映射from”的内容,但我在API Gateway控制台中找不到该设置。

method.request.path。在方法请求页面中定义了一个名为parameter-name的路径参数。 method.request.querystring。parameter-name用于在方法请求页面中定义的名为parameter-name的查询字符串参数。

尽管我定义了一个查询字符串,但我没有看到这两个选项。


当前回答

如今,AWS上的API Gateway控制台中包含了一个下拉模板。

对于您的API,单击资源名称…然后

展开“Body Mapping Templates”

输入

application / json

为Content-Type(必须显式地输入),并单击勾

一个新窗口将打开,显示“生成模板”和一个下拉菜单(见图)。

选择

请求传递

然后点击保存

要访问任何变量,只需使用以下语法(这是Python) 例如URL:

https://yourURL.execute-api.us-west-2.amazonaws.com/prod/confirmReg?token=12345&uid=5

可以得到如下变量:

from __future__ import print_function

import boto3
import json

print('Loading function')


def lambda_handler(event, context):
    print(event['params']['querystring']['token'])
    print(event['params']['querystring']['uid'])

因此,不需要显式地命名或映射您想要的每个变量。

其他回答

为了将参数传递给lambda函数,您需要在API Gateway请求和lambda函数之间创建映射。映射在所选API Gateway资源的集成请求->映射模板部分中完成。

创建一个application/json类型的映射,然后在右侧编辑(单击铅笔)模板。

映射模板实际上是一个Velocity模板,你可以使用if,循环,当然还可以打印变量。模板中注入了这些变量,你可以分别访问查询字符串参数、请求头等等。使用下面的代码,您可以重新创建整个查询字符串:

{
    "querystring" : "#foreach($key in $input.params().querystring.keySet())#if($foreach.index > 0)&#end$util.urlEncode($key)=$util.urlEncode($input.params().querystring.get($key))#end",
    "body" : $input.json('$')
}

注意:点击复选符号保存模板。您可以使用资源中的“test”按钮测试更改。但是为了在AWS控制台中测试查询字符串参数,您需要在资源的Method Request部分中定义参数名称。

注意:查看Velocity用户指南以获得关于Velocity模板语言的更多信息。

然后在你的lambda模板中,你可以执行以下操作来解析查询字符串:

var query = require('querystring').parse(event.querystring)
// access parameters with query['foo'] or query.foo

我已经使用这个映射模板为Lambda事件提供了Body、Headers、Method、Path和URL查询字符串参数。我写了一篇博客文章详细解释了这个模板:http://kennbrodhagen.net/2015/12/06/how-to-create-a-request-object-for-your-lambda-event-from-api-gateway/

下面是你可以使用的映射模板:

{
  "method": "$context.httpMethod",
  "body" : $input.json('$'),
  "headers": {
    #foreach($param in $input.params().header.keySet())
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "queryParams": {
    #foreach($param in $input.params().querystring.keySet())
    "$param": "$util.escapeJavaScript($input.params().querystring.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "pathParams": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end

    #end
  }  
}

你可以使用Lambda作为“Lambda代理集成”,参考这个[https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-python],这个Lambda可用的选项是

对于Nodejs Lambda ”事件。头”、“事件。pathParameters”、“事件。身体”、“event.stageVariables”, 和“event.requestContext”

对于Python Lambda 事件['headers']['parametername']等

exports.handler = async (event) => {
    let query = event.queryStringParameters;
    console.log(`id: ${query.id}`);
    const response = {
        statusCode: 200,
        body: "Hi",
    };
    return response;
};

Lambda函数需要JSON输入,因此需要解析查询字符串。解决方案是使用Mapping Template将查询字符串更改为JSON。我用它为c# . net核心,所以预期的输入应该是一个JSON与“queryStringParameters”参数。遵循以下4个步骤来实现这一目标:

打开API Gateway资源的映射模板,添加新的application/json content-tyap:

Copy the template below, which parses the query string into JSON, and paste it into the mapping template: { "queryStringParameters": {#foreach($key in $input.params().querystring.keySet())#if($foreach.index > 0),#end"$key":"$input.params().querystring.get($key)"#end} } In the API Gateway, call your Lambda function and add the following query string (for the example): param1=111&param2=222&param3=333 The mapping template should create the JSON output below, which is the input for your Lambda function. { "queryStringParameters": {"param3":"333","param1":"111","param2":"222"} } You're done. From this point, your Lambda function's logic can use the query string parameters. Good luck!