Ravikumar
Ravikumar

Reputation: 1131

Create secondary global index for existing dynamoDB table using CloudFormation

The DynamoDB table is already created and running in production. As per current use-case, planning to add new secondary global index. This can be achieved via AWS SDK, is it possible to update DynamoDB table with CloudFormation script.

Any helps will be appreciated.

Upvotes: 0

Views: 2288

Answers (2)

Sriram Umapthy
Sriram Umapthy

Reputation: 700

It is possible via cloud formation but you need to manually import the existing resource.

As in your case you need to manually import the dynamo DB to Cloudformation.

That can be achieved using these commands, refer to AWS doc.

# Create the Change set
aws cloudformation create-change-set \
  --stack-name {your cloudformation stack name} \
  --change-set-name {giveSomeNameYouLikeHere} \
  --change-set-type IMPORT \
  --resources-to-import file://resourcesToImport.json \
  --template-body file://cloudformationTemplate.json;

# Check the status of the taskset
aws cloudformation describe-change-set \
  --stack-name {your cloudformation stack name} \
  --change-set-name {giveSomeNameYouLikeHere};

# Execute the taskset
aws cloudformation execute-change-set \
  --stack-name {your cloudformation stack name} \
  --change-set-name {giveSomeNameYouLikeHere};

For the template, I think you can refer to the other comments.

The resource to import will look like this :

[
    {
        "ResourceType": "AWS::DynamoDB::Table",
        "LogicalResourceId": "{your resource id in Cloudformation for example: DDBTable}",
        "ResourceIdentifier": {
            "TableName": "{your table name}"
        }
    }
]

Upvotes: 0

Ethan Nguyen
Ethan Nguyen

Reputation: 114

create a new or overwrite your current CloudFormation script with

Resources
 DDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: YOUR_EXISTING_TABLE_NAME
      AttributeDefinitions: #List out all existing cols here
        -
          AttributeName: "ColX" # hash key
          AttributeType: "S"
        -
          AttributeName: "ColY" # range key
          AttributeType: "S"
        -
          AttributeName: "ColZ" # used for your Global Secondary Index 
          AttributeType: "S"

      KeySchema: # List out your main Hash & Range Key
        -
          AttributeName: "ColX"
          KeyType: "HASH"
        -
          AttributeName: "ColY"
          KeyType: "RANGE"

      GlobalSecondaryIndexes: #  new Global Secondary Index
      - IndexName: INDEX_NAME
        KeySchema:
        - AttributeName: ColZ #different than your main table Hash Key
          KeyType: HASH
        Projection:
          ProjectionType: ALL

Upvotes: 0

Related Questions