0
0
DynamoDBquery~10 mins

Creating GSI in DynamoDB - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating GSI
Define GSI attributes
Specify GSI key schema
Add GSI to table creation or update
DynamoDB creates GSI asynchronously
GSI becomes ACTIVE
Use GSI in queries
END
Creating a Global Secondary Index (GSI) involves defining its attributes and key schema, adding it to the table, waiting for it to become active, then using it for queries.
Execution Sample
DynamoDB
CreateTable {
  TableName: 'Books',
  AttributeDefinitions: [
    {AttributeName: 'ISBN', AttributeType: 'S'},
    {AttributeName: 'Author', AttributeType: 'S'}
  ],
  BillingMode: 'PAY_PER_REQUEST',
  KeySchema: [{AttributeName: 'ISBN', KeyType: 'HASH'}],
  GlobalSecondaryIndexes: [{
    IndexName: 'AuthorIndex',
    KeySchema: [{AttributeName: 'Author', KeyType: 'HASH'}],
    Projection: {ProjectionType: 'ALL'}
  }]
}
This creates a table 'Books' with a GSI named 'AuthorIndex' on the 'Author' attribute.
Execution Table
StepActionInputDynamoDB ResponseState Change
1Define table name and primary keyTableName='Books', KeySchema=ISBN as HASHTable creation request acceptedTable 'Books' creation started
2Define GSI attributes and key schemaGSI Name='AuthorIndex', KeySchema=Author as HASHGSI definition acceptedGSI 'AuthorIndex' added to table creation
3Send CreateTable request with GSIFull table and GSI definitionTable status: CREATINGTable and GSI creation in progress
4Wait for table and GSI to become ACTIVEPolling statusTable status: ACTIVE, GSI status: ACTIVETable and GSI ready to use
5Query using GSIQuery on 'AuthorIndex' with Author='John Doe'Query results returnedData retrieved using GSI
6End--Process complete
💡 Table and GSI become ACTIVE, ready for queries
Variable Tracker
VariableStartAfter Step 2After Step 4Final
TableNameundefined'Books''Books''Books'
PrimaryKeyundefinedISBN (HASH)ISBN (HASH)ISBN (HASH)
GSI Nameundefined'AuthorIndex''AuthorIndex''AuthorIndex'
GSI KeySchemaundefinedAuthor (HASH)Author (HASH)Author (HASH)
Table StatusundefinedCREATINGACTIVEACTIVE
GSI StatusundefinedCREATINGACTIVEACTIVE
Key Moments - 3 Insights
Why does the GSI creation take time after the table is created?
Because DynamoDB creates GSIs asynchronously, the GSI status changes from CREATING to ACTIVE after some time, as shown in execution_table step 4.
Can I query the GSI before it becomes ACTIVE?
No, queries on the GSI only work after its status is ACTIVE, as shown in execution_table step 5.
Do I need to specify all attributes in the GSI's Projection?
No, you can choose ALL, KEYS_ONLY, or INCLUDE specific attributes. In the example, ProjectionType is ALL, meaning all attributes are projected.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the GSI become ready to use?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'DynamoDB Response' and 'State Change' columns in step 4 for GSI status.
According to variable_tracker, what is the Table Status after Step 3?
AACTIVE
BCREATING
CDELETING
DUPDATING
💡 Hint
Look at the 'Table Status' row under 'After Step 2' and 'After Step 4' to infer the status at Step 3.
If the ProjectionType was changed from 'ALL' to 'KEYS_ONLY', what would change in the execution?
AThe GSI would include only key attributes in queries
BThe GSI creation would fail
CThe table would not have a primary key
DThe GSI would become ACTIVE faster
💡 Hint
ProjectionType controls which attributes are available in the GSI, see execution_sample code.
Concept Snapshot
Creating a GSI in DynamoDB:
- Define GSI name and key schema (HASH and optional RANGE keys)
- Add GSI to table creation or update request
- DynamoDB creates GSI asynchronously (status: CREATING -> ACTIVE)
- Use GSI in queries once ACTIVE
- ProjectionType controls attributes included in GSI
Full Transcript
Creating a Global Secondary Index (GSI) in DynamoDB involves defining the index's attributes and key schema, then adding it to the table creation or update request. DynamoDB processes the creation asynchronously, changing the GSI status from CREATING to ACTIVE. Only after the GSI is ACTIVE can you query it. The ProjectionType setting determines which attributes are available in the GSI. This process is shown step-by-step in the execution table and variable tracker, illustrating the state changes and key actions.