I am quite new to Node.js and I am trying to load json configs files stored either in Amazon s3 or local repository. Below is my code so far:
var cfg = process.env.CONFIG_FILE_NAME;
log.info("Loading config '%s'", cfg);
if(cfg.indexOf("s3") !== -1 || cfg.indexOf("S3") !== -1) {
log.info("S3 path detected");
var s3 = new aws.S3();
var myRegex = /\/\/(\w*)\/(.*)/g;
var matched = myRegex.exec(cfg);
var bucket = matched[1];
log.info("Extracted bucket: ", bucket);
var key = matched[2];
log.info("Extracted key: ", key);
var params = {
Bucket: bucket,
Key: key
};
s3.getObject(params, function(err, data) {
if (err) log.warn(err, err.stack);
else {
log.info("Loaded config from S3");
cfg = JSON.parse(data.Body);
log.info("Config content: "cfg);
}
});
}
else {
try {
//some code here
} catch (e) {
//some code here
}
}
subscriptions = cfg.subscriptions;
log.info("This supposes to contain json content from S3: ", cfg);
The idea is that the code will check if there is a path to S3 in the message sent to Amazon Lambda (CONFIG_FILE_NAME field). If it exists, then the code load the config file from s3, otherwise, it loads locally. However, when I try to run the code, it returns something like this:
4 Jan 11:37:34 - [INFO] Loading config 'Path-to-S3'
4 Jan 11:37:34 - [INFO] S3 path detected
4 Jan 11:37:34 - [INFO] Extracted bucket: mybucket
4 Jan 11:37:34 - [INFO] Extracted key: mykey.cfg.json
4 Jan 11:37:34 - [INFO] "This suppose to contain json content from S3: Path-to-S3'
4 Jan 11:37:34 - [INFO] Loaded config from S3
4 Jan 11:37:34 - [INFO] Config content: my-config-content
So the problem is that, the code executes the line subscriptions = cfg.subscriptions;
before the config file is loaded from S3. The variable cfg at this line only contains the path to the config, not the config content I want to load from S3. My later code implementation depends on this subscriptions field from cfg file so it stucks right here.