Step 6: Create a global secondary index - Amazon DynamoDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Step 6: Create a global secondary index

In this step, you create a global secondary index for the Music table that you created in Step 1: Create a table.

For more information about global secondary indexes, see Using Global Secondary Indexes in DynamoDB.

To use the Amazon DynamoDB console to create a global secondary index AlbumTitle-index for the Music table:

  1. Open the DynamoDB console at https://console.amazonaws.cn/dynamodb/.

  2. In the navigation pane on the left side of the console, choose Tables.

  3. Choose the Music table from the table list.

  4. Choose the Indexes tab for the Music table.

  5. Choose Create index.

    
                                Console screenshot showing the create index button.
  6. For the Partition key, enter AlbumTitle.

  7. For Index name, enter AlbumTitle-index.

  8. Leave the other settings on their default values and choose Create index.

    
                                Console screenshot showing the completed fields in the
                                    create index dialog box.

The following Amazon CLI example creates a global secondary index AlbumTitle-index for the Music table using update-table.

Linux

aws dynamodb update-table \ --table-name Music \ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S \ --global-secondary-index-updates \ "[{\"Create\":{\"IndexName\": \"AlbumTitle-index\",\"KeySchema\":[{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], \ \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5 },\"Projection\":{\"ProjectionType\":\"ALL\"}}}]"

Windows CMD

aws dynamodb update-table ^ --table-name Music ^ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S ^ --global-secondary-index-updates "[{\"Create\":{\"IndexName\": \"AlbumTitle-index\",\"KeySchema\":[{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5},\"Projection\":{\"ProjectionType\":\"ALL\"}}}]"

Using update-table returns the following sample result.

{ "TableDescription": { "TableArn": "arn:aws:dynamodb:us-west-2:111122223333:table/Music", "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "GlobalSecondaryIndexes": [ { "IndexSizeBytes": 0, "IndexName": "AlbumTitle-index", "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 10 }, "IndexStatus": "CREATING", "Backfilling": false, "KeySchema": [ { "KeyType": "HASH", "AttributeName": "AlbumTitle" } ], "IndexArn": "arn:aws:dynamodb:us-west-2:111122223333:table/Music/index/AlbumTitle-index", "ItemCount": 0 } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 10 }, "TableSizeBytes": 0, "TableName": "Music", "TableStatus": "UPDATING", "TableId": "a04b7240-0a46-435b-a231-b54091ab1017", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": 1558028402.69 } }

Note that the value of the IndexStatus field is set to CREATING.

To verify that DynamoDB has finished creating the AlbumTitle-index global secondary index, use the describe-table command.

Linux

aws dynamodb describe-table --table-name Music | grep IndexStatus

Windows CMD

aws dynamodb describe-table --table-name Music | findstr IndexStatus

This command returns the following result. The index is ready for use when the value of the IndexStatus field returned is set to ACTIVE.

"IndexStatus": "ACTIVE",

Next, you can query the global secondary index. For details, see Step 7: Query the global secondary index.