I intend to use DynamoDB streams to implement a log trail that tracks changes to a number of tables (and writes this to log files on S3). Whenever a modification is made to a table, a lambda function will be invoked from the stream event.
Now, I need to record the user that made the modification.
For put
and update
, I can solve this by including an actual table attribute holding the ID of the caller. Now the record stored in the table will include this ID, which isn't really desirable as it's more meta-data about the operation than part of the record itself, but I can live with that.
So for example:
put({
TableName: 'fruits',
Item: {
id: 7,
name: 'Apple',
flavor: 'Delicious',
__modifiedBy: 'USER_42'
})
This will result in a lambda function invocation, where I can write something like the following to my S3 log file:
table: 'fruits',
operation: 'put',
time: '2018-12-10T13:35:00Z',
user: 'USER_42',
data: {
id: 7,
name: 'Apple',
flavor: 'Delicious',
}
However, for deletes, a problem arises - how can I log the calling user of the delete operation? Of course I can make two requests, one that updates the __modifiedBy
, and another that deletes the item, and the stream would just fetch the __modifiedBy
value from the OLD_IMAGE
included in the stream event. However, this is really undesirable, having to spend 2 writes on a single delete of an item.
So is there a better way, such as attaching metadata to DynamoDB operations, that are carried over into stream events, without being part of the data written to the table itself?