4

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.

Doug Bower
  • 319
  • 2
  • 14

1 Answers1

3

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...
)
Nicholas Sizer
  • 3,490
  • 3
  • 26
  • 29
  • 1
    It appears as if this method works well, but if you are not careful you can create multiple global secondary indexes with the same KeySchema. This would waste resources and provide no value. So this routine should probably check to see if an index already exists before creating a new GS index. – Doug Bower Apr 18 '18 at 02:23
  • Yes, personally I'd recommend using a CF template (or similar) to avoid that scenario – Nicholas Sizer Apr 18 '18 at 05:26