How to Create a New DynamoDB Table Using AWS CLI

Introduction

Amazon DynamoDB is a powerful NoSQL database service designed for high-performance applications requiring scalability and low-latency operations. As organizations increasingly adopt cloud-native solutions, the ability to interact with AWS DynamoDB via the AWS Command Line Interface (CLI) is a vital skill for developers and engineers. This guide provides step-by-step instructions to create a new DynamoDB table using the AWS CLI, covering key configurations and best practices.

Why Use the AWS CLI for DynamoDB Table Creation?

The AWS CLI offers several advantages over manual operations through the AWS Management Console:

  1. Automation: CLI commands can be integrated into scripts, enabling automated infrastructure provisioning.
  2. Speed: CLI-based operations reduce the need for navigating the web interface, saving time.
  3. Consistency: Using pre-defined configurations ensures repeatable and error-free deployments.

By mastering the AWS CLI, you can streamline your workflows and manage infrastructure efficiently.


Prerequisites

Before creating a DynamoDB table with the AWS CLI, ensure you have:

AWS CLI Installed: Download and install the AWS CLI from the official AWS CLI page. Verify installation with:

aws --version

AWS Credentials Configured: Set up credentials using the aws configure command:

aws configure

Provide your AWS Access Key, Secret Access Key, Region, and Output format (e.g., JSON).

IAM Permissions: Ensure the IAM role or user has the necessary permissions to create DynamoDB tables.


Steps to Create a DynamoDB Table

Understand Table Configuration
DynamoDB tables require key settings, including:

Table Name: A unique name for the table.

Primary Key: Consists of a partition key and, optionally, a sort key.

Provisioned Throughput or On-Demand Mode: Determines read and write capacity.

Optional Settings: Such as indexes, streams, and encryption.

Define the Table Parameters
Use the following template to structure your table creation command:

aws dynamodb create-table \
    --table-name <TableName> \
    --attribute-definitions AttributeName=<AttributeName>,AttributeType=<Type> \
    --key-schema AttributeName=<AttributeName>,KeyType=<KeyType> \
    --provisioned-throughput ReadCapacityUnits=<ReadUnits>,WriteCapacityUnits=<WriteUnits>

Replace the placeholders with your table-specific details.

Example: Create a Simple Table
Create a table named Movies with a partition key Year (number) and a sort key Title (string):

aws dynamodb create-table \
    --table-name Movies \
    --attribute-definitions \
        AttributeName=Year,AttributeType=N \
        AttributeName=Title,AttributeType=S \
    --key-schema \
        AttributeName=Year,KeyType=HASH \
        AttributeName=Title,KeyType=RANGE \
    --provisioned-throughput \
        ReadCapacityUnits=5 \
        WriteCapacityUnits=5

This command defines:

A partition key Year of type number (N).

A sort key Title of type string (S).

Provisioned throughput of 5 read and 5 write units.

Verify Table Creation
Check the status of your table with:

aws dynamodb describe-table --table-name Movies

Look for the TableStatus field, which should show ACTIVE when the table is ready for use.

Optional: Enable On-Demand Capacity
For dynamic workloads, use on-demand capacity instead of provisioned throughput:

aws dynamodb create-table \
    --table-name Movies \
    --attribute-definitions \
        AttributeName=Year,AttributeType=N \
        AttributeName=Title,AttributeType=S \
    --key-schema \
        AttributeName=Year,KeyType=HASH \
        AttributeName=Title,KeyType=RANGE \
    --billing-mode PAY_PER_REQUEST

Best Practices

Use Tags: Add tags to your table for better resource management:

--tags Key=Environment,Value=Production Key=Project,Value=MovieApp

Monitor Performance: Utilize Amazon CloudWatch to track table usage and optimize throughput settings.

Leverage Backups: Enable point-in-time recovery to safeguard against accidental data loss.


Conclusion

Creating a DynamoDB table using the AWS CLI is a straightforward process that offers flexibility and control over database configurations. Whether you’re setting up a simple table for a development project or a complex schema for production, the CLI streamlines the process, ensuring efficiency and repeatability. By following the steps outlined in this guide, you can confidently integrate DynamoDB into your workflows and harness its scalability for your applications.

More From Author

Leave a Reply

Recent Comments

No comments to show.