3

I can notseem to find an example on how to add Global Secondary Index to an existing table in DynamoDB using the aws cli.
This is what i know so far from the docs

Any pointers would be appreciated

3 Answers 3

3

Here is the update-table document.

Example:

aws dynamodb update-table --table-name <tableName> --global-secondary-index-updates file://gsi-command.json

Create a JSON file based with either update, create or delete action:-

Keep one of the action (update, create or delete) from below sample JSON and update the attribute definitions accordingly

[
  {
    "Update": {
      "IndexName": "string",
      "ProvisionedThroughput": {
        "ReadCapacityUnits": long,
        "WriteCapacityUnits": long
      }
    },
    "Create": {
      "IndexName": "string",
      "KeySchema": [
        {
          "AttributeName": "string",
          "KeyType": "HASH"|"RANGE"
        }
        ...
      ],
      "Projection": {
        "ProjectionType": "ALL"|"KEYS_ONLY"|"INCLUDE",
        "NonKeyAttributes": ["string", ...]
      },
      "ProvisionedThroughput": {
        "ReadCapacityUnits": long,
        "WriteCapacityUnits": long
      }
    },
    "Delete": {
      "IndexName": "string"
    }
  }
  ...
]
Sign up to request clarification or add additional context in comments.

Comments

1

There is a small section in the Options section of the Update Table documentation that mentions the required options specific to creating a new global secondary index which requires that the attribute-definitions include the key elements of the new index. Just adding that option to the end of the example provided by @notionquest should do the trick.

aws dynamodb update-table --table-name <tableName> --global-secondary-index-updates file://gsi-command.json --attribute-definitions AttributeName=<attributeName>, AttributeType=<attributeType>

Comments

1

Creating global secondary indexes in existing tables. Use this CLI command and JSON file for update.

aws dynamodb update-table --table-name sample--cli-input-json file://gsi-update.json --endpoint-url http://localhost:8000

Save the arguments in JSON format.

{  
   "AttributeDefinitions":[  
      {  
         "AttributeName":"String",
         "AttributeType":"S"
      },
      {  
         "AttributeName":"String",
         "AttributeType":"S"
      }
   ],
   "GlobalSecondaryIndexUpdates":[  
      {  
         "Create":{  
            "IndexName":"index-name",
            "KeySchema":[  
               {  
                  "AttributeName":"String",
                  "KeyType":"HASH"
               },
               {  
                  "AttributeName":"String",
                  "KeyType":"RANGE"
               }
            ],
            "Projection":{  
               "ProjectionType":"ALL"
            },
            "ProvisionedThroughput":{  
               "ReadCapacityUnits":5,
               "WriteCapacityUnits":5
            }
         }
      }
   ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.