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.

1 Answer 1

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...
)
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Yes, personally I'd recommend using a CF template (or similar) to avoid that scenario

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.