Create Your First DynamoDb Table

Amazon DynamoDB is a fully managed NoSQL database service offered by AWS. In this tutorial, we'll walk you through the process of creating your first DynamoDB table using the AWS Management Console, AWS CLI and AWS SAM.

Creating your DynamoDB table using AWS Console

Step 1: Sign in to the AWS Management Console:
Visit the AWS Management Console (console.aws.amazon.com) and sign in to your AWS account.

Step 2: Open the DynamoDB Service
Once you're signed in, search for "DynamoDB" in the AWS Management Console search bar, and click on the DynamoDB service.

Step 3: Click on "Create Table"
In the DynamoDB console, click on the "Create table" button to start the table creation process.

Step 4: Provide Table Details
In the "Create DynamoDB table" wizard, you'll need to provide the following details:

    • Table name: Enter a unique name for your table.
    • Primary key: Specify the primary key for your table. It can consist of one or two attributes: the partition key and, optionally, the sort key.
    • Provisioned capacity or on-demand: Choose whether you want to provision capacity manually or use on-demand capacity mode.

Step 5: Configure Additional Settings (Optional)
If desired, you can configure additional settings such as encryption, tags, and advanced options. However, for the purpose of creating your first table, you can leave these settings as default.

Step 6: Review and Create
Review the table details you provided and ensure everything is correct. Once you've reviewed it, click on the "Create" button to create your DynamoDB table.

Step 7: Table Creation and Availability
DynamoDB will create your table, which may take a few moments. Once the table is created, it will be available for use.

Creating your DynamoDB table using AWS CLI

Please see the following script to create a DynamoDB table using AWS CLI

#!/bin/bash

# Set your AWS Region and DynamoDB table details
REGION="ap-south-1"
TABLE_NAME="YourTableName"
PARTITION_KEY="YourPartitionKey"
SORT_KEY="YourSortKey"
READ_CAPACITY_UNITS=5
WRITE_CAPACITY_UNITS=5

# Create the DynamoDB table
aws dynamodb create-table \
  --table-name $TABLE_NAME \
  --attribute-definitions AttributeName=$PARTITION_KEY,AttributeType=S \
  --key-schema AttributeName=$PARTITION_KEY,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=$READ_CAPACITY_UNITS,WriteCapacityUnits=$WRITE_CAPACITY_UNITS \
  --region $REGION

# Wait for the table to be created
aws dynamodb wait table-exists \
  --table-name $TABLE_NAME \
  --region $REGION

# Output the table details
aws dynamodb describe-table \
  --table-name $TABLE_NAME \
  --region $REGION

Make sure to replace the placeholder values (YourTableName, YourPartitionKey, YourSortKey) with your desired table name and primary key attributes.

To use this script, you need to have the AWS CLI installed and configured with your AWS credentials. Simply save the script to a file, and give it executable permissions (chmod +x create_dynamodb_table.sh), and run it in your terminal.

The script will create a DynamoDB table with the specified table name, partition key, sort key (if applicable), and provisioned throughput. It will then wait for the table to be created and output the details of the created table.

Please note that the script assumes you have the necessary permissions to create DynamoDB tables in your AWS account.

Creating your DynamoDB table using AWS SAM

AWS SAM (serverless application model) is an open-source framework to develop and deploy serverless applications on AWS. If you are not familiar with AWS SAM or how to use it, you can refer to our articles here.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  DynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: YourTableName
      AttributeDefinitions:
        - AttributeName: YourPartitionKey
          AttributeType: S
      KeySchema:
        - AttributeName: YourPartitionKey
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5

Outputs:
  DynamoDBTableName:
    Value: !Ref DynamoDBTable

In this SAM template, we define a DynamoDB table resource with the specified table name, partition key attribute, and provisioned throughput. The !Ref DynamoDBTable expression in the Outputs section references the created DynamoDB table's logical ID and exposes it as an output value.

To use this template, save it to a file with an .yaml extension, such as dynamodb-table.yaml. Then, you can deploy the stack using the AWS CLI or AWS Management Console.

For example, using the AWS CLI, run the following command:

aws cloudformation deploy \
  --template-file dynamodb-table.yaml \
  --stack-name YourStackName \
  --capabilities CAPABILITY_IAM

Make sure to replace YourTableName it with your desired table name and YourStackName with a unique name for your CloudFormation stack.

This template creates a DynamoDB table with the specified attributes and provisioned throughput. You can customize it further by adding additional properties or modifying the template as per your requirements.

Please note that you need to have the AWS SAM CLI and AWS CLI installed and configured with your AWS credentials to deploy this template.

Congratulations! You have successfully created your first DynamoDB table. You can now start adding items and performing various operations on your table using the DynamoDB console, SDKs, or APIs.

Remember to consider your data model, access patterns, and scalability requirements when designing and working with your DynamoDB tables. DynamoDB offers various features and capabilities to optimize performance and scalability, such as partitioning, secondary indexes, and auto-scaling.

In upcoming articles, we will be talking about the above-mentioned things in more detail. Till then subscribe to AWSMAG to know more about AWS.