Hey everyone! I'm working on an AWS project that utilizes AWS Amplify for user authentication and Cognito for managing users. I've already set up user sign-up and login features with Cognito, along with a DynamoDB table to store user data like progress. However, I'm running into an issue: I need a way to automatically create a corresponding record for each new user in DynamoDB when they sign up. How can I achieve this? Any insights on connecting new Cognito users to my DynamoDB table for tracking purposes would be greatly appreciated!
2 Answers
Another approach is to have users set up their profiles after sign-up, which triggers a secured API request to your API Gateway. This can utilize the auth token that contains the user's ID.
One effective method is to use the post-confirmation Lambda trigger. This allows you to write a record to DynamoDB immediately after a user confirms their sign-up. Check out the documentation for more details on setting this up!
That's definitely the way to go! Just a heads up: the post and pre-confirmation hooks are limited to 5 seconds, which can't be extended. If you're just saving user data to DynamoDB, you should be fine. In my setup, I call a function in the post-confirmation Lambda, using the pynamodb library. Here's a snippet of how I do it:
```python
def get_or_create_user_in_db(
hash_key: str, email: str, customer_id: Optional[str] = None
) -> UserModel:
try:
user = UserModel.get(hash_key=hash_key)
except DoesNotExist:
user = UserModel(
cognito_username_and_sub=hash_key, email=email, customer_id=customer_id
)
user.save()
return user
```

Thanks for your input! Could you explain how you set that up? I'd really appreciate more details.