Dynamodb: add GSI to existing Dynamodb table via CDK deploy
See original GitHub issueA way to use Table.addGlobalSecondaryIndex for existing tables so we can Add an inde to a existing table using the CDK.
Use Case
I create my DynamoDB tables via the CDK.
Now I have to add an index, so I want to ideally update the existing table and not recreate the table (no delete policy is enabled). So, in my CDK script I check (using the SDK) if the table already exists. If it doesn’t exist I use :
const table = new Table(this, tableName, options);
// and
table.addGlobalSecondaryIndex(options);
to create table and GSI.
However, if the table does already exist (in staging or prod), I am using
const table = Table.fromTableName(this, tableName, tableName);
to get a table reference ( so I can use that to get the datasource for Appsync resolvers and such).
The problem I have is that fromTableName
returns an interface of ITable
and not Table
. ITable
doesn’t have the addGlobalSecondaryIndex
function, only Table
does.
So there is no way for me (as far as I know) to add an index to an existing table, either then removing the table first (bad) or adding it manually.
I have add a hacky workaround in my CDK deploy script using the SDK where I use the updateTable
command from @aws-sdk/client-dynamodb
however that is not ideal.
Proposed Solution
If the Table.fromTableName
function would return a Table
interface, that could allow us to call addGlobalSecondaryIndex
or removeGlobalSecondaryIndex
and update our tables via the CDK.
Other
- 👋 I may be able to implement this feature request
- ⚠️ This feature might incur a breaking change
This is a 🚀 Feature Request
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:18 (9 by maintainers)
Top GitHub Comments
Hi Adam, No man, that is it. Thanks again.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.