📖 Scenario: You are building a simple online bookstore database using DynamoDB. You want to create a secondary index to quickly find books by their genre. To optimize your index, you need to understand how to project attributes into the index using different projection types.
🎯 Goal: Create a DynamoDB table with a Global Secondary Index (GSI) on the Genre attribute. Configure the index to use different projection types: ALL, KEYS_ONLY, and INCLUDE. This will help you learn how each projection type affects the data stored in the index.
📋 What You'll Learn
Create a DynamoDB table named Books with primary key BookID (string).
Add a Global Secondary Index named GenreIndex with partition key Genre (string).
Set the index projection type to ALL in Step 1.
Change the index projection type to KEYS_ONLY in Step 2.
Change the index projection type to INCLUDE and include the Title and Author attributes in Step 3.
Complete the final table definition with the INCLUDE projection type in Step 4.
💡 Why This Matters
🌍 Real World
In real-world applications, choosing the right projection type for your DynamoDB indexes helps optimize performance and cost by controlling which attributes are copied into the index.
💼 Career
Database developers and cloud engineers often configure DynamoDB indexes to improve query speed and reduce costs by selecting appropriate projection types.
Progress0 / 4 steps
1
Create DynamoDB table with GSI using ALL projection
Create a DynamoDB table named Books with primary key BookID of type string. Add a Global Secondary Index named GenreIndex with partition key Genre of type string. Set the index's ProjectionType to ALL.
DynamoDB
Hint
Remember to define AttributeDefinitions for both BookID and Genre. Set ProjectionType inside the Projection object of the GSI.
2
Change the GSI projection type to KEYS_ONLY
Modify the existing GenreIndex in the Books table to set the ProjectionType to KEYS_ONLY instead of ALL.
DynamoDB
Hint
Change the ProjectionType value inside the Projection object of the GenreIndex to KEYS_ONLY.
3
Change the GSI projection type to INCLUDE with specific attributes
Modify the GenreIndex to use INCLUDE as the ProjectionType. Include the attributes Title and Author in the NonKeyAttributes list.
DynamoDB
Hint
Set ProjectionType to INCLUDE and add NonKeyAttributes with the list ["Title", "Author"] inside the Projection object.
4
Complete the table definition with INCLUDE projection type
Finalize the Books table definition with the GenreIndex using INCLUDE projection type including Title and Author as non-key attributes. Ensure all attributes and throughput settings are present.
DynamoDB
Hint
Make sure the full table definition includes the GenreIndex with INCLUDE projection and the non-key attributes Title and Author. Also include provisioned throughput settings.
Practice
(1/5)
1. Which DynamoDB index projection type copies all attributes from the base table to the index?
easy
A. ALL
B. KEYS_ONLY
C. INCLUDE
D. PARTIAL
Solution
Step 1: Understand projection types
ALL copies every attribute from the base table to the index.
Step 2: Compare with other types
KEYS_ONLY copies only keys; INCLUDE copies keys plus selected attributes.
Final Answer:
ALL -> Option A
Quick Check:
ALL = copies all attributes [OK]
Hint: ALL means every attribute is copied to the index [OK]
Common Mistakes:
Confusing INCLUDE with ALL
Thinking KEYS_ONLY copies all attributes
Assuming PARTIAL is a valid projection type
2. Which of the following is the correct way to specify a KEYS_ONLY projection in a DynamoDB index definition?
easy
A. "ProjectionType": "ALL"
B. "ProjectionType": "KEYS_ONLY"
C. "ProjectionType": "INCLUDE"
D. "ProjectionType": "KEYS"
Solution
Step 1: Recall correct syntax for KEYS_ONLY
The projection type must be exactly "KEYS_ONLY" in quotes.
Step 2: Eliminate incorrect options
ALL and INCLUDE are valid but not KEYS_ONLY; "KEYS" is invalid syntax.
Final Answer:
"ProjectionType": "KEYS_ONLY" -> Option B
Quick Check:
KEYS_ONLY syntax = "KEYS_ONLY" [OK]
Hint: Use exact string "KEYS_ONLY" for keys-only projection [OK]
Common Mistakes:
Using "KEYS" instead of "KEYS_ONLY"
Confusing INCLUDE with KEYS_ONLY syntax
Omitting quotes around projection type
3. Given a DynamoDB table with attributes id, name, age, and city, and a Global Secondary Index (GSI) with projection type INCLUDE and non-key attributes age and city, which attributes will be available in the index?
medium
A. id, age, and city
B. id, name, age, and city
C. Only age and city
D. Only id and name
Solution
Step 1: Identify keys and included attributes
Index always includes keys; INCLUDE adds specified non-key attributes.
Step 2: Determine which attributes are keys
Assuming id is the partition key, name is not a key.
Step 3: Combine keys and included attributes
Index has id (key), plus age and city (included).
Final Answer:
id, age, and city -> Option A
Quick Check:
INCLUDE = keys + chosen attributes [OK]
Hint: INCLUDE = keys plus only specified extra attributes [OK]
Common Mistakes:
Including non-key attributes not specified in INCLUDE
Assuming all attributes are included
Confusing keys with non-key attributes
4. You defined a DynamoDB index with projection type INCLUDE but forgot to specify any non-key attributes. What will happen when you try to create this index?
medium
A. The index will include keys plus all attributes by default.
B. The index will include only the keys, ignoring INCLUDE.
C. The index will include all attributes from the base table.
D. The index creation will fail with a validation error.
Solution
Step 1: Understand INCLUDE projection requirements
INCLUDE requires specifying at least one non-key attribute.
Step 2: Check behavior when no attributes specified
Omitting non-key attributes causes a validation error during index creation.
Final Answer:
The index creation will fail with a validation error. -> Option D
Quick Check:
INCLUDE needs attributes; missing causes error [OK]
Hint: INCLUDE must list attributes; empty list causes error [OK]
Common Mistakes:
Assuming INCLUDE defaults to ALL attributes
Thinking index silently defaults to KEYS_ONLY
Ignoring validation errors on index creation
5. You want to optimize a DynamoDB Global Secondary Index (GSI) to reduce storage costs but still need to query by email and retrieve phone and address attributes quickly. Which projection type should you choose?
hard
A. ALL
B. KEYS_ONLY
C. INCLUDE with phone and address
D. INCLUDE with email only
Solution
Step 1: Understand storage cost vs attribute needs
ALL copies all attributes but costs more storage; KEYS_ONLY copies only keys.
Step 2: Identify needed attributes for queries
You need email as key and phone, address for quick retrieval.
Step 3: Choose INCLUDE projection with needed attributes
INCLUDE lets you copy keys plus only phone and address, saving storage.
Final Answer:
INCLUDE with phone and address -> Option C
Quick Check:
INCLUDE = keys + needed attributes saves cost [OK]
Hint: Use INCLUDE to add only needed attributes, saving space [OK]
Common Mistakes:
Choosing ALL and increasing storage unnecessarily
Using KEYS_ONLY and missing needed attributes
Including only email but not phone or address
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.