Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a DynamoDB global secondary Index via Python/Boto3

Is it possible to add a Global Secondary Index to and existing DynamoDB table AFTER it has been created? I am using Python 3.x with Boto3 and have not been able to find any examples of them being added to the table after it was created.

like image 706
Doug Bower Avatar asked Oct 19 '25 13:10

Doug Bower


1 Answers

In general, yes it is possible to add a Global Secondary Index (GSI) after the table is created.

However, it can take a long time for the change to come into effect, because building the GSI requires a table scan.

In the case of boto3, have a look at the documentation for update_table

For example, you try something like this:

response = client.update_table(
    TableName = 'YourTableName',
    # ...snip...
    GlobalSecondaryIndexUpdates=[
        {
            'Create': {
                'IndexName': 'YourGSIName',
                'KeySchema': [
                    {
                        'AttributeName': 'YourGSIFieldName',
                        'KeyType': 'HASH'
                    }
                ],
                'Projection': {
                    'ProjectionType': 'ALL'
                },
                'ProvisionedThroughput': {
                    'ReadCapacityUnits': 1,
                    'WriteCapacityUnits': 1
                }
            }
        }
    ],
    # ...snip...
)
like image 133
Nicholas Sizer Avatar answered Oct 21 '25 16:10

Nicholas Sizer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!