Created
May 9, 2016 13:19
-
-
Save richardfriedman/c38e7f8568cdb23d0436bae356acf908 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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