How to Use AWS CLI to Delete a DynamoDB Table with Global Secondary Index

Introduction

In this lesson, we will learn how to use AWS CLI to delete a DynamoDB table with Global Secondary Index (GSI). We will cover the basics of DynamoDB and GSI, and then discuss how to use AWS CLI to delete a DynamoDB table with GSI. We will also provide examples of how to use AWS CLI to delete a DynamoDB table with GSI.

What is DynamoDB?

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It is a serverless database that can be used to store and retrieve any amount of data, and serve any level of request traffic. DynamoDB is designed to be highly available, durable, and secure.

What is Global Secondary Index?

A Global Secondary Index (GSI) is an index that can be created on a DynamoDB table to allow for faster access to data. A GSI can be used to query data in a DynamoDB table based on different attributes than the primary key. A GSI can also be used to sort data in a DynamoDB table based on different attributes than the primary key.

How to Use AWS CLI to Delete a DynamoDB Table with Global Secondary Index

Now that we have a basic understanding of DynamoDB and GSI, let’s discuss how to use AWS CLI to delete a DynamoDB table with GSI.

Step 1: Create a DynamoDB Table

The first step is to create a DynamoDB table. To do this, you can use the AWS CLI command aws dynamodb create-table. This command will create a new DynamoDB table with the specified parameters.

Step 2: Create a Global Secondary Index

The next step is to create a Global Secondary Index (GSI) on the DynamoDB table. To do this, you can use the AWS CLI command aws dynamodb create-global-secondary-index. This command will create a new GSI on the specified DynamoDB table.

Step 3: Delete the DynamoDB Table

Once you have created the DynamoDB table and GSI, you can delete the DynamoDB table using the AWS CLI command aws dynamodb delete-table. This command will delete the specified DynamoDB table and all associated GSI.

Step 4: Clean Up

Once you have deleted the DynamoDB table, you should clean up any remaining resources associated with the table. This includes deleting any GSI associated with the table, as well as any data stored in the table.

Examples

Now that we have discussed the basics of how to use AWS CLI to delete a DynamoDB table with GSI, let’s look at some examples.

Example 1: Deleting a DynamoDB Table with GSI

In this example, we will use the AWS CLI command aws dynamodb delete-table to delete a DynamoDB table with GSI.

First, we will create a DynamoDB table called my-table with a primary key called id.

aws dynamodb create-table \
  --table-name my-table \
  --attribute-definitions AttributeName=id,AttributeType=S \
  --key-schema AttributeName=id,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Next, we will create a GSI on the my-table table called my-gsi with a primary key called name.

aws dynamodb create-global-secondary-index \
  --table-name my-table \
  --global-secondary-index-name my-gsi \
  --key-schema AttributeName=name,KeyType=HASH \
  --projection-type ALL

Finally, we will delete the my-table table and its associated GSI using the aws dynamodb delete-table command.

aws dynamodb delete-table \
  --table-name my-table

Example 2: Deleting a DynamoDB Table with GSI using AWS CDK

In this example, we will use AWS CDK with Typescript to delete a DynamoDB table with GSI.

First, we will create a new CDK stack called MyStack.

import * as cdk from '@aws-cdk/core';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
  }
}

Next, we will create a DynamoDB table called my-table with a primary key called id.

import * as dynamodb from '@aws-cdk/aws-dynamodb';

const table = new dynamodb.Table(this, 'my-table', {
  partitionKey: {
    name: 'id',
    type: dynamodb.AttributeType.STRING
  },
  readCapacity: 5,
  writeCapacity: 5
});

Then, we will create a GSI on the my-table table called my-gsi with a primary key called name.

const gsi = table.addGlobalSecondaryIndex({
  indexName: 'my-gsi',
  partitionKey: {
    name: 'name',
    type: dynamodb.AttributeType.STRING
  },
  projectionType: dynamodb.ProjectionType.ALL
});

Finally, we will delete the my-table table and its associated GSI using the table.delete method.

table.delete();

Conclusion

In this lesson, we learned how to use AWS CLI to delete a DynamoDB table with Global Secondary Index (GSI). We discussed the basics of DynamoDB and GSI, and then discussed how to use AWS CLI to delete a DynamoDB table with GSI. We also provided examples of how to use AWS CLI to delete a DynamoDB table with GSI, as well as how to use AWS CDK with Typescript to delete a DynamoDB table with GSI.

The key learnings from this lesson are:

  • DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability.
  • A Global Secondary Index (GSI) is an index that can be created on a DynamoDB table to allow for faster access to data.
  • You can use the AWS CLI command aws dynamodb delete-table to delete a DynamoDB table with GSI.
  • You can use the table.delete method in AWS CDK with Typescript to delete a DynamoDB table with GSI.
Share :