Generating a unique key for dynamodb within a lambda function

If you are using Node.js 8.x, you can use uuid module.

var AWS = require('aws-sdk'),
    uuid = require('uuid'),
    documentClient = new AWS.DynamoDB.DocumentClient();
[...]
        Item:{
            "id":uuid.v1(),
            "Name":"MyName"
        },

If you are using Node.js 10.x, you can use awsRequestId without uuid module.

    var AWS = require('aws-sdk'),
        documentClient = new AWS.DynamoDB.DocumentClient();
[...]
    Item:{
        "id":context.awsRequestId,
        "Name":"MyName"
    },

The UUID package available on NPM does exactly that.

https://www.npmjs.com/package/uuid

You can choose between 4 different generation algorithms:

  • V1 Timestamp
  • V3 Namespace
  • V4 Random
  • V5 Namespace (again)

This will give you:

"A UUID [that] is 128 bits long, and can guarantee uniqueness across space and time." - RFC4122

The generated UUID will look like this: 1b671a64-40d5-491e-99b0-da01ff1f3341
If it's too long, you can always encode it in Base64 to get G2caZEDVSR6ZsAAA2gH/Hw but you'll lose the ability to manipulate your data through the timing and namespace information contained in the raw UUID (which might not matter to you).


awsRequestId looks like its actually V.4 UUID (Random), code snippet below:

exports.handler = function(event, context, callback) {
    console.log('remaining time =', context.getRemainingTimeInMillis());
    console.log('functionName =', context.functionName);
    console.log('AWSrequestID =', context.awsRequestId);
    callback(null, context.functionName);
};

In case you want to generate this yourself, you can still use https://www.npmjs.com/package/uuid or Ulide (slightly better in performance) to generate different versions of UUID based on RFC-4122

For Go developers, you can use these packages from Google's UUID, Pborman, or Satori. Pborman is better in performance, check these articles and benchmarks for more details.

More Info about Universal Unique Identifier Specification could be found here.