Last active
May 8, 2018 07:03
-
-
Save fforres/c0687e13be7326ae16087389d6e702d6 to your computer and use it in GitHub Desktop.
lambda self-updating function
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 accessKeyId = process.env.accessKeyId; | |
var secretAccessKey = process.env.secretAccessKey; | |
var currentTS = Number(process.env.currentTS); | |
var lambda = new AWS.Lambda({ | |
accessKeyId, | |
secretAccessKey, | |
region: "us-east-1" | |
}); | |
const start = function(event, context, callback) { | |
if (Date.now() < 10 * 1000 * 60) { | |
// do something only if 10 mins passed since last update | |
lambda.updateFunctionConfiguration( | |
{ | |
FunctionName: "EnvironmentTestLambda", | |
Environment: { | |
Variables: { | |
ENV_VARIABLE_NAME: "STRING_VALUE", | |
accessKeyId, | |
secretAccessKey, | |
currentTS: Date.now().toString() | |
} | |
} | |
}, | |
function(err, data) { | |
if (err) { | |
console.log(err, err.stack); // an error occurred | |
callback(err); | |
} else { | |
console.log(data); // successful response | |
callback(null, data); | |
} | |
} | |
); | |
} | |
}; | |
// Uncomment this to test locally | |
// start(null, null, console.log); | |
exports.handler = start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment