Search code examples
amazon-web-servicesaws-lambdaamazon-dynamodbaws-amplifyaws-amplify-sdk-js

How can I get my dynamodb lambda function to check the user's identity?


I've set up a dynamodb lambda trigger using this documentation. The function successfully triggers when the dynamodb table is updated, and I can view the output just fine.

I want to find the identity of the user that updated the dynamodb table but this info doesn't seem to be included in the event. How can I accomplish this?

The event looks like this:

{
"Records": [
    {
        "eventID": "1725dad5b286b22b02cffc28e5006437",
        "eventName": "INSERT",
        "eventVersion": "1.1",
        "eventSource": "aws:dynamodb",
        "awsRegion": "us-west-2",
        "dynamodb": {
            "ApproximateCreationDateTime": 1607759729,
            "Keys": {
                "receiver": {
                    "S": "c2217b13-12e8-42a4-a1ab-627f764493c9"
                },
                "sender": {
                    "S": "6fad5bc8-a389-4d73-b171-e709d5d8bdd8"
                }
            },
            "NewImage": {
                "createdAt": {
                    "S": "2020-12-12T07:55:29.105Z"
                },
                "receiver": {
                    "S": "c2217b13-12e8-42a4-a1ab-627f764493c9"
                },
                "sender": {
                    "S": "6fad5bc8-a389-4d73-b171-e709d5d8bdd8"
                },
                "__typename": {
                    "S": "FriendRequest"
                },
                "updatedAt": {
                    "S": "2020-12-12T07:55:29.105Z"
                }
            },
            "SequenceNumber": "4092400000000003379896405",
            "SizeBytes": 261,
            "StreamViewType": "NEW_AND_OLD_IMAGES"
        },
        "eventSourceARN": "arn:aws:dynamodb:us-west-2:213277979580:table/FriendRequest-rmzuppsajfhzlfgjehczargowa-apisecure/stream/2020-12-11T07:48:02.462"
    }
]

}


Solution

  • DynamoDB does not provide the ID of the user that did write the record. The only way you can achieve this is to have the user id be part of the DynamoDB item in the first place. But that means your application needs to identify the user and write that attribute.

    But this obviously will not work, if the item is inserted using the AWS Console. In that case your user would need to insert his own ID (or the ID of another user) by hand.