Task 3: Create a Data Table
|
Time to complete |
5 minutes |
|
Services used |
|
|
Get help |
Overview
In this task, you will create a data model to persist data using a GraphQL API and Amazon DynamoDB. Additionally, you will use AWS Identity and Access Management (IAM) authorization to securely give our services the required permissions to interact with each other. You will also allow the Lambda function you created in the previous task to use the GraphQL API to write to your newly created DynamoDB table using an IAM policy.
Key concepts
Amplify backend: Amplify Gen 2 uses a full stack TypeScript developer experience (DX) for defining backends. Simply author app requirements like data models, business logic, and auth rules in TypeScript. Amplify automatically configures the correct cloud resources and deploys them to per-developer cloud sandbox environments for fast, local iteration.
Implementation
-
Update the resource file
On your local machine, navigate to the amplify/data/resource.ts file and update it with the code in this file
. Then, save the file. -
This code will define the schema for the UserProfile data model using a per-owner authorization rule allow.owner() to restrict the expense record’s access to the creator of the record
-
It also uses the field profileOwner to track the ownership, and configures the authorization rule to allow the postConfirmation resource.
-
Granting access to resources creates environment variables for your function, such as the GraphQL API endpoint.
-
-
Deploy sandbox
Open a new terminal window, navigate to your app's root folder (profilesapp), and run the following command to deploy cloud resources into an isolated development space so you can iterate fast.
npx ampx sandbox
-
View confirmation message
Once the cloud sandbox has been fully deployed, your terminal will display a confirmation message.
-
Verify outputs file is added to your project
Verify that the amplify_outputs.json file was generated and added to your project.
-
Generate GraphQL client code
Open a new terminal window, navigate to your app's root folder(profilesapp), and run the following command to generate the GraphQL client code to call your data backend.
Note
You will need to update the following command to use the path to the post-confirmation folder that you created in the previous step. For example: npx ampx generate graphql-client-code --out amplify/auth/post-confirmation/graphql.
npx ampx generate graphql-client-code --out <path-to-post-confirmation-handler-dir>/graphqlAmplify will create the folder amplify/auth/post-confirmation/graphql where you will find the client code to connect to the GraphQL API.
-
Modify the handler file
On your local machine, navigate to the amplify/auth/post-confirmation/handler.ts file and replace the code with the code in this file
. Then, save the file. This code configures the Amplify using the env variables and sets the authorization to use IAM. It then generates a data client using the generateClient() function. Finally, it uses the data client to create a user profile by setting the email and owner based on the attributes of the signed-up user.
Conclusion
You have created a data table and configured a GraphQL API to persist data in an Amazon DynamoDB database. Then, you updated the Lambda function to use the API.