The IndexNotFoundException in DynamoDB means the query operation couldn’t find the specified index, which is usually an IndexName or a ProjectionExpression that implies an index.
Common Causes and Fixes for IndexNotFoundException
-
Incorrect
IndexNameProvided:- Diagnosis: Check the exact spelling and case of the
IndexNamein your query request against the actual index names defined for your table. You can list your table’s indexes using the AWS CLI:aws dynamodb list-indexes --table-name YourTableName - Fix: Ensure the
IndexNamein yourQueryAPI call or SDK method matches one of the names returned by thelist-indexescommand precisely. For example, if the CLI shows"index-name": "my-gsi-example", your query must useIndexName: "my-gsi-example". - Why it works: DynamoDB is case-sensitive for index names. A simple typo or case mismatch prevents it from locating the index metadata.
- Diagnosis: Check the exact spelling and case of the
-
Index Not Yet Active:
- Diagnosis: Global Secondary Indexes (GSIs) take time to provision and become active after creation. Check the status of your index using the AWS CLI:
Look foraws dynamodb describe-table --table-name YourTableName --query "Table.GlobalSecondaryIndexes[?IndexName=='YourIndexName'].IndexStatus" --output textIndexStatus: ACTIVE. If it’sCREATINGorUPDATING, the index is not ready. - Fix: Wait for the index status to be
ACTIVE. You can poll thedescribe-tablecommand or use CloudWatch alarms to notify you when it becomes active. Attempting to query an inactive GSI will result inIndexNotFoundException. - Why it works: DynamoDB prevents queries against indexes that are still being built or updated to ensure data consistency and prevent partial results.
- Diagnosis: Global Secondary Indexes (GSIs) take time to provision and become active after creation. Check the status of your index using the AWS CLI:
-
Querying a Local Secondary Index (LSI) with Wrong
IndexName:- Diagnosis: LSIs are created at the same time as the table and share the same provisioned throughput. Their
IndexNameis often omitted inQueryoperations, relying on the partition key to implicitly target the LSI if it shares the same partition key as the base table. However, if you explicitly provide anIndexNamefor an LSI, it must be correct. Usedescribe-tableto confirm:aws dynamodb describe-table --table-name YourTableName --query "Table.LocalSecondaryIndexes" - Fix: If you are explicitly specifying an
IndexNamefor an LSI, ensure it matches the name shown in thedescribe-tableoutput. If you’re not specifying anIndexNameand your query uses the base table’s partition key, it should implicitly use the LSI if one exists with that partition key. If you need to query a different LSI, you must provide its correctIndexName. - Why it works: While LSIs are tightly coupled with the base table, DynamoDB still requires accurate identification if an
IndexNameparameter is used, especially if multiple LSIs share partition keys.
- Diagnosis: LSIs are created at the same time as the table and share the same provisioned throughput. Their
-
Misunderstanding
ProjectionExpressionfor Index Targeting:- Diagnosis: A
ProjectionExpressiondefines which attributes are copied into an index. It is not used to specify which index to query. TheIndexNameparameter is the sole determinant of which index a query targets. If you are seeingIndexNotFoundExceptionand you haven’t specified anIndexName, DynamoDB is attempting to query the base table. - Fix: If you intend to query a specific index (GSI or LSI), you must provide the
IndexNameparameter in your query request. TheProjectionExpressionis a separate concern for controlling index size and cost. - Why it works: The
IndexNameparameter tells DynamoDB which data structure (base table or a specific index) to use for the query execution.ProjectionExpressiononly affects the data within that structure.
- Diagnosis: A
-
Using an Index that was Deleted:
- Diagnosis: If an index was previously active but has since been deleted, any application code or configuration still referencing its
IndexNamewill fail. Verify the index still exists usingaws dynamodb list-indexes --table-name YourTableName. - Fix: Remove any references to the deleted index’s name from your application’s query logic, configuration files, or infrastructure-as-code definitions.
- Why it works: The
IndexNameis a unique identifier. If that identifier no longer exists in DynamoDB’s metadata for the table, the system cannot find it.
- Diagnosis: If an index was previously active but has since been deleted, any application code or configuration still referencing its
-
Region Mismatch in SDK Configuration:
- Diagnosis: While less common for
IndexNotFoundExceptionspecifically (more typical forResourceNotFoundException), if your AWS SDK client is configured for the wrong region, it might be trying to access indexes on a table that doesn’t exist or has a different index configuration in that region. Double-check your SDK client’s region configuration. - Fix: Ensure your AWS SDK client is initialized with the correct region where your DynamoDB table and its indexes reside. For example, in Python’s Boto3:
import boto3 dynamodb = boto3.resource('dynamodb', region_name='us-east-1') - Why it works: DynamoDB tables and their indexes are region-specific. The SDK needs to communicate with the correct regional endpoint to find and query your resources.
- Diagnosis: While less common for
The next error you’ll likely encounter after fixing IndexNotFoundException is ValidationException if your query parameters (like KeyConditionExpression or FilterExpression) are malformed for the index you are now successfully targeting.