Reputation: 1098
I have a Lambda function to query data from DynamoDB table. The Lambda function is as follows:
'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. Now, I'm unsure how to pass these parameters using API to invoke this Lambda function. I've created an API with GET method but the GET method doesn't use request Body to send parameters. 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. I'm just unable to query items from the table using the set parameters.
Upvotes: 2
Views: 9739
Reputation: 14915
Typically, a REST API will pass parameters through the Query string, such as :
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
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.
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
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
Upvotes: 3