Skip to content

Instantly share code, notes, and snippets.

@richardfriedman
Created May 9, 2016 13:19
Show Gist options
  • Save richardfriedman/c38e7f8568cdb23d0436bae356acf908 to your computer and use it in GitHub Desktop.
Save richardfriedman/c38e7f8568cdb23d0436bae356acf908 to your computer and use it in GitHub Desktop.
var aws = require('aws-sdk');
var fs = require("fs");
var Promise = require("es6-promise").Promise;
/**
* Load Configuration from local file or from DynamoDB.
*/
var loadConfig = function(){
return new Promise( function(resolve, reject) {
try {
// If we have it locally.
if ( fs.existsSync('/tmp/config.json') ){
var c = JSON.parse(fs.readFileSync( '/tmp/config.json' ));
resolve(c);
}
} catch( e ){
// Send Error Message, but load from dynamic
console.log( e.message );
}
// Specific to my work
var params = {
TableName: 'myLambdaConfig',
Key: { 'service': 'photos' }
};
var dynamoDb = new aws.DynamoDB.DocumentClient({region:'us-east-1',version:'latest'});
dynamoDb.get(params, function(err, data) {
if ( err ) {
reject(err);
}
if ( data['Item'] && data['Item']['config'] ){
fs.writeFileSync( '/tmp/config.json', JSON.stringify(data['Item']['config']) );
resolve(data['Item']['config']);
} else {
reject("Empty Config.");
}
});
});
}
loadConfig().then(
function( config ) { console.log( config ); },
function( error ) { console.log( "Failed to load config." + error ); }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment