I am trying to generate a YouTube style id in a function every time a video is uploaded to an S3 bucket.
I have a Lambda function set up and ready to run each time this happens. It includes the following code to generate the hash:
var ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var ID_LENGTH = 10;
function generatehash() {
var rtn = '';
for (var i = 0; i < ID_LENGTH; i++) {
rtn += ALPHABET.charAt(Math.floor(Math.random() * ALPHABET.length));
}
return rtn;
}
var outputhash = generatehash();
exports.handler = function(event, context) {
console.log(outputhash);
};
Every time the function is triggered the same ID is generated. Have I misunderstood how these Lambda functions work? Are they not running dynamically each time they are called?