I was having trouble with updating a Global Secondary Index (GSI) in AWS DynamoDB. But I got this gist from @toastdriven to kick me in the right direction. Here’s how you do it with the boto.dynamodb2
library.
First off, make sure that you have the latest boto
(at least 2.20.1
) release, as it has support for DynamoDB GSI.
pip install -U boto
My table looks like this when described:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [35]: table.describe() | |
Out[35]: | |
{u'Table': {u'AttributeDefinitions': [{u'AttributeName': u'Subject', | |
u'AttributeType': u'S'}, | |
{u'AttributeName': u'postDate', u'AttributeType': u'S'}, | |
{u'AttributeName': u'postId', u'AttributeType': u'N'}], | |
u'CreationDateTime': 1386880069.946, | |
u'GlobalSecondaryIndexes': [{u'IndexName': u'gsiPosts', | |
u'IndexSizeBytes': 204, | |
u'IndexStatus': u'ACTIVE', | |
u'ItemCount': 5, | |
u'KeySchema': [{u'AttributeName': u'postId', u'KeyType': u'HASH'}, | |
{u'AttributeName': u'Subject', u'KeyType': u'RANGE'}], | |
u'Projection': {u'ProjectionType': u'KEYS_ONLY'}, | |
u'ProvisionedThroughput': {u'ReadCapacityUnits': 1, | |
u'WriteCapacityUnits': 1}}], | |
u'ItemCount': 5, | |
u'KeySchema': [{u'AttributeName': u'postId', u'KeyType': u'HASH'}, | |
{u'AttributeName': u'postDate', u'KeyType': u'RANGE'}], | |
u'LocalSecondaryIndexes': [{u'IndexName': u'lsiPosts', | |
u'IndexSizeBytes': 244, | |
u'ItemCount': 5, | |
u'KeySchema': [{u'AttributeName': u'postId', u'KeyType': u'HASH'}, | |
{u'AttributeName': u'postDate', u'KeyType': u'RANGE'}], | |
u'Projection': {u'ProjectionType': u'ALL'}}], | |
u'ProvisionedThroughput': {u'LastDecreaseDateTime': 1387214748.435, | |
u'NumberOfDecreasesToday': 0, | |
u'ReadCapacityUnits': 1, | |
u'WriteCapacityUnits': 1}, | |
u'TableName': u'MailPostsExperimet', | |
u'TableSizeBytes': 244, | |
u'TableStatus': u'ACTIVE'}} |
You can see that the table is called MailPostsExperimet
and my only GSI is called gsiPosts
. The GSI has a read/write provisioning of 1/1 initially. To increase the provisioning run:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
connection.update_table( | |
table_name='MailPostsExperimet', | |
global_secondary_index_updates=[ | |
{ | |
"Update": { | |
"IndexName": "gsiPosts", | |
"ProvisionedThroughput": { | |
"ReadCapacityUnits": 2, | |
"WriteCapacityUnits": 2 | |
} | |
} | |
} | |
] | |
) |
That’s it!