Generating a unique key for dynamodb within a lambda function

20,368

Solution 1

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).

Solution 2

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"
    },

Solution 3

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.

Solution 4

We use idgen npm package to create id's. There are more questions on the length depending upon the count to increase or decrease the size.

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

We prefer this over UUID or GUID's since those are just numbers. With DynamoDB it is all characters for guid/uuid, using idgen you can create more id's with less collisions using less number of characters. Since each character has more ranges.

Hope it helps.

EDIT1:

Note! As of idgen 1.2.0, IDs of 16+ characters will include a 7-character prefix based on the current millisecond time, to reduce likelihood of collisions.

Share:
20,368
Philiiiiiipp
Author by

Philiiiiiipp

Updated on January 12, 2022

Comments

  • Philiiiiiipp
    Philiiiiiipp over 2 years

    DynamoDB does not have the option to automatically generate a unique key for you.

    In examples I see people creating a uid out of a combination of fields, but is there a way to create a unique ID for data which does not have any combination of values that can act as a unique identifier? My questions is specifically aimed at lambda functions.

    One option I see is to create a uuid based on the timestamp with a counter at the end, insert it (or check if it exists) and in case of duplication retry with an increment until success. But, this would mean that I could potentially run over the execution time limit of the lambda function without creating an entry.