Error: ValidationException: Invalid KeyConditionExpression: Attribute name is a reserved keyword;

Answered by Rafal Wilinski
Last updated: October 23rd, 2026
Solution
Invalid KeyConditionExpression: Attribute name is a reserved keyword means an attribute name in the expression conflicts with DynamoDB's expression language. Keep the stored name and reference it through an ExpressionAttributeNames alias.
For a table whose partition key is pk and sort key is timestamp, alias the sort key while retaining the partition-key equality:
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, QueryCommand } from "@aws-sdk/lib-dynamodb";
const db = DynamoDBDocumentClient.from(new DynamoDBClient({}));
const result = await db.send(new QueryCommand({
TableName: "Events",
KeyConditionExpression: "#pk = :pk AND #ts BETWEEN :start AND :end",
ExpressionAttributeNames: { "#pk": "pk", "#ts": "timestamp" },
ExpressionAttributeValues: {
":pk": "USER#123", ":start": "2026-10-01", ":end": "2026-10-31",
},
}));This example assumes string sort keys stored in a consistent sortable date format. Match the stored type and desired boundaries in your application. If timestamp is an ordinary non-key attribute, put that condition in FilterExpression instead, or use an index designed for the lookup.
Aliases begin with #; value placeholders begin with :. Define each one in the matching map and use the alias everywhere that attribute appears. The same approach works for FilterExpression, ProjectionExpression and UpdateExpression. Use the reserved-word checker to identify other conflicting names.
See query guidance and AWS documentation.
Other Common DynamoDB Errors (with Solutions)
- DynamoDB Delete Not Working
- Conditional Update Error DynamoDB
- DynamoDB Query Limit Not Working
- dynamodb unable to parse base64 string
- DynamoDB Not Supported Requires @DynamoDBTyped Or @DynamoDBTypeConverted
- dynamodb: one or more parameter values were invalid: type mismatch for index key
- Float types are not supported. Use decimal types instead.
- aws.dynamodb.converter.unmarshall not working
- DynamoDB Error All Attributes Must Be Indexed
- accessdeniedexception dynamodb
- DynamoDB stream lambda error
- error retrieving dynamodb table item serializationexception
- dynamodb the parameter cannot be converted to a numeric value NaN
- failed to list tables not authorized dynamodb
- dynamodb unable to locate credentials
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today