[英]How to pass parameters to a Lambda function using API gateway for querying items from DynamoDB?
I have a Lambda function to query data from DynamoDB table. 我有一个Lambda函数,用于从DynamoDB表查询数据。 The Lambda function is as follows:
Lambda函数如下:
'use strict';
var AWS = require('aws-sdk'),
documentClient = new AWS.DynamoDB.DocumentClient();
exports.listItems = function(event, context, callback){
var params = {
TableName : event.tablename,
IndexName : "active_flag-index",
KeyConditionExpression: "#active = :active",
FilterExpression: "#deliverable = :deliverable and #type = :type",
ProjectionExpression: "#name, price, item_description, item_type",
ExpressionAttributeNames:{
"#active": "active_flag",
"#deliverable": "deliverable_flag",
"#name": "name",
"#type": "item_type"
},
ExpressionAttributeValues: {
":active": "active",
":deliverable": "deliverable",
":type": event.type
}
};
documentClient.query(params, function(err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log("Query succeeded.");
data.Items.forEach(function(item) {
console.log(" -", item.name + ": " + item.price);
});
}
});
}
The test parameters are { "tablename": "vijayarams_items", "type": "Main Dish" } Using this test parameters, the items corresponding to Main Dish are retrieved successfully. 测试参数为{“ tablename”:“ vijayarams_items”,“ type”:“ Main Dish”}使用此测试参数,可以成功检索与Main Dish相对应的项目。 Now, I'm unsure how to pass these parameters using API to invoke this Lambda function.
现在,我不确定如何使用API传递这些参数来调用此Lambda函数。 I've created an API with GET method but the GET method doesn't use request Body to send parameters.
我已经使用GET方法创建了API,但是GET方法未使用请求正文发送参数。 Please enlighten me on how to proceed further.
请启发我进一步进行操作。 I'm able to create table, update items using POST method using AJAX and passing parameters to body.
我能够创建表,使用AJAX的POST方法更新项目并将参数传递给正文。 I'm just unable to query items from the table using the set parameters.
我只是无法使用设置的参数从表中查询项目。
Typically, a REST API will pass parameters through the Query string, such as : 通常,REST API将通过查询字符串传递参数,例如:
GET /resources?param1=value1¶m2=value2
. GET /resources?param1=value1¶m2=value2
。
You can define your parameters at API Gateway Level as described here : https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-settings-method-request.html#setup-method-request-parameters 您可以按照以下说明在API网关级别定义参数: https : //docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-method-settings-method-request.html#setup-method-request -参数
Then in your Lambda code, you need to read the values passed by the API Gateway in the incoming request and use them to build your DynamoDB params
object. 然后,在Lambda代码中,您需要读取传入请求中API网关传递的值,并使用它们来构建DynamoDB
params
对象。 The exact format of the incoming request is here : https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format 传入请求的确切格式在这里: https : //docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for- lambda输入格式
I would suggest you to read this tutorial, it explains all the details, steps by steps. 我建议您阅读本教程,它逐步解释了所有详细信息。 https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html
https://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.