📖 Scenario: You are setting up a simple database to store information about books in a library. You will create a DynamoDB table, define its key structure, and add some book items with attributes.
🎯 Goal: Build a DynamoDB table named LibraryBooks with a primary key and add three book items with specific attributes.
📋 What You'll Learn
Create a DynamoDB table named LibraryBooks with BookID as the partition key (string type).
Define a variable books containing three book items with attributes: BookID, Title, and Author.
Use a loop to add each book item to the LibraryBooks table.
Add a final configuration to enable point-in-time recovery on the table.
💡 Why This Matters
🌍 Real World
DynamoDB tables are used to store structured data for applications like libraries, e-commerce, and user profiles.
💼 Career
Understanding how to create tables, add items, and configure recovery is essential for cloud engineers and developers working with AWS databases.
Progress0 / 4 steps
1
Create the DynamoDB table definition
Create a variable called table_definition that defines a DynamoDB table named LibraryBooks with a partition key named BookID of type string.
AWS
Hint
Use a dictionary with keys TableName, KeySchema, AttributeDefinitions, and ProvisionedThroughput.
2
Define the book items data
Create a variable called books that is a list of dictionaries. Each dictionary represents a book with keys BookID, Title, and Author. Add these exact books: {'BookID': 'B1', 'Title': '1984', 'Author': 'George Orwell'}, {'BookID': 'B2', 'Title': 'To Kill a Mockingbird', 'Author': 'Harper Lee'}, and {'BookID': 'B3', 'Title': 'The Great Gatsby', 'Author': 'F. Scott Fitzgerald'}.
AWS
Hint
Use a list with three dictionaries exactly matching the book details.
3
Add the book items to the table
Write a for loop using variables book to iterate over the books list and add each book to the LibraryBooks table using a method call table.put_item(Item=book).
AWS
Hint
Use a for loop with variable book and call table.put_item(Item=book) inside the loop.
4
Enable point-in-time recovery on the table
Add a configuration line to enable point-in-time recovery on the LibraryBooks table by calling table.update_point_in_time_recovery(PointInTimeRecoverySpecification={'PointInTimeRecoveryEnabled': True}).
AWS
Hint
Call table.update_point_in_time_recovery with the correct dictionary to enable recovery.
Practice
(1/5)
1. What is the main purpose of a table in AWS DynamoDB?
easy
A. To store items, which are records
B. To run code functions
C. To manage user permissions
D. To host websites
Solution
Step 1: Understand the role of a table in DynamoDB
A table is a container that holds items, which are like rows in a database.
Step 2: Identify the correct purpose
Since tables store items (records), this matches To store items, which are records.
Final Answer:
To store items, which are records -> Option A
Quick Check:
Table = container for items [OK]
Hint: Tables hold records called items [OK]
Common Mistakes:
Confusing tables with code functions
Thinking tables manage permissions
Assuming tables host websites
2. Which of the following is the correct way to define an item attribute in DynamoDB?
easy
A. ("Name", "John", "Age", 30)
B. {"Name": "John", "Age": 30}
C. ["Name", "John", "Age", 30]
D. "Name": "John", "Age": 30
Solution
Step 1: Recognize the attribute format
Attributes are key-value pairs, best represented as JSON objects with keys and values.
Step 2: Match the correct syntax
{"Name": "John", "Age": 30} shows a JSON object with keys "Name" and "Age" and their values, which is correct.
Final Answer:
{"Name": "John", "Age": 30} -> Option B
Quick Check:
Attributes = key-value pairs in JSON [OK]
Hint: Attributes are key-value pairs in JSON format [OK]
Common Mistakes:
Using arrays instead of key-value pairs
Using tuples instead of objects
Omitting braces for JSON objects
3. Given a DynamoDB table with primary key "UserID", what happens if you try to add two items with the same UserID but different attributes?
medium
A. Both items are stored separately
B. The table creates a duplicate primary key
C. An error is thrown and no item is added
D. The second item overwrites the first item
Solution
Step 1: Understand primary key uniqueness
In DynamoDB, the primary key must be unique for each item in the table.
Step 2: Effect of duplicate primary key
If you add an item with an existing primary key, it replaces the old item with the new one.
Final Answer:
The second item overwrites the first item -> Option D
4. You try to insert an item into a DynamoDB table but get an error. The item has no primary key attribute. What is the most likely cause?
medium
A. Missing required primary key attribute in the item
B. Table is full and cannot accept new items
C. Attributes have invalid data types
D. You exceeded the maximum number of attributes
Solution
Step 1: Recall primary key requirement
Every item must have the primary key attribute to be uniquely identified in the table.
Step 2: Identify cause of error
If the item lacks the primary key attribute, DynamoDB rejects it with an error.
Final Answer:
Missing required primary key attribute in the item -> Option A
Quick Check:
Primary key missing causes insert error [OK]
Hint: Every item must include the primary key attribute [OK]
Common Mistakes:
Assuming table capacity causes insert errors
Blaming attribute data types without checking keys
Thinking attribute count limits cause this error
5. You want to design a DynamoDB table to store user profiles. Each user has a unique UserID and may have multiple email addresses. How should you structure the table and items to handle this?
hard
A. Use email as the primary key and ignore UserID
B. Use UserID as the primary key and create a separate item for each email
C. Use UserID as the primary key and store emails as a list attribute inside each item
D. Create multiple tables, one per email address
Solution
Step 1: Identify unique identifier
UserID uniquely identifies each user, so it should be the primary key.
Step 2: Store multiple emails
Since a user can have multiple emails, store them as a list attribute inside the user item.
Step 3: Evaluate other options
Creating separate items per email duplicates UserID or loses uniqueness; using email as key ignores UserID; multiple tables is inefficient.
Final Answer:
Use UserID as the primary key and store emails as a list attribute inside each item -> Option C
Quick Check:
Primary key + list attribute for multiple values [OK]
Hint: Use primary key for user, list attribute for multiple emails [OK]
Common Mistakes:
Using email as primary key instead of UserID
Creating multiple items for one user
Making multiple tables unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.