I'd like to use Cognito's "sub" value to create a Dynamodb entry via a lambda trigger

Viewed 787

I am using aws cognito for user auth, but would like a dynamodb user table to store more application specific user attributes that cognito isn't designed for.

I want to link cognito & dynamobd together via a unique id. Cognito sets a "sub" value as a unique id once a user is confirmed (done with a pre-signup trigger) but I am not sure how to capture this value in a post confirmation lambda trigger to add to dynamobd. It appears to only be provided in an Auth token payload. Am I best requesting an auth token in my post confirmation lambda trigger? Im not sure if this can be done using Python. I'm very new to it all.

What I am trying to do in one swoop is:

  1. User Registers - request to cognito
  2. Pre lambda trigger sets Confirmed = True
  3. User added to Cognito
  4. Post confirmed lambda trigger adds user to dynamodb with the "sub" uuid as primary key.

Thanks.

1 Answers

According to this answer, the event object for post confirmation lambda will look like this:

{
    "version": "1",
    "region": "eu-central-1",
    "userPoolId": "eu-central-1_45YtlkflA",
    "userName": "user4",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-java-console",
        "clientId": "4736lckau64in48dku3rta0eqa"
    },
    "triggerSource": "PostConfirmation_ConfirmSignUp",
    "request": {
        "userAttributes": {
            "sub": "a2c21839-f9fc-49e3-be9a-16f5823d6705",
            "cognito:user_status": "CONFIRMED",
            "email_verified": "true",
            "email": "asdfsdfsgdfg@carbtc.net"
        }
    },
    "response": {}
}

It looks like you should be able to grab sub from request.userAttributes. userName might be another good choice.

Related