User management for AWS Amplify Gen2 applications

The goal of this prompt is to implement a user management feature that integrates with Amplify Auth and stores user profile records in an Amazon DynamoDb table

@cremich

Author

Implement
IDE
Dev Agent
Private

Prompt

/dev Implement a user management feature that integrates with Amplify Auth and stores user profiles records in an Amazon DynamoDB table. The application is built on AWS Amplify Gen2. Here are the required changes:

1. Update the `amplify/data/resource.ts` file to define a data model for the user's profile:
- The user model should contain `id`, `username`, `email` and `displayName`, 'profileOwner'
- Data access for the user model is configured with the owner authorization strategy allowing read-only access for the owner defined in attribute `profileOwner`.

2. Create a new Amplify function resource for the post-authentication lambda function.
- Create a new directory and a resource file, `amplify/auth/post-confirmation/resource.ts`. Then, define the Function with `defineFunction`
- set arm64 architecture to use graviton processors
- set timeout to 30 seconds
- set memory to 512

3. Create the lambda function handler implementation:
- Create a new handler file `amplify/auth/post-authentication/handler.ts`
- The handler should map attributes from the cognito event source to a DynamoDB record that maps to the user model schema
- The format of the `profileOwner` attribute must be `<sub>::<username>`. 
- The user record should also contain a `createdAt` and `updatedAt` timestamp.
- The user record should be saved directly into the DynamoDB user table
- Handle errors gracefully without blocking authentication
- Use environment variables for the DynamoDB table name

4. Set the newly created Amplify function resource on the auth resource as a trigger.

5. Modify Amplify-generated post-authentication lambda resource
- Add necessary IAM permissions 
- Add necessary environment variables
- Remember that these modifications should be made in the `amplify/backend.ts` file after the `defineBackend` call. Only Lambda functions that are part of the amplify backend can be modified.

How to Use

Copy-paste this prompt directly into your chat. This will directly start the dev agent. Once the dev-agent finished the work, review the changes. Prepare yourself for minor adjustments. The following references to the AWS Amplify Gen 2 documentation are helpful for your code-review or providing feedback to the dev agent to regenerate the implementation: - [Create a user profile record](https://docs.amplify.aws/react/build-a-backend/functions/examples/create-user-profile-record/) - [Modify Amplify-generated Lambda resources with CDK](https://docs.amplify.aws/react/build-a-backend/functions/modify-resources-with-cdk/)