aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

aws-dynamodb: Cannot add multiple dynamodb GSI.

Open sarawin-pm opened this issue 5 years ago • 49 comments

:question: General Issue

I got "Cannot perform more than one GSI creation or deletion in a single update" error when I tried to add multiple GSI to dynamodb.

const mainTable = new dynamodb.Table(this, "MainTable", {
  partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
  sortKey: { name: "sk", type: dynamodb.AttributeType.STRING },
  billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
});

mainTable.addGlobalSecondaryIndex({
  indexName: "pk-createdAt-index",
  partitionKey: { name: "pk", type: dynamodb.AttributeType.STRING },
  sortKey: { name: "createdAt", type: dynamodb.AttributeType.STRING },
});

mainTable.addGlobalSecondaryIndex({
  indexName: "__typename-index",
  partitionKey: { name: "__typename", type: dynamodb.AttributeType.STRING },
});

mainTable.addGlobalSecondaryIndex({
  indexName: "__typename-status-index",
  partitionKey: { name: "__typename", type: dynamodb.AttributeType.STRING },
  sortKey: { name: "status", type: dynamodb.AttributeType.STRING },
});

The Question

Until now, I have to uncomment code for each GSI adding section and redeploy multiple times. How can I add multiple GSI to dynamodb and deploy in single time?

Environment

  • CDK CLI Version: 1.73.0 (build eb6f3a9)
  • Module Version: 1.73.0
  • Node.js Version: v12.18.3
  • OS: Ubuntu 18.04.1
  • Language (Version): typescript 3.9.7

Other information

I think aws-sdk does not allow to add more than one GSI at the time. Is it possible that we can add one by one until every GSI are added?

sarawin-pm avatar Dec 28 '20 08:12 sarawin-pm

That is very weird... this sounds like a CloudFormation bug to me...?

@sarawin-pm can you see if you get a similar error when deploying purely through CloudFormation?

skinny85 avatar Dec 28 '20 19:12 skinny85

Yes. This is a known limitation in CloudFormation.

hoegertn avatar Dec 28 '20 19:12 hoegertn

Damn. This seems like a giant problem with the CloudFormation DynamoDB support...

skinny85 avatar Dec 28 '20 22:12 skinny85

I have two ideas. I'm not sure is it possible to implement.

  1. Can we generate cloud formation multiple times during single cdk deployment? When deployment, check whether it has GSI1 if yes then add GSI2 to cloud formation and repeat until we can have all.
  2. Can we generate GSI creation into some aws lambda during cdk deployment? and lambda handles all GSI creation task.

sarawin-pm avatar Dec 29 '20 15:12 sarawin-pm

Yes, we could add a Custom Resource that adds the GSIs one at a time.

But honestly, this seems to me like such an egregious bug with the CloudFormation DynamoDB support, I think it should be solved on the level of the CFN DynamoDB provider.

skinny85 avatar Dec 29 '20 17:12 skinny85

This has been flagged with the CloudFormation team before (https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/229). If you look at this particular answer https://github.com/aws-cloudformation/aws-cloudformation-coverage-roadmap/issues/229#issuecomment-573243450 from @luiseduardocolon who closed the issue, they believe it's the DynamoDB team that should fix this. I don't really care that much about AWS' internal politics and who's right or wrong, but I think the clear loser here is the customer.

This is especially dangerous for disaster recovery scenarios where I would need to quickly deploy the stack in a new environment. I would need some kind of script that deploys it as many times as there are indexes in my table, adding one after the other.

laurrentt avatar Feb 10 '21 22:02 laurrentt

I would strongly encourage someone to create and independently vend a construct that creates multiple GSIs in order.

With the right NPM tags it will automatically show up here: https://awscdk.io/

Once the 3rd party construct has some proven mileage on it, we will consider merging it upstream into the CDK core library.

rix0rrr avatar Mar 09 '21 10:03 rix0rrr

Pinging to see if anyone that's been following the issue has workarounds so we don't have to break our deployments up

shellscape avatar Oct 22 '21 23:10 shellscape

We used step functions to create GSI, wait it's completion and then create next GSI. We use separate CDK stack that creates only this one table that needed multiple GSI's.

Mikko-AM avatar Nov 08 '21 22:11 Mikko-AM

A semi-automated workaround I've been using, via a nodejs script invoked by CDK.

  1. For all tables that are being deployed:
  2. Pull down the stack template JSON via cloudformation.getTemplate. Access the cloudformation resource associated with the dynamodb table
  3. Compute a list of indexes to destroy, indexes to create via comparing the desired GSI state against the current one in cloudformation
  4. Invoke the stack constructor in CDK passing in the original CF's GSI state with a single index change from the desired state. The indexes to destroy are applied first.

The script is invoked by the CDK runner cdk deploy --all --app "ts-node --prefer-ts-exts scripts/iterative-dynamodb-index-update dev" --path-metadata=false, with multiple GSIs changes being applied via running the script, waiting until the index change is applied, and then re-running it.

Edit May 2022: Open Sourced @ https://github.com/ThomasRooney/reamplify/blob/master/packages/deploy/scripts/iterative-dynamodb-index-update.ts

ThomasRooney avatar Jan 25 '22 19:01 ThomasRooney

I was able to deploy a dynamodb table with more than one GSI with a single CodeBuild. It seems to me that the dynamo limitation to modify only one GSI is only for cli.

stoyan-scava avatar Jan 27 '22 09:01 stoyan-scava

I would strongly encourage someone to create and independently vend a construct that creates multiple GSIs in order.

With the right NPM tags it will automatically show up here: https://awscdk.io/

Once the 3rd party construct has some proven mileage on it, we will consider merging it upstream into the CDK core library.

Here is a first draft : https://www.npmjs.com/package/aws-dynamodb-table-multi-gsis

flochaz avatar Feb 22 '22 13:02 flochaz

I wanted to call out that you can currently create a table with multiple GSIs defined without any problems or workarounds. The CloudFormation-related issue is when you're mutating more than one index in a single changeset.

I agree that the upstream index mutation logic should be fixed for updates, but I wanted to comment that creates are not affected for folks reading this thread.

blimmer avatar Jun 01 '22 18:06 blimmer

I don't know if what I'm doing is wrong but I tried to follow @blimmer's advice and wasn't able to create a new table with 2 GSIs. I got the same error: "Cannot perform more than one GSI creation or deletion in a single update"

This is a summary of the code that I've used. I didn't find a reference in the Table docs for another way to add a GSI.

some_table = dynamodb.Table(
    ...
)
some_table.add_global_secondary_index(
    ...
)
some_table.add_global_secondary_index(
    ...
)

lmiguelmh avatar Jul 12 '22 03:07 lmiguelmh

Same issue Sep 22 using SAM which builds in a codebuild project and deploys straight to cfn. Can add one GSI at a time only. Very frustrating.

ApexOps-Scott avatar Sep 26 '22 17:09 ApexOps-Scott

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

github-actions[bot] avatar Nov 06 '22 00:11 github-actions[bot]

Yes, same here - would be great to have a proper solution for this issue available.

cfinkelstein avatar Dec 16 '22 20:12 cfinkelstein

Yes, we could add a Custom Resource that adds the GSIs one at a time.

But honestly, this seems to me like such an egregious bug with the CloudFormation DynamoDB support, I think it should be solved on the level of the CFN DynamoDB provider.

How would you make sure your lambda to create the custom resource doesn't run until the table is finished being created?

CreateTable is an asynchronous operation. Upon receiving a CreateTable request, DynamoDB immediately returns a response with a TableStatus of CREATING. After the table is created, DynamoDB sets the TableStatus to ACTIVE. You can perform read and write operations only on an ACTIVE table.

UpdateTable is an asynchronous operation; while it is executing, the table status changes from ACTIVE to UPDATING. While it is UPDATING, you cannot issue another UpdateTable request. When the table returns to the ACTIVE state, the UpdateTable operation is complete.

Suppose you want your table to have two GSIs. If you create the table in CDK then your lambda has to wait for the CreateTable to be finished, then issue one UpdateTable, then wait for THAT to be finished, then issue the second UpdateTable. If the custom resource is in on_create() it can do it all at once (up to 20 indices) but if it's in on_update() then it has to know when DynamoDB is ready for the next action.

How is everyone trying to solve this with custom resources solving this?

wz2b avatar Jan 23 '23 04:01 wz2b

I was able to deploy a dynamodb table with more than one GSI with a single CodeBuild. It seems to me that the dynamo limitation to modify only one GSI is only for cli.

I don't think that's the case. According to the API documentation, it's a DynamoDB limitation. It comes from two different constraints. First, each UpdateTable can only touch one index at a time:

GlobalSecondaryIndexUpdates An array of one or more global secondary indexes for the table. For each index in the array, you can request one action: Create - add a new global secondary index to the table. Update - modify the provisioned throughput settings of an existing global secondary index. Delete - remove a global secondary index from the table. You can create or delete only one global secondary index per UpdateTable operation.

Second, UpdateTable is asynchronous, so you can't issue two UpdateTables at the same time - you have to wait for the table status to go from UPDATING back to ACTIVE before you can issue the second one. If you're building an index on a huge table I think that means waiting for it to complete generating the index which could take a long time.

wz2b avatar Jan 23 '23 05:01 wz2b

I don't know if what I'm doing is wrong but I tried to follow @blimmer's advice and wasn't able to create a new table with 2 GSIs. I got the same error: "Cannot perform more than one GSI creation or deletion in a single update"

Me, too. According to the DynamoDB API it should work when creating the table for the first time. Maybe there is a problem in both the DynamoDB API and how CloudFormation handles it.

wz2b avatar Jan 23 '23 05:01 wz2b