DEV Community

Cover image for AWS Lambda in-built UUID
Prabusah
Prabusah

Posted on • Edited on

10 1

AWS Lambda in-built UUID

Example:

const AWS = require('aws-sdk');

exports.handler = async (event) => {
   console.log(AWS.util.uuid.v4());
}
Enter fullscreen mode Exit fullscreen mode

How this works:
Below snippet from aws-sdk-js shows - it does having uuid node dependency included.

  /**
   * @api private
   */
  uuid: {
    v4: function uuidV4() {
      return require('uuid').v4();
    }
  } 
Enter fullscreen mode Exit fullscreen mode

Benefits

  • Avoiding addition of npm dependency uuid.
  • Reduced code size. Saves few milliseconds of cold start time.

Option: Node.js in-built module crypto

const {randomUUID} = require('crypto'); //Crypto is part of Node.js runtime since v14.17 
console.log(randomUUID());
Enter fullscreen mode Exit fullscreen mode

Current Node.js lambda runtime 14.x does supports above code.

Learned about this option from @galkin comments to this post. Thank you.

Image by Ian Lindsay from Pixabay

Top comments (2)

Collapse
 
galkin profile image
Nikita Galkin

Use const { randomUUID } = require('crypto');

Collapse
 
fmcdev profile image
fmcdev • Edited

This saved my day with AWS Lambda functions, to replace the AWS.util.uuid.v4() not available anymore in aws-sdk V3 without compiling and adding extra layers!!! Thanks so much!

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay