Albert S
Albert S

Reputation: 2602

Add Global Secondary Index to an existing table in DynamoDB using aws cli

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

Upvotes: 3

Views: 4940

Answers (3)

Bruce
Bruce

Reputation: 8879

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
            }
         }
      }
   ]
}

Upvotes: 1

mur-tha
mur-tha

Reputation: 851

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>

Upvotes: 1

notionquest
notionquest

Reputation: 39226

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"
    }
  }
  ...
]

Upvotes: 3

Related Questions