
- Use
AWS.util.uuid.v4()
to generate UUID in AWS Lambda (NodeJS) - No need to installhttps://www.npmjs.com/package/uuid.
Example:
const AWS = require('aws-sdk');exports.handler = async (event) => { console.log(AWS.util.uuid.v4());}
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(); } }
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());
Current Node.js lambda runtime 14.x does supports above code.
Learned about this option from@galkin comments to this post. Thank you.
Image byIan Lindsay fromPixabay
Top comments(2)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse