Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions and
limitations under the License. */
// Lambda function acting as API endpoint for IoT Reference Architecture
console.log('Loading function');
var AWS = require('aws-sdk');
var DOC = require('dynamodb-doc');
var cw = new AWS.CloudWatch();
var doc = new DOC.DynamoDB();
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var stackName = context.functionName.split("-")[0];
var params = {
TableName: stackName + '-SensorData',
Limit: 20, //return the 20...
ScanIndexForward: false, //... most recent items
KeyConditions: [doc.Condition('SensorId', 'EQ', event.sensorid)]
};
doc.query(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
const AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
// Create table helper function that creates the supplied table if it
// does not already exist
module.exports.createTable = function (tableDefinition, callback) {
const newTable = {
TableName: tableDefinition.TableName
};
dynamo.describeTable(newTable, function (error) {
if (!error) {
console.log("Table already exists: " + tableDefinition.TableName);
callback(null);
}
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
const AWS = require('aws-sdk');
const s3 = new AWS.S3({apiVersion: '2006-03-01'});
// Create table helper function that creates the supplied table if it
// does not already exist
module.exports.createTable = function (tableDefinition, callback) {
var newTable = {
TableName: tableDefinition.TableName
};
dynamo.describeTable(newTable, function (error) {
if (!error) {
console.log("Table already exists: " + tableDefinition.TableName);
callback(null);
}
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
'use strict';
const logManager = require('./Utils/log-manager.js');
const helper = require('./Utils/helper.js');
const doc = require('dynamodb-doc');
const dynamodb = new doc.DynamoDB();
const tableName = "Order";
const tableDefinition = {
AttributeDefinitions: [
{
AttributeName: "OrderId",
AttributeType: "S"
} ],
KeySchema: [
{
AttributeName: "OrderId",
KeyType: "HASH"
} ],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
'use strict';
const logManager = require('/opt/nodejs/log-manager.js');
const helper = require('/opt/nodejs/helper.js');
const doc = require('dynamodb-doc');
const dynamodb = new doc.DynamoDB();
const tableName = "Order";
const tableDefinition = {
AttributeDefinitions: [
{
AttributeName: "OrderId",
AttributeType: "S"
} ],
KeySchema: [
{
AttributeName: "OrderId",
KeyType: "HASH"
} ],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
'use strict';
const logManager = require('/opt/nodejs/log-manager.js');
const helper = require('/opt/nodejs/helper.js');
const doc = require('dynamodb-doc');
const dynamodb = new doc.DynamoDB();
const tableName = "Product";
const tableDefinition = {
AttributeDefinitions: [
{
AttributeName: "ProductId",
AttributeType: "S"
} ],
KeySchema: [
{
AttributeName: "ProductId",
KeyType: "HASH"
} ],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
'use strict';
const logManager = require('/opt/nodejs/log-manager.js');
const helper = require('/opt/nodejs/helper.js');
const metricsManager = require('/opt/nodejs/metrics-manager.js');
const doc = require('dynamodb-doc');
const dynamodb = new doc.DynamoDB();
const tableName = "Order";
const tableDefinition = {
AttributeDefinitions: [
{
AttributeName: "OrderId",
AttributeType: "S"
} ],
KeySchema: [
{
AttributeName: "OrderId",
KeyType: "HASH"
} ],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
module.exports.DynamoDBDoc = function () {
if (!Config.aws.dynamoDbEndpoint) {
return new DynamoDbDOC.DynamoDB(new AWS.DynamoDB())
}
else {
return new DynamoDbDOC.DynamoDB(new AWS.DynamoDB({endpoint: new AWS.Endpoint(Config.aws.dynamoDbEndpoint)}));
}
};
var aws = require('aws-sdk');
var doc = require('dynamodb-doc');
var fs = require('fs');
aws.config.update({
accessKeyId: process.env.ACCESS_KEY || 'Temp',
secretAccessKey: process.env.SECRET_KEY || 'Temp',
endpoint: process.env.DYNAMODB_ENDPOINT || 'http://192.168.99.100:8000',
region: process.env.AWS_REGION || 'us-east-1'
});
var schemaDirectory = process.env.SCHEMA_LOCATION || './tables/';
var sampleDataDirectory = process.env.DATA_LOCATION || './table_data/';
var dynamodb = new doc.DynamoDB(new aws.DynamoDB());
fs.readdir(schemaDirectory, (err, items) => {
if (err) {
console.log(err);
} else {
items.map(makeTable);
}
});
function makeTable(item){
var table = item.split('.')[0];
console.log('making table ' + table);
deleteTable(table)
.then(createTable)
.then(loadData)
.catch(function(err) { console.log(err.stack); });
}
function deleteTable(tableName) {
return new Promise((resolve, reject) => {
dynamodb.deleteTable({ TableName: tableName }, (err, data) => {
if (err && err.code === 'ResourceNotFoundException') {
console.log('WARN: can\'t delete ' + tableName + ' table because it does not exist');
} else if (err) {
return reject(err);
}
dynamodb.waitFor('tableNotExists', { TableName: tableName }, (err, data) => {
if (err) {
return reject(err);
} else {
return resolve(tableName);
}
});
});
});
}
function createTable(tableName) {
return new Promise((resolve, reject) => {
var params;
try {
params = require(schemaDirectory + tableName + '.json');
} catch (err) {
return reject(err);
}
dynamodb.createTable(params, (err, data) => {
if (err) {
return reject(err);
}
dynamodb.waitFor('tableExists', { TableName: tableName }, (err, data) => {
if (err) {
return reject(err);
} else {
console.log('table created: ' + tableName);
return resolve(tableName);
}
});
});
});
}
function loadData(tableName) {
var items;
try {
items = require(sampleDataDirectory + tableName + '.json');
} catch (err) {
console.log(err);
return;
}
var requestItem = {};
requestItem[tableName] = [];
var requests = [];
items.forEach((current, index) => {
requestItem[tableName].push({
PutRequest: {
Item: current
}
});
if (index % 25 === 0) {
var copy = Object.assign({}, requestItem);
copy[tableName] = requestItem[tableName].slice();
requests.push(copy);
requestItem[tableName] = [];
}
});
if (requestItem[tableName].length > 0) {
requests.push(requestItem);
}
requests.map((request) => {
dynamodb.batchWriteItem({ RequestItems: request }, (err, data) => {
if (err) {
console.log('error in batch write for ' + tableName + ': ' + err);
} else {
console.log('items saved for ' + tableName);
}
});
});
}
var AWS = require('aws-sdk');
var DOC = require('dynamodb-doc');
var dynamo = new DOC.DynamoDB();
exports.handler = function(event, context) {
var cb = function(err, data) {
if(err) {
console.log('error on GetDemoSAInfo: ',err);
context.done('Unable to retrieve information', null);
} else {
if(data.Items) {
data.Items.forEach(function(elem) {
console.log( elem.username + ": ",elem.score);
});
data.Items.sort(function(a,b) { return parseInt(b.score) - parseInt(a.score) } );
context.done(null, data.Items);
} else {
context.done(null, {});
}