0
0
DynamoDBquery~30 mins

Global Secondary Index (GSI) concept in DynamoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Create and Query a Global Secondary Index (GSI) in DynamoDB
📖 Scenario: You are building a simple product catalog database using DynamoDB. You want to store products with their ProductID as the main key. Later, you want to quickly find products by their Category using a Global Secondary Index (GSI).
🎯 Goal: Create a DynamoDB table with a primary key ProductID, add a Global Secondary Index (GSI) on Category, and write a query to find all products in a specific category.
📋 What You'll Learn
Create a DynamoDB table named Products with ProductID as the partition key.
Add a Global Secondary Index named CategoryIndex with Category as the partition key.
Insert sample product items with ProductID, Name, and Category attributes.
Write a query using the GSI CategoryIndex to find all products in the category Electronics.
💡 Why This Matters
🌍 Real World
Global Secondary Indexes let you query DynamoDB tables efficiently by attributes other than the primary key, which is common in real-world applications like product catalogs, user profiles, and event logs.
💼 Career
Understanding GSIs is essential for database engineers and backend developers working with DynamoDB to design scalable and performant NoSQL databases.
Progress0 / 4 steps
1
Create the DynamoDB table with primary key
Create a DynamoDB table named Products with ProductID as the partition key of type S (string). Use the AWS CLI command aws dynamodb create-table with the required attributes and key schema.
DynamoDB
Need a hint?

Use the AWS CLI create-table command with --attribute-definitions and --key-schema options.

2
Add a Global Secondary Index (GSI) on Category
Update the Products table creation command to include a Global Secondary Index named CategoryIndex with Category as the partition key of type S. Add --attribute-definitions for Category and define the GSI with --global-secondary-indexes.
DynamoDB
Need a hint?

Remember to add Category to --attribute-definitions and define the GSI with --global-secondary-indexes.

3
Insert sample product items
Insert three product items into the Products table using the AWS CLI put-item command. Use these exact items:
1. {"ProductID": {"S": "P100"}, "Name": {"S": "Smartphone"}, "Category": {"S": "Electronics"}}
2. {"ProductID": {"S": "P101"}, "Name": {"S": "Laptop"}, "Category": {"S": "Electronics"}}
3. {"ProductID": {"S": "P102"}, "Name": {"S": "Coffee Mug"}, "Category": {"S": "Kitchen"}}
DynamoDB
Need a hint?

Use aws dynamodb put-item three times with the exact JSON items.

4
Query products by Category using the GSI
Write an AWS CLI query command to find all products in the Electronics category using the Global Secondary Index CategoryIndex. Use --table-name Products, --index-name CategoryIndex, and a KeyConditionExpression with Category = :cat. Define the expression attribute value :cat as {"S": "Electronics"}.
DynamoDB
Need a hint?

Use aws dynamodb query with --index-name CategoryIndex and the correct key condition expression.