Bird
Raised Fist0
AWScloud~10 mins

Secondary indexes (GSI, LSI) in AWS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Global Secondary Index (GSI) in a DynamoDB table.

AWS
GlobalSecondaryIndexes: [{
  IndexName: "UserEmailIndex",
  KeySchema: [{ AttributeName: "email", KeyType: [1] }],
  Projection: { ProjectionType: "ALL" },
  ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}]
Drag options to blanks, or click blank then click option'
AHASH
BRANGE
CPRIMARY
DSORT
Attempts:
3 left
💡 Hint
Common Mistakes
Using RANGE instead of HASH for the partition key.
Confusing PRIMARY key type with HASH or RANGE.
2fill in blank
medium

Complete the code to add a Local Secondary Index (LSI) with a sort key.

AWS
LocalSecondaryIndexes: [{
  IndexName: "StatusIndex",
  KeySchema: [
    { AttributeName: "userId", KeyType: "HASH" },
    { AttributeName: [1], KeyType: "RANGE" }
  ],
  Projection: { ProjectionType: "KEYS_ONLY" }
}]
Drag options to blanks, or click blank then click option'
AcreatedAt
Bemail
Cstatus
DuserName
Attempts:
3 left
💡 Hint
Common Mistakes
Using an attribute that is not part of the table's key schema.
Confusing partition key with sort key.
3fill in blank
hard

Fix the error in the GSI definition by completing the missing attribute.

AWS
GlobalSecondaryIndexes: [{
  IndexName: "OrderDateIndex",
  KeySchema: [
    { AttributeName: "orderId", KeyType: "HASH" },
    { AttributeName: [1], KeyType: "RANGE" }
  ],
  Projection: { ProjectionType: "INCLUDE", NonKeyAttributes: ["total"] },
  ProvisionedThroughput: { ReadCapacityUnits: 10, WriteCapacityUnits: 5 }
}]
Drag options to blanks, or click blank then click option'
AorderDate
BcustomerId
Ctotal
Dstatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using an attribute not part of the key schema as the sort key.
Choosing an attribute that is only projected but not part of the key.
4fill in blank
hard

Fill both blanks to define a DynamoDB table with a primary key and a GSI.

AWS
TableName: "Products",
AttributeDefinitions: [
  { AttributeName: "productId", AttributeType: "S" },
  { AttributeName: [1], AttributeType: "S" }
],
KeySchema: [
  { AttributeName: "productId", KeyType: "HASH" }
],
GlobalSecondaryIndexes: [{
  IndexName: "CategoryIndex",
  KeySchema: [
    { AttributeName: [2], KeyType: "HASH" }
  ],
  Projection: { ProjectionType: "ALL" },
  ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }
}]
Drag options to blanks, or click blank then click option'
Acategory
Bprice
Dstock
Attempts:
3 left
💡 Hint
Common Mistakes
Using different attribute names in AttributeDefinitions and KeySchema.
Using an attribute not defined in AttributeDefinitions.
5fill in blank
hard

Fill all three blanks to create a DynamoDB table with a composite primary key and a Local Secondary Index (LSI).

AWS
TableName: "Orders",
AttributeDefinitions: [
  { AttributeName: [1], AttributeType: "S" },
  { AttributeName: [2], AttributeType: "S" },
  { AttributeName: [3], AttributeType: "S" }
],
KeySchema: [
  { AttributeName: [1], KeyType: "HASH" },
  { AttributeName: [2], KeyType: "RANGE" }
],
LocalSecondaryIndexes: [{
  IndexName: "StatusIndex",
  KeySchema: [
    { AttributeName: [1], KeyType: "HASH" },
    { AttributeName: [3], KeyType: "RANGE" }
  ],
  Projection: { ProjectionType: "ALL" }
}]
Drag options to blanks, or click blank then click option'
AorderId
BcreatedAt
Cstatus
DcustomerId
Attempts:
3 left
💡 Hint
Common Mistakes
Using different partition keys for the table and LSI.
Using the same sort key for both table and LSI.

Practice

(1/5)
1. What is the main difference between a Local Secondary Index (LSI) and a Global Secondary Index (GSI) in DynamoDB?
easy
A. LSI can have a different partition key, but GSI must use the main table's partition key.
B. LSI uses the same partition key as the main table, but GSI can use a different partition key.
C. LSI and GSI both require the same partition and sort keys as the main table.
D. LSI is only for read operations, while GSI is only for write operations.

Solution

  1. Step 1: Understand LSI key structure

    LSI shares the same partition key as the main table but allows a different sort key.
  2. Step 2: Understand GSI key structure

    GSI can have a completely different partition key and sort key from the main table.
  3. Final Answer:

    LSI uses the same partition key as the main table, but GSI can use a different partition key. -> Option B
  4. Quick Check:

    LSI shares partition key, GSI can differ [OK]
Hint: LSI shares partition key; GSI can change both keys [OK]
Common Mistakes:
  • Thinking LSI can have a different partition key
  • Assuming GSI must use main table keys
  • Confusing read/write capabilities of indexes
2. Which of the following is the correct syntax to define a Global Secondary Index (GSI) in a DynamoDB table using AWS CloudFormation?
easy
A. "GlobalSecondaryIndexes": [{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "UserId", "KeyType": "HASH"}], "Projection": {"ProjectionType": "KEYS_ONLY"}}]
B. "LocalSecondaryIndexes": [{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "UserId", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}}]
C. "GlobalSecondaryIndexes": [{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "UserId", "KeyType": "RANGE"}], "Projection": {"ProjectionType": "ALL"}}]
D. "GlobalSecondaryIndexes": [{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "UserId", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}}]

Solution

  1. Step 1: Identify correct index type and key schema

    GlobalSecondaryIndexes is the correct property for GSI, and the partition key must have KeyType "HASH".
  2. Step 2: Check projection type and syntax

    ProjectionType "ALL" is valid and commonly used; syntax matches CloudFormation format.
  3. Final Answer:

    "GlobalSecondaryIndexes": [{"IndexName": "GSI1", "KeySchema": [{"AttributeName": "UserId", "KeyType": "HASH"}], "Projection": {"ProjectionType": "ALL"}}] -> Option D
  4. Quick Check:

    GSI uses GlobalSecondaryIndexes with HASH key [OK]
Hint: GSI uses GlobalSecondaryIndexes with HASH key [OK]
Common Mistakes:
  • Using LocalSecondaryIndexes for GSI
  • Setting KeyType to RANGE for partition key
  • Incorrect projection types or syntax errors
3. Given a DynamoDB table with a Global Secondary Index (GSI) on attribute Category as partition key and Price as sort key, what will the following query return?
Query on GSI where Category = 'Books' and Price between 10 and 20
medium
A. All items with Category 'Books' and Price between 10 and 20
B. All items with Price between 10 and 20 regardless of Category
C. All items with Category 'Books' regardless of Price
D. An error because Price cannot be used in query conditions

Solution

  1. Step 1: Understand GSI query conditions

    Query on GSI requires specifying the partition key (Category = 'Books') and can filter by sort key range (Price between 10 and 20).
  2. Step 2: Interpret the query result

    The query returns all items matching Category 'Books' with Price in the specified range.
  3. Final Answer:

    All items with Category 'Books' and Price between 10 and 20 -> Option A
  4. Quick Check:

    GSI query filters partition and sort keys correctly [OK]
Hint: Query GSI with partition key + sort key range filters [OK]
Common Mistakes:
  • Ignoring partition key in query
  • Thinking sort key can't be filtered
  • Expecting error on valid query
4. You created a Local Secondary Index (LSI) on a DynamoDB table but your query on the LSI returns no results even though matching items exist. What is the most likely cause?
medium
A. The LSI was created with a different partition key than the main table.
B. The query used a filter expression instead of a key condition.
C. The query did not specify the same partition key as the main table.
D. LSIs do not support queries, only scans.

Solution

  1. Step 1: Recall LSI key requirements

    LSI shares the same partition key as the main table, so queries must specify that partition key.
  2. Step 2: Analyze query behavior

    If the query does not specify the partition key, no results will be returned because partition key is mandatory for queries on LSI.
  3. Final Answer:

    The query did not specify the same partition key as the main table. -> Option C
  4. Quick Check:

    LSI queries require main table partition key [OK]
Hint: LSI queries must include main table partition key [OK]
Common Mistakes:
  • Assuming LSI can have different partition key
  • Using filter instead of key condition for partition key
  • Believing LSI does not support queries
5. You have a DynamoDB table with a partition key UserId and sort key OrderDate. You want to efficiently query all orders by Status and sort them by OrderAmount. Which secondary index should you create and how?
hard
A. Create a Global Secondary Index with partition key Status and sort key OrderAmount.
B. Create a Local Secondary Index with partition key UserId and sort key Status.
C. Create a Global Secondary Index with partition key UserId and sort key Status.
D. Create a Local Secondary Index with partition key Status and sort key OrderAmount.

Solution

  1. Step 1: Identify query requirements

    You want to query by Status (different from main partition key) and sort by OrderAmount.
  2. Step 2: Choose index type and keys

    Since Status is different from main partition key, use a Global Secondary Index with Status as partition key and OrderAmount as sort key.
  3. Final Answer:

    Create a Global Secondary Index with partition key Status and sort key OrderAmount. -> Option A
  4. Quick Check:

    GSI allows different partition and sort keys [OK]
Hint: Use GSI when partition key differs from main table [OK]
Common Mistakes:
  • Trying to use LSI with different partition key
  • Swapping partition and sort keys incorrectly
  • Choosing wrong index type for query pattern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard