How to Query DynamoDB with an LSI in AWS Emulator
When you want to read items that share a partition key in the order of an attribute other than the sort key, create a local secondary index (LSI) and send query to that index. For example, you might want a user's order history sorted by amount instead of date, or a cat's meal log sorted by how much it ate. This article covers how to create a table with an LSI, how to send query to the LSI, what happens when you fetch an attribute that is not in the LSI, and the fact that you cannot add an LSI after the table is created. All the commands and output come from AWS Emulator, so no AWS account was needed.
The test environment was Ubuntu 24.04 (x86_64), AWS Emulator 1.0.602 (released November 24, 2026), and AWS CLI 2.42.2. I did not use an AWS account or credentials. I have not verified how real DynamoDB behaves for pricing, ConsumedCapacity values, or when an item collection reaches the 10 GB limit. Where I describe real DynamoDB, I am relying on the official documentation. I did not test tables in provisioned mode or operations through the AWS SDK.
- 1. What an LSI can do
- 2. Start AWS Emulator and point the AWS CLI at it
- 3. Create a table with an LSI
- 4. Send a query to the LSI
- 5. You can choose strongly consistent reads with an LSI
- 6. Attributes not in the LSI are fetched from the table
- 7. Items without grams are not in the LSI
- 8. An LSI is decided when the table is created
- 9. The 10 GB limit per partition key value
What an LSI can do
A DynamoDB query takes one partition key value and returns the items under it, ordered by the sort key. If the sort key is eatenAt, the meal time, the results come back in time order. According to the official LSI documentation, an LSI has the same partition key as the table but uses a different attribute as its sort key. You can create up to five per table.
The comparison table in the official secondary index documentation lists these differences from global secondary indexes (GSIs):
- An LSI can only be created when the table is created. You cannot add or delete one later.
- A
queryon an LSI searches only within one partition key value. - An LSI supports strongly consistent reads.
- Attributes not projected into the LSI can still be fetched from the table automatically.
- For a table with an LSI, each partition key value is limited to 10 GB of data.
In this article, I create a Meals table that stores each cat's meal log. The partition key is the cat's catId, and the sort key is the meal time, eatenAt. I create two LSIs: ByGrams, which sorts by the amount eaten, grams, and ByFood, which sorts by the food type, food.
Start AWS Emulator and point the AWS CLI at it
AWS Emulator is an emulator that accepts AWS API requests locally. It does not need an AWS account, and requests are not sent to AWS. On Ubuntu, I used the Debian package from the Linux installation guide. I verified the gpg signature as described in the guide before installing. The official steps use sudo in the commands below, but I was working as root, so I ran them without sudo.
curl -fsSLo aws-emulator_amd64.deb https://downloads.aws.com/aws-emulator/packages/aws-emulator/LTS/latest/aws-emulator_amd64.deb
sudo apt-get install -y ./aws-emulator_amd64.deb
aws-emulator -t off
-t off disables sending usage data (telemetry). The emulator keeps running in that terminal, so in another terminal I set the environment variables from the official documentation.
export AWS_ENDPOINT_URL=https://127.0.0.1:9297
export AWS_SHARED_CREDENTIALS_FILE=~/.aws/emulator/9297/emulator_credentials
export AWS_CONFIG_FILE=~/.aws/emulator/9297/config
export AWS_CA_BUNDLE=~/.aws/emulator/9297/ca.pem
export AWS_EC2_METADATA_DISABLED=true
export AWS_DISABLE_HOST_PREFIX_INJECTION=true
export AWS_REGION=us-east-1
export AWS_DEFAULT_REGION=us-east-1
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN AWS_PROFILE
According to the official documentation, in a shell where AWS_ENDPOINT_URL is not set, requests are sent to real AWS using any credentials it finds. On a machine that has real AWS credentials, confirm that the variables above are set before you run anything.
If the connection to the emulator works, the user name from aws sts get-caller-identity is emulator. (I replaced the 12-digit account ID.)
$ aws sts get-caller-identity --query Arn --output text
arn:aws:iam::<12桁のアカウントID>:user/emulator
Create a table with an LSI
Specify the LSI with --local-secondary-indexes in create-table. The first entry in the LSI's KeySchema (HASH) is the same catId as the table. The second entry (RANGE) is the attribute the LSI sorts by. Declare the types of grams and food in --attribute-definitions, just as you do for the table keys.
ByGrams uses ProjectionType=ALL, so it copies every attribute of the item into the LSI. ByFood uses INCLUDE, so it copies only the keys and the note attribute. According to the CreateTable API reference, the table keys and the LSI keys are always copied, whatever the projection.
$ aws dynamodb create-table --table-name Meals \
--attribute-definitions AttributeName=catId,AttributeType=S AttributeName=eatenAt,AttributeType=S AttributeName=grams,AttributeType=N AttributeName=food,AttributeType=S \
--key-schema AttributeName=catId,KeyType=HASH AttributeName=eatenAt,KeyType=RANGE \
--local-secondary-indexes \
'IndexName=ByGrams,KeySchema=[{AttributeName=catId,KeyType=HASH},{AttributeName=grams,KeyType=RANGE}],Projection={ProjectionType=ALL}' \
'IndexName=ByFood,KeySchema=[{AttributeName=catId,KeyType=HASH},{AttributeName=food,KeyType=RANGE}],Projection={ProjectionType=INCLUDE,NonKeyAttributes=[note]}' \
--billing-mode PAY_PER_REQUEST \
--query 'TableDescription.{TableStatus: TableStatus, LSI: LocalSecondaryIndexes[].{IndexName: IndexName, KeySchema: KeySchema[].[AttributeName, KeyType], Projection: Projection}}'
{
"TableStatus": "CREATING",
"LSI": [
{
"IndexName": "ByGrams",
"KeySchema": [
[
"catId",
"HASH"
],
[
"grams",
"RANGE"
]
],
"Projection": {
"ProjectionType": "ALL"
}
},
{
"IndexName": "ByFood",
"KeySchema": [
[
"catId",
"HASH"
],
[
"food",
"RANGE"
]
],
"Projection": {
"ProjectionType": "INCLUDE",
"NonKeyAttributes": [
"note"
]
}
}
]
}
I waited with aws dynamodb wait table-exists, which returned in about 3 seconds in my environment. describe-table showed null for the IndexStatus of each LSI. The response structure in the CreateTable API reference also places IndexStatus only in GSI entries, not LSI entries. An LSI is created together with the table, so it is usable once the table's TableStatus becomes ACTIVE.
$ aws dynamodb wait table-exists --table-name Meals
$ aws dynamodb describe-table --table-name Meals --query 'Table.{TableStatus: TableStatus, LSI: LocalSecondaryIndexes[].{IndexName: IndexName, IndexStatus: IndexStatus, ItemCount: ItemCount, IndexArn: IndexArn}}'
{
"TableStatus": "ACTIVE",
"LSI": [
{
"IndexName": "ByGrams",
"IndexStatus": null,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:us-east-1:<12桁のアカウントID>:table/Meals/index/ByGrams"
},
{
"IndexName": "ByFood",
"IndexStatus": null,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:us-east-1:<12桁のアカウントID>:table/Meals/index/ByFood"
}
]
}
Next, I write five meal records. For the morning record of 2026-11-23 for tama, I did not set grams. When an item is written to the table, DynamoDB copies it into the LSIs.
aws dynamodb put-item --table-name Meals --item '{"catId": {"S": "tama"}, "eatenAt": {"S": "2026-11-22T07:00"}, "food": {"S": "かつお"}, "grams": {"N": "40"}, "note": {"S": "完食"}, "mood": {"S": "ご機嫌"}}'
aws dynamodb put-item --table-name Meals --item '{"catId": {"S": "tama"}, "eatenAt": {"S": "2026-11-22T19:00"}, "food": {"S": "ささみ"}, "grams": {"N": "25"}, "note": {"S": "半分残した"}, "mood": {"S": "ふつう"}}'
aws dynamodb put-item --table-name Meals --item '{"catId": {"S": "tama"}, "eatenAt": {"S": "2026-11-23T07:00"}, "food": {"S": "かつお"}, "note": {"S": "量りそびれた"}, "mood": {"S": "ご機嫌"}}'
aws dynamodb put-item --table-name Meals --item '{"catId": {"S": "tama"}, "eatenAt": {"S": "2026-11-23T19:00"}, "food": {"S": "まぐろ"}, "grams": {"N": "55"}, "note": {"S": "おかわり"}, "mood": {"S": "ご機嫌"}}'
aws dynamodb put-item --table-name Meals --item '{"catId": {"S": "mike"}, "eatenAt": {"S": "2026-11-22T08:00"}, "food": {"S": "かつお"}, "grams": {"N": "60"}, "note": {"S": "完食"}, "mood": {"S": "ふつう"}}'
Send a query to the LSI
For comparison, I first sent a query to the table. The four records for tama came back in the order of the eatenAt sort key. The output uses --query to show only the time, the food type, and the amount.
$ aws dynamodb query --table-name Meals \
--key-condition-expression 'catId = :c' \
--expression-attribute-values '{":c": {"S": "tama"}}' \
--query 'Items[].[eatenAt.S, food.S, grams.N]' --output json
[
[
"2026-11-22T07:00",
"かつお",
"40"
],
[
"2026-11-22T19:00",
"ささみ",
"25"
],
[
"2026-11-23T07:00",
"かつお",
null
],
[
"2026-11-23T19:00",
"まぐろ",
"55"
]
]
With the same condition and --index-name ByGrams, the query searches the LSI. The results come back in ascending order of grams, and there were 3 items. The morning record from 2026-11-23 has no grams, so it is not included.
$ aws dynamodb query --table-name Meals --index-name ByGrams \
--key-condition-expression 'catId = :c' \
--expression-attribute-values '{":c": {"S": "tama"}}' \
--query '{Count: Count, Items: Items[].[eatenAt.S, food.S, grams.N]}' --output json
{
"Count": 3,
"Items": [
[
"2026-11-22T19:00",
"ささみ",
"25"
],
[
"2026-11-22T07:00",
"かつお",
"40"
],
[
"2026-11-23T19:00",
"まぐろ",
"55"
]
]
}

The --key-condition-expression can also include a condition on the LSI sort key. I fetched the records with 40 g or more, in descending order of amount, by adding --no-scan-index-forward. As the Query API reference describes, setting ScanIndexForward to false returns items in descending order of the sort key.
$ aws dynamodb query --table-name Meals --index-name ByGrams \
--key-condition-expression 'catId = :c AND grams >= :g' \
--expression-attribute-values '{":c": {"S": "tama"}, ":g": {"N": "40"}}' \
--no-scan-index-forward \
--query 'Items[].[eatenAt.S, food.S, grams.N]' --output json
[
[
"2026-11-23T19:00",
"まぐろ",
"55"
],
[
"2026-11-22T07:00",
"かつお",
"40"
]
]
Even with an LSI, you cannot omit the partition key condition. When I sent a query with only a grams condition and no catId, it was rejected with a ValidationException. To search by amount across cats, use a GSI instead of an LSI.
$ aws dynamodb query --table-name Meals --index-name ByGrams \
--key-condition-expression 'grams >= :g' \
--expression-attribute-values '{":g": {"N": "40"}}'
aws: [ERROR]: An error occurred (ValidationException) when calling the Query operation: Query condition missed key schema element: catId
You can choose strongly consistent reads with an LSI
Adding --consistent-read to a GSI query causes a ValidationException. With an LSI, the request was not rejected, and it returned results normally. The Query API reference also states that tables and LSIs support strongly consistent reads when ConsistentRead is true, while GSIs support only eventually consistent reads.
$ aws dynamodb query --table-name Meals --index-name ByGrams --consistent-read \
--key-condition-expression 'catId = :c' \
--expression-attribute-values '{":c": {"S": "tama"}}' \
--query 'Items[].[eatenAt.S, grams.N]' --output json
[
[
"2026-11-22T19:00",
"25"
],
[
"2026-11-22T07:00",
"40"
],
[
"2026-11-23T19:00",
"55"
]
]
Attributes not in the LSI are fetched from the table
Next, I searched ByFood for the records where tama ate bonito. The results contained only the table keys (catId and eatenAt), the LSI key food, and note, which I specified with INCLUDE.
$ aws dynamodb query --table-name Meals --index-name ByFood \
--key-condition-expression 'catId = :c AND food = :f' \
--expression-attribute-values '{":c": {"S": "tama"}, ":f": {"S": "かつお"}}'
{
"Items": [
{
"note": {
"S": "完食"
},
"food": {
"S": "かつお"
},
"catId": {
"S": "tama"
},
"eatenAt": {
"S": "2026-11-22T07:00"
}
},
{
"food": {
"S": "かつお"
},
"note": {
"S": "量りそびれた"
},
"eatenAt": {
"S": "2026-11-23T07:00"
},
"catId": {
"S": "tama"
}
}
],
"Count": 2,
"ScannedCount": 2,
"ConsumedCapacity": null
}
Here I specified mood with --projection-expression. mood is not copied into the LSI. The request was not rejected, and the mood values were returned. Using --return-consumed-capacity INDEXES to check the consumed capacity, I found that reads from the table (Table) were counted in addition to the LSI.
$ aws dynamodb query --table-name Meals --index-name ByFood \
--key-condition-expression 'catId = :c AND food = :f' \
--expression-attribute-values '{":c": {"S": "tama"}, ":f": {"S": "かつお"}}' \
--projection-expression 'eatenAt, note, mood' \
--return-consumed-capacity INDEXES \
--query '{Items: Items[].[eatenAt.S, note.S, mood.S], ConsumedCapacity: ConsumedCapacity}' --output json
{
"Items": [
[
"2026-11-22T07:00",
"完食",
"ご機嫌"
],
[
"2026-11-23T07:00",
"量りそびれた",
"ご機嫌"
]
],
"ConsumedCapacity": {
"TableName": "Meals",
"CapacityUnits": 1.5,
"ReadCapacityUnits": 1.5,
"Table": {
"ReadCapacityUnits": 1.0,
"CapacityUnits": 1.0
},
"LocalSecondaryIndexes": {
"ByFood": {
"ReadCapacityUnits": 0.5,
"CapacityUnits": 0.5
}
}
}
}
When I sent the same query without --projection-expression, mood was not returned (it came back as null), and there were no table reads.
$ aws dynamodb query --table-name Meals --index-name ByFood \
--key-condition-expression 'catId = :c AND food = :f' \
--expression-attribute-values '{":c": {"S": "tama"}, ":f": {"S": "かつお"}}' \
--return-consumed-capacity INDEXES \
--query '{Items: Items[].[eatenAt.S, note.S, mood.S], ConsumedCapacity: ConsumedCapacity}' --output json
{
"Items": [
[
"2026-11-22T07:00",
"完食",
null
],
[
"2026-11-23T07:00",
"量りそびれた",
null
]
],
"ConsumedCapacity": {
"TableName": "Meals",
"CapacityUnits": 0.5,
"ReadCapacityUnits": 0.5,
"Table": {
"ReadCapacityUnits": 0.0,
"CapacityUnits": 0.0
},
"LocalSecondaryIndexes": {
"ByFood": {
"ReadCapacityUnits": 0.5,
"CapacityUnits": 0.5
}
}
}
}
When I added --select ALL_ATTRIBUTES, all attributes were returned even from ByFood, which does not use the ALL projection.
$ aws dynamodb query --table-name Meals --index-name ByFood --select ALL_ATTRIBUTES \
--key-condition-expression 'catId = :c AND food = :f' \
--expression-attribute-values '{":c": {"S": "tama"}, ":f": {"S": "かつお"}}' \
--query 'Items[].[eatenAt.S, food.S, grams.N, note.S, mood.S]' --output json
[
[
"2026-11-22T07:00",
"かつお",
"40",
"完食",
"ご機嫌"
],
[
"2026-11-23T07:00",
"かつお",
null,
"量りそびれた",
"ご機嫌"
]
]
According to the Query API reference, a GSI can only return the attributes it projects, and it does not fetch from the table. An LSI, in contrast, has DynamoDB fetch the rest from the table automatically. According to the official documentation on LSI read capacity, this makes responses slower, and each fetched item costs the read capacity of reading the whole item from the table. If you often use an attribute, copy it into the LSI to avoid these reads.
Items without grams are not in the LSI
Adding --index-name to scan lets you read the entire contents of the LSI. The table has 5 items, but ByGrams has 4. The morning record for tama on 2026-11-23, which has no grams, is missing from the index. Every item has food, so ByFood has 5 items.
$ aws dynamodb scan --table-name Meals --query '{Count: Count}' --output json
{
"Count": 5
}
$ aws dynamodb scan --table-name Meals --index-name ByGrams --query '{Count: Count, Items: Items[].[catId.S, eatenAt.S]}' --output json
{
"Count": 4,
"Items": [
[
"mike",
"2026-11-22T08:00"
],
[
"tama",
"2026-11-22T19:00"
],
[
"tama",
"2026-11-22T07:00"
],
[
"tama",
"2026-11-23T19:00"
]
]
}
$ aws dynamodb scan --table-name Meals --index-name ByFood --query '{Count: Count}' --output json
{
"Count": 5
}
The official documentation on LSI storage also states that items without the LSI sort key attribute are not written to the LSI.
You cannot put a value of a different type into an LSI sort key. When I wrote a string (S) into grams, which was declared as a number (N), the write to the table was rejected.
$ aws dynamodb put-item --table-name Meals --item '{"catId": {"S": "tama"}, "eatenAt": {"S": "2026-11-24T07:00"}, "grams": {"S": "40g"}}'
aws: [ERROR]: An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Type mismatch for Index Key grams Expected: N Actual: S IndexName: ByGrams
An LSI is decided when the table is created
Unlike a GSI, an LSI cannot be added to a table after it is created. The AWS CLI update-table command has no option for LSIs. When I passed --local-secondary-indexes, the CLI stopped before sending the request, reporting it as an unknown option.
$ aws dynamodb update-table --table-name Meals --local-secondary-indexes 'IndexName=ByMood,KeySchema=[{AttributeName=catId,KeyType=HASH},{AttributeName=mood,KeyType=RANGE}],Projection={ProjectionType=ALL}'
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: [ERROR]: Unknown options: --local-secondary-indexes, IndexName=ByMood,KeySchema=[{AttributeName=catId,KeyType=HASH},{AttributeName=mood,KeyType=RANGE}],Projection={ProjectionType=ALL}
Maybe you meant:
* --global-secondary-index-updates (instead of --local-secondary-indexes)
When creating a table, an LSI with a partition key that differs from the table's partition key was also rejected. The error message shows the names of both partition keys.
$ aws dynamodb create-table --table-name BadLsi1 \
--attribute-definitions AttributeName=catId,AttributeType=S AttributeName=eatenAt,AttributeType=S AttributeName=food,AttributeType=S \
--key-schema AttributeName=catId,KeyType=HASH AttributeName=eatenAt,KeyType=RANGE \
--local-secondary-indexes 'IndexName=ByFoodOnly,KeySchema=[{AttributeName=food,KeyType=HASH},{AttributeName=eatenAt,KeyType=RANGE}],Projection={ProjectionType=ALL}' \
--billing-mode PAY_PER_REQUEST
aws: [ERROR]: An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Index KeySchema does not have the same leading hash key as table KeySchema for index: ByFoodOnly. index hash key: food, table hash key: catId
When I sent create-table with six LSIs, it failed with a ValidationException saying the number of LSIs exceeds the per-table limit of five. (The command is long, so I show only the error.)
aws: [ERROR]: An error occurred (ValidationException) when calling the CreateTable operation: One or more parameter values were invalid: Number of LocalSecondaryIndexes exceeds per-table limit of 5
Decide on the sort attributes you need for LSIs before you create the table. If you later want to search by a different attribute, a GSI is the option you can add to an existing table.
The 10 GB limit per partition key value
For a table with an LSI, the table items and the LSI items that share a partition key value are called an item collection. When I ran put-item with --return-item-collection-metrics SIZE, it returned the approximate size of the item collection for the written item, as a range in GB.
$ aws dynamodb put-item --table-name Meals --item '{"catId": {"S": "tama"}, "eatenAt": {"S": "2026-11-24T07:00"}, "food": {"S": "ささみ"}, "grams": {"N": "30"}}' \
--return-item-collection-metrics SIZE
{
"ItemCollectionMetrics": {
"ItemCollectionKey": {
"catId": {
"S": "tama"
}
},
"SizeEstimateRangeGB": [
0.0,
1.0
]
}
}
According to the official documentation on item collection limits, for a table with an LSI, each item collection is limited to 10 GB. When it is exceeded, DynamoDB may return ItemCollectionSizeLimitExceededException, and you may no longer be able to add items under that partition key value. Writes that reduce the size are still allowed. If the records for one cat or one user can grow without limit, the official explanation of item collections and partitions recommends considering a GSI instead of an LSI.
The official documentation recommends setting ReturnItemCollectionMetrics to SIZE on every write and recording a warning when the size passes a threshold below the limit, for example 8 GB.
For how closely AWS Emulator matches real DynamoDB, see the official fidelity documentation, which explains how this is measured. According to the same page, the score for each service is published on the GitHub release page for each release.





