Search code examples
amazon-dynamodbboto3python-ggplot

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.


Solution

  • 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...
    )