Search code examples
amazon-web-servicesamazon-dynamodbaws-amplifyaws-amplify-cli

Deleting single GSI out of many using Amplify CLI toolkit


I wanted to delete single GSI from 4 GSIs I had in a project. But the Amplify CLI toolkit only gave option to delete all GSIs. When I ran amplify update storage, the CLI toolkit asked following option in the flow:

Do you want to keep existing global secondary indexes created on your table? (Y/n) · no

Selecting "No" to this option removes all the GSI at once. (And there is no option to select and remove single GSI in amplify cli version 12.10.3)

When I selected "no" to above option and tried to push the changes to server, I got following error:

🛑 The following resources failed to deploy:
Resource Name: DynamoDBTable (AWS::DynamoDB::Table)
Event Type: update
Reason: Resource handler returned message: "Cannot perform more than one GSI creation or deletion in a single update" (RequestToken: fcd558ca-1d24-4590-d3b1-d5f57c0cc17f, HandlerErrorCode: InvalidRequest)

I tried to find a way to remove only selected GSI, but I did not find any such option.


Solution

  • I managed to delete a single GSI by going into the amplify/backend/storage/<dynamodbresourcename>/cli-inputs.json file, which had structure similar to the below:

    {
      "resourceName": "dbname",
      "tableName": "dbnane",
      "partitionKey": {
        "fieldName": "pk",
        "fieldType": "string"
      },
      "sortKey": {
        "fieldName": "sk",
        "fieldType": "string"
      },
      "gsi": [
        {
          "name": "gs1",
          "partitionKey": {
            "fieldName": "gs1pk",
            "fieldType": "string"
          },
          "sortKey": {
            "fieldName": "gs1sk",
            "fieldType": "string"
          }
        },
        {
          "name": "gs2",
          "partitionKey": {
            "fieldName": "gs2pk",
            "fieldType": "string"
          },
          "sortKey": {
            "fieldName": "gs2sk",
            "fieldType": "string"
          }
        },
        {
          "name": "gs3",
          "partitionKey": {
            "fieldName": "gs3pk",
            "fieldType": "string"
          },
          "sortKey": {
            "fieldName": "gs3sk",
            "fieldType": "string"
          }
        }
      ],
      "triggerFunctions": []
    }
    

    Then I simply removed the entry from gsi array for the GSI that I wanted to remove. For example, if I wanted to remove gs3, I removed that entry from gsi arrey, after which the json looked like following:

    {
      "resourceName": "dbname",
      "tableName": "dbname",
      "partitionKey": {
        "fieldName": "pk",
        "fieldType": "string"
      },
      "sortKey": {
        "fieldName": "sk",
        "fieldType": "string"
      },
      "gsi": [
        {
          "name": "gs1",
          "partitionKey": {
            "fieldName": "gs1pk",
            "fieldType": "string"
          },
          "sortKey": {
            "fieldName": "gs1sk",
            "fieldType": "string"
          }
        },
        {
          "name": "gs2",
          "partitionKey": {
            "fieldName": "gs2pk",
            "fieldType": "string"
          },
          "sortKey": {
            "fieldName": "gs2sk",
            "fieldType": "string"
          }
        }
      ],
      "triggerFunctions": []
    }
    

    And then ran the command amplify push storage.

    I hope this helps someone.