Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

context.awsRequestId from lambda

Is the uuid from context.awsRequestId really unique? I want to use it in association with the creation of a resource, so I can now when the resource was created:

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

const dynamoDb = new AWS.DynamoDB.DocumentClient();

module.exports.create = (event, context, callback) => {
  const timestamp = new Date().getTime();
  const data = JSON.parse(event.body);
  if (typeof data.text !== 'string') {
    console.error('Validation Failed');
    callback(null, {
      statusCode: 400,
      headers: { 'Content-Type': 'text/plain' },
      body: 'Couldn\'t create the todo item.',
    });
    return;
  }

  const params = {
    TableName: process.env.DYNAMODB_TABLE,
    Item: {
      id: context.awsRequestId,
      text: data.text,
      checked: false,
      createdAt: timestamp,
      updatedAt: timestamp,
    },
  };

  // write the todo to the database
  dynamoDb.put(params, (error) => {
    // handle potential errors
    if (error) {
      console.error(error);
      callback(null, {
        statusCode: error.statusCode || 501,
        headers: { 'Content-Type': 'text/plain' },
        body: 'Couldn\'t create the todo item.',
      });
      return;
    }

    // create a response
    const response = {
      statusCode: 200,
      body: JSON.stringify(params.Item),
    };
    callback(null, response);
  });
};

Thank you.

like image 344
FXux Avatar asked Feb 06 '18 17:02

FXux


1 Answers

I don't think this is clearly documented, but based on observations, these UUIDs do not appear to be randomly generated (which is good for uniqueness). Instead, they look like they are a variant of Type 1 UUIDs, where most of the bytes actually represent a timestamp, so it would appear to be a safe assumption that they are both spatially and temporally unique.

When the digit M in xxxxxxxx-xxxx-Mxxx-xxxx-xxxxxxxxxxxx is set to 1, the UUID should be a Type 1 and should represent a high resolution timestamp and "node" identifier, although in this case the node component seems to carry no meaningful information... but the timestamps seem close to real time (though not precisely so).

like image 81
Michael - sqlbot Avatar answered Oct 17 '22 00:10

Michael - sqlbot