来自 lambda 的 context.awsRequestId

FXu*_*Xux 6 lambda amazon-web-services node.js serverless-framework

来自 uuid 的context.awsRequestId真的是独一无二的吗?我想将它与资源的创建结合使用,所以我现在可以在创建资源时:

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);
  });
};
Run Code Online (Sandbox Code Playgroud)

谢谢你。

Mic*_*bot 9

我认为这没有明确记录,但根据观察,这些 UUID 似乎不是随机生成的(这有利于唯一性)。相反,它们看起来像是Type 1 UUIDs的变体,其中大部分字节实际上表示时间戳,因此似乎可以安全地假设它们在空间和时间上都是唯一的。

当数位Mxxxxxxxx-xxxx-Mxxx-xxxx-xxxxxxxxxxxx被设置为1,UUID应该是1型和应该代表一个高分辨率的时间戳和“节点”标识,但在这种情况下,节点部件似乎携带任何有意义的信息......但似乎时间戳接近到实时(虽然不完全如此)。