Solved: cannot perform more than one GSI creation or deletion in a single update

cannot perform more than one GSI creation or deletion in a single update

Error Message :

cannot perform more than one GSI creation or deletion in a single update

Problem:

I was trying to add multiple GSI on an existing table using CloudFormation updatestack.

Initially, I created a DynamoDB table with two GSI Location-index and Department-index. After few days of initial table creation, I needed to add two more index on my table.

  • JobTitle-index
  • ProjectId-index

Therefore, I updated my DynamoDB template like below and tried to update my existing CloudFormation stack

AWSTemplateFormatVersion: 2010-09-09
Description: AWS CloudFormation Template To Create a DynamoDB With GSI

Resources:
  EmployeeTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Employee
      AttributeDefinitions:
        - 
          AttributeName: EmployeeId
          AttributeType: S
        - 
          AttributeName: LocationId
          AttributeType: S
        - 
          AttributeName: DepartmentId
          AttributeType: S
        - 
          AttributeName: JobTitle
          AttributeType: S
        - 
          AttributeName: ProjectId
          AttributeType: S
      KeySchema:
        - 
          AttributeName: EmployeeId
          KeyType: HASH
      GlobalSecondaryIndexes:
        -
          IndexName: Location-index
          KeySchema:
            - 
              AttributeName: LocationId
              KeyType: HASH
          Projection:
            ProjectionType: ALL
        -
          IndexName: Department-index
          KeySchema:
            - 
              AttributeName: DepartmentId
              KeyType: HASH
          Projection:
            ProjectionType: ALL
        -
          IndexName: JobTitle-index
          KeySchema:
            - 
              AttributeName: LocationId
              KeyType: HASH
          Projection:
            ProjectionType: ALL
        -
          IndexName: ProjectId-index
          KeySchema:
            - 
              AttributeName: DepartmentId
              KeyType: HASH
          Projection:
            ProjectionType: ALL
      BillingMode: PAY_PER_REQUEST
 
Outputs:
  Employee:
    Description: Table Created using this template.
    Value: !Ref EmployeeTable

My Stack Update Failed With Below Error !!!

cannot perform more than one GSI creation or deletion in a single update

Why?

I was trying to find why to above problem. Little bit of research on the same took me to conclusion that, CloudFormation doesn’t allow you add or delete more then one GSI in a single update.

Note: Please note that, you can create a table with more then one GSI at a time when you initially create your stack. Only during update operation this is not allowed.

Suggested Read: Create a DynamoDB table with Global Secondary Index using CloudFormation

What do we do then?

Solution:

Well, the solution or to be honest the workaround is really simple.

  • Add one GSI and trigger the update on your stack.
  • After successful updation, check DynamoDB table to see if added index is created and is in active state.
  • Then add or remove another index and do another deployment.

Or If you know all the index you need to add at the time of table creation

  • Add all of them and create your stack as create stack allows more then one GSI addition.

Basically, you need to break your deployment into steps so that in each deployment you are doing a single addition or deletion of Global Secondary Index during stack update operation.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

Suggested Read:

Leave a Reply

Your email address will not be published. Required fields are marked *