var AWS = require("aws-sdk"); //var AWS = require("async"); // Let me get this straight. AWS brags about the async library and how it can be used in Lambda but does // not make it part of AWS Lambda like "aws-sdk" is. Brilliant. // The below is only needed if DynamoDB is not in the same region as the Lambda function. //AWS.config.update({ // region: "us-west-2" //}); var docClient = new AWS.DynamoDB.DocumentClient(); /* Variable to store the data we get back from DynamoDB */ var datafound = 0; /* Define the DynamoDB query */ var params = { TableName: "UserAccountServiceState", /* The DynamoDB table to connect to */ ProjectionExpression: "UserAccountServiceStateID", /* The column(s) we want to be returned */ FilterExpression: "Ansi = :datainAnsi", /* Search term; in this case return rows whose Ansi column value equals CT */ ExpressionAttributeValues: { ":datainAnsi": 'CT' /* Search value CT substituted into the search term where :datainAnsi is found */ } }; /* Define the function to query DynamoDB. As is shown later, while this works, its change to datafound will never be seen by the calling function "exports.handler = function(event, context)" */ function onScan(err, data) { if (err) { console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2)); } else { // We have data...Go through the data returned by DynamoDB // Save the value to the "global" variable to be read for changes in // "exports.handler = function(event, context)". As we know, that function will not see the change. data.Items.forEach(function(row) { datafound = row.UserAccountServiceStateID; }); } } exports.handler = function(event, context) { /* Nope #1: Cannot do the following because, while "onScan" works the function is external to "exports.handler = function(event, context)" so if it sets any data, even "global" data like a variable, the changes will not be seen inside this function. In fact, this function will just continue on executing whatever is after this external function call. */ //docClient.scan(params, onScan); /* Nope #2: Cannot do this because this function will not see the change to the variable's value even if that variable is "global". The result will just be an infinite loop. */ //do { // /* Wait for the scan to complete */ //} while (datafound === 0); /* Nope #3: While having a context return is recommended, because the variable datafound never changes from this function's point of view the value of 0 will always be returned. */ ///* Generate Response */ //context.done( // null, // { // "datafound": datafound // } //); /* DO. Embed the scan function inside of "exports.handler = function(event, context)" so that execution will feel "synchronous"; in this case "exports.handler = function(event, context)" will continue execution but since it has an embedded function, it will not exit until the embedded function has completed executing. Because the scan callback function is embedded, the context.done (your response) should be placed within it. */ docClient.scan(params, function(err, data) { if (err) { // DynamoDB error console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2)); } else { // We have data...Go through the data returned by DynamoDB data.Items.forEach(function(row) { // Save the value to the "global" variable datafound = row.UserAccountServiceStateID; }); } /* Generate Response */ context.done( null, { "datafound": datafound } ); }); };