2

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.

1 Answer 1

3

Typically, a REST API will pass parameters through the Query string, such as :

GET /resources?param1=value1&param2=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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Is it not possible to use POST method to receive the data? I tested the API using POST method in API gateway and is executing successfully. Is it not possible to retrieve the data from it?
You shouldn't be using POST method for getting a record it isn't RESTful. As explained in the answer you should be using GET method and parameters are found in event.queryStringParameters. POST body request can be found in event.body
Why shouldn't I use POST method for getting a record. I used a callback method in my lambda function to return the records in the response and handled the response in my javascript. Why is it not a right method? It works perfectly as intended.
The principle of HTTP REST is to use GET to query data, POST/PUT to create/update data and DELETE to delete data. You read more about REST design at en.wikipedia.org/wiki/Representational_state_transfer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.