7

So I've been encountering this issue during serverless deployment:

ServerlessError: An error occurred: MyDdbTable - Cannot perform more than one GSI creation or deletion in a single update.

My DDB table configuration is this: Configuration in serverless.yml

      AttributeDefinitions:
          - AttributeName: externalId
            AttributeType: S
          - AttributeName: code
            AttributeType: S
          - AttributeName: accountId
            AttributeType: S
        KeySchema:
          - AttributeName: externalId
            KeyType: HASH
          - AttributeName: code
            KeyType: RANGE
        GlobalSecondaryIndexes:
          - IndexName: gsi-account-id
            KeySchema:
              - AttributeName: accountId
                KeyType: HASH
              - AttributeName: code
                KeyType: RANGE
            Projection:
              ProjectionType: ALL
          - IndexName: externalId
            KeySchema:
              - AttributeName: externalId
                KeyType: HASH
            Projection:
              ProjectionType: ALL
          - IndexName: code
            KeySchema:
              - AttributeName: code
                KeyType: RANGE
            Projection:
              ProjectionType: ALL

Additional information:

  • One (1) index is already existing which is the gsi-account-id where it has two (2) keySchema, accountId and the code
  • I added two (2) additional index which are externalId and code

Objective/s:

My goal is to add those two (2) additional indexes (externalId and code) but upon doing a serverless deployment, I always encounter the issue said above.

Questions:

  1. Do I encounter this issue because the code is already existing in the first's keySchema (gsi-account-id)?
  2. Do you have any idea/suggestion on why do I encounter this issue if the #1 is not the answer?

Thank you for those who'll help me on this. ❤️

Z Mars
  • 159
  • 1
  • 14
  • Add a GSI, deploy, repeat. – jarmod Jun 04 '21 at 18:39
  • Thank you for the reply. May I know what do you mean by adding a GSI? Because right now, what I am encountering is `Cannot perform more than one GSI creation or deletion in a single update.`. – Z Mars Jun 04 '21 at 18:41
  • 2
    Your table currently has one index (GSI). You want 3 indexes. You cannot add more than one index at a time. So, add index #2 to your template, deploy it. Then do the same for the remaining index #3. – jarmod Jun 04 '21 at 18:43
  • Oooohh. I get it now. Thank you. Let me try this out right now and will update you guys. Appreciate the help! – Z Mars Jun 04 '21 at 18:47
  • @jarmod suggestion works! Thank you so much for helping. – Z Mars Jun 04 '21 at 19:25

1 Answers1

17

Per the documentation Adding a Global Secondary Index to an Existing Table

You can only create one global secondary index per UpdateTable operation.

You'll need to add one GSI, deploy that change, then add the second and deploy it.

Jason Wadsworth
  • 8,059
  • 19
  • 32
  • 25
    Thats fecked up. – Jonesie Jun 23 '22 at 19:35
  • 5
    AWS: "use IaC". Also AWS: this – jameslol Aug 17 '22 at 06:19
  • Why is CDK like this? What is the reason behind this "only one GSI at a time" rule? – colefner Dec 20 '22 at 04:52
  • 2
    This isn’t a CDK limit, it’s a CloudFormation limit. Adding a GSI is not a small task. You are essentially telling DynamoDB to scan the entire table and make a copy of the data for the GSI. Keep in mind that DynamoDB doesn’t know if the field(s) in your index are actually in the data already or not. So every record has to be viewed, even if it won’t be included in the GSI. – Jason Wadsworth Dec 21 '22 at 14:45
  • What would be nice is a path forward. Right now you get an error, google the error, delete some code or even write some new temporary code, rinse and repeat until your template is back how you actually want it. It's quite a lot of effort. – Benjamin Carlsson Jul 30 '23 at 14:42