2

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?

DaveR
  • 2,355
  • 1
  • 19
  • 36
  • 1
    have you tried to move `var outputhash = generatehash();` in body your `exports.handler` function? – D.Dimitrioglo Jun 19 '16 at 08:52
  • 1
    Unrelated to your actual question, note that your implementation throws away a lot of entropy by using each random call to generate only one character... potentially reducing the ultimate randomness (reduced collision resistance, increased predictability) of this function. You may want to look at something like [randomstring](https://www.npmjs.com/package/randomstring), which uses `crypto`, and might be a better solution. Also, eliminating the letter `u` will help prevent generation of a lot of potentially offensive words, at least in English. – Michael - sqlbot Jun 19 '16 at 15:38

1 Answers1

4

You should move var outputhash = generatehash(); in body your exports.handler function.

D.Dimitrioglo
  • 3,413
  • 2
  • 21
  • 41
  • 2
    could you explain why this doesn't work elsewhere in the function? – DaveR Jun 19 '16 at 09:00
  • 3
    as far as I know, lambda creates docker container and takes it available for some time, therefore all the code that you have written out of your `exports.handler` calls once per docker container – D.Dimitrioglo Jun 19 '16 at 09:02
  • 1
    That's essentially correct. The handler is called once per function invocation, but the process itself may be frozen and thawed (possibly, those are imprecise terms that I just now made up) for reuse, by calling its handler again, an undefined number of times for an undefined length of time before being destroyed. The same container and process will never be used for concurrent requests, where one request begins before another ends -- that case always results in multiple independent containers and processes; otherwise, persistence must be anticipated but is not guaranteed. – Michael - sqlbot Jun 19 '16 at 15:29
  • Hi @Michael-sqlbot, can i ask you something off-topic if you don'n mind? – D.Dimitrioglo Jun 19 '16 at 15:47
  • I don't mind. Depending on the nature of your question, I monitor the feed for [tag:amazon-web-services] questions, and my contact info is also on my [profile](http://stackoverflow.com/users/1695906/michael-sqlbot?tab=profile). – Michael - sqlbot Jun 19 '16 at 16:04
  • Take a look please at my question http://stackoverflow.com/questions/37905874/fill-the-table-with-data-for-missing-date-postgresql-redshift, maybe you will give me some advice. – D.Dimitrioglo Jun 19 '16 at 16:41