How to Create DynamoDB Table using AWS CLI
When it comes to creating and managing resources in AWS, there are quite a few options to us like AWS Management Console, CLI, SDK, CDK, CloudFormation etc.
However, nothing beats the ease of quickly hitting a CLI command to see how something will work. AWS CLI is a great tool that helps you automate mundane tasks.
Suggested Read: 5 Ways to Create and Manage Resources in AWS You Should Know
In today’s post, you are going to learn how to create DynamoDB table using AWS CLI. We will cover below scenarios
- simple table creation with on demand capacity
- complex table creation with provisioned capacity using json file
- Global Secondary Index creation
- Table deletion
Prerequisite:
Overview of DynamoDB
DynamoDB is a fully managed, scalable and highly available serverless NoSQL service provided by AWS.
- It provides single digit milli second performance at any scale
- Frees you up from database management administrative tasks
- Can store and retrieve any amount of data
There are quite a few ways in which you can interact with DynamoDB. For example –
- Using AWS Console
- AWS CLI
- CloudFormation
- SDK
- CDK
- Terraform etc.
Today, we are going to learn how to create a DynamoDB table using AWS CLI
Steps to Create a DynamoDB Table using AWS CLI
- Install & Configure AWS CLI
- Create a Simple DynamoDB Table using CLI
- Create Complex DynamoDB table using JSON File
- Verify the Created Tables
- Clean Up
1. Install & Configure AWS ALI
Before you can create a DynamoDB table using AWS CLI, you need to install and configure it. If you already have it configured, feel free to move to next step.
Install latest version of CLI from here. Once installed, you need to configure it with your credential so that it can communicate with services in your account on your behalf.
You can configure CLI using below command-
aws configure
Once you hit enter, it will ask you details like access key, secret key, region etc. Provide these information one by one and you are done. This is how it looks like-
If you are a beginner and need help in installing and configuring CLI, here is a detailed post on How to Install and Configure AWS CLI.
Important Tip: If you don’t want to install and configure AWS CLI and still want proceed with this tutorial, Use AWS CloudShell. It will give you terminal access from AWS console itself.
You can start a CloudShell session from AWS console by clicking terminal sign as highlighted below-
2. Create a Simple DynamoDB Table using CLI
A good way to work with CLI is, to always make use of help command. And, you will never face any problem while working with CLI. The help command is gonna be your guiding angel during your CLI journey.
Since, we are going to create a DynamoDB table, let’s run below command
aws dynamodb help
and you get all the available commands of dynamodb –
o batch-execute-statement
o batch-get-item
o batch-write-item
o create-backup
o create-global-table
o create-table
o delete-backup
o delete-item
o delete-table
o describe-backup
o describe-continuous-backups
o describe-contributor-insights
o describe-endpoints
o describe-export
o describe-global-table
o describe-global-table-settings
o describe-kinesis-streaming-destination
o describe-limits
o describe-table
o describe-table-replica-auto-scaling
o describe-time-to-live
o disable-kinesis-streaming-destination
o enable-kinesis-streaming-destination
o execute-statement
o execute-transaction
o export-table-to-point-in-time
o get-item
o help
o list-backups
o list-contributor-insights
o list-exports
o list-global-tables
o list-tables
o list-tags-of-resource
o put-item
o query
o restore-table-from-backup
o restore-table-to-point-in-time
o scan
o tag-resource
o transact-get-items
o transact-write-items
o untag-resource
o update-continuous-backups
o update-contributor-insights
o update-global-table
o update-global-table-settings
o update-item
o update-table
o update-table-replica-auto-scaling
o update-time-to-live
o wait
o wizard
To create a DynamoDB table, we will use create-table command.
Let’s try to run help command again but this time with create-table like shown below-
aws dynamodb create-table help
As soon as you hit enter, you get detailed description with all the available parameter that you can use with this command.
create-table
--attribute-definitions <value>
--table-name <value>
--key-schema <value>
[--local-secondary-indexes <value>]
[--global-secondary-indexes <value>]
[--billing-mode <value>]
[--provisioned-throughput <value>]
[--stream-specification <value>]
[--sse-specification <value>]
[--tags <value>]
[--table-class <value>]
[--cli-input-json | --cli-input-yaml]
[--generate-cli-skeleton <value>]
On scrolling down in the CLI response, you will see the detail of each parameter and what’s the value supported by those parameter. Always make sure to read those as it will help you.
Lets go ahead with an example to crate a table
AWS DynamoDB create-table CLI example
We are going to create a simple table with a partition key, one range key and the billing mode set to On Demand.
Table Details:
- Table Name: Employee
- Partition Key: EmployeeId
- Range Key/Sort Key: Department
- Billing Mode: On Demand
Let’s prepare the CLI command
aws dynamodb create-table \
--table-name Employee \
--attribute-definitions AttributeName=EmployeeId,AttributeType=S AttributeName=Department,AttributeType=S \
--key-schema AttributeName=EmployeeId,KeyType=HASH AttributeName=Department,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST
Note: Please note that ‘\’ is used in above command to break it in multiple lines for better readability.
As soon as you hit enter, you get response like below-
{
"TableDescription": {
"AttributeDefinitions": [
{
"AttributeName": "Department",
"AttributeType": "S"
},
{
"AttributeName": "EmployeeId",
"AttributeType": "S"
}
],
"TableName": "Employee",
"KeySchema": [
{
"AttributeName": "EmployeeId",
"KeyType": "HASH"
},
{
"AttributeName": "Department",
"KeyType": "RANGE"
}
],
"TableStatus": "CREATING",
"CreationDateTime": "2022-02-07T11:12:44.278000+00:00",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:ap-south-1:123456789012:table/Employee",
"TableId": "42e16ff5-e39a-48c9-950c-a67c4d9eee46",
"BillingModeSummary": {
"BillingMode": "PAY_PER_REQUEST"
}
}
}
If you inspect the response closely, you will see that the table creation status is-
"TableStatus": "CREATING"
All I mean to say is, create-table is asynchronous and on receiving this request DynamoDB returns the response immediately with ‘Creating’ status. Once the table is created, DynamoDB marks the table as ‘Active‘.
Now, if you want to work with your table , you should know that table status must be ‘Active‘ before you can do any operation such as read or write on it.
Therefore, after firing command you can check the status of table by using describe-table command.
AWS DynamoDB describe-table example
You can use describe-table to check table status like below-
aws dynamodb describe-table --table-name Employee
As expected, this time I got table status as Active. So, ideally I am all set to read and write on my table Employee.
By the way, If you are still wondering, how I got to know what parameter to pass to describe-table ?
Well, it’s pretty simple. I did the same thing I was asking you to do. I ran help command on describe-table again to see what are the expected parameters and how to provide values for it.
aws dynamodb describe-table help
On hitting enter, you will see
describe-table
--table-name <value>
[--cli-input-json | --cli-input-yaml]
[--generate-cli-skeleton <value>]
3. Create Complex DynamoDB table using JSON
We saw how to create a DynamoDB table using CLI. Although, it’s great to create simple table however, when you have got a lot of things like GSI, LSI, Encryption, Stream specification etc. It starts becoming quite clumsy. Therefore, it’s a good practice to use a separate file to store table specification.
If you remember, when we did aws dynamodb create-table help, we saw a parameter [–generate-cli-skeleton <value>] and a parameter [–cli-input-json | –cli-input-yaml].
Together, they can help you generate the skeleton, then you can fill up details and then create a table.
aws dynamodb create-table –generate-cli-skeleton
aws dynamodb create-table --generate-cli-skeleton
- The value that you can provide for –generate-cli-skeleton parameter is input, yaml-input or no value.
- If you don’t provide anything or provide input, it’s gonna print a json input that you can pass to —cli-input-json to create a table.
- If you provide yaml-input value, it will print a yaml input that you can pass to –cli-input-yaml parameter to create a table
Let’s try that
aws dynamodb create-table --generate-cli-skeleton
Once I hit enter, it printed this beautiful json file-
{
"AttributeDefinitions": [
{
"AttributeName": "",
"AttributeType": "N"
}
],
"TableName": "",
"KeySchema": [
{
"AttributeName": "",
"KeyType": "RANGE"
}
],
"LocalSecondaryIndexes": [
{
"IndexName": "",
"KeySchema": [
{
"AttributeName": "",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL",
"NonKeyAttributes": [
""
]
}
}
],
"GlobalSecondaryIndexes": [
{
"IndexName": "",
"KeySchema": [
{
"AttributeName": "",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "KEYS_ONLY",
"NonKeyAttributes": [
""
]
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0
}
}
],
"BillingMode": "PAY_PER_REQUEST",
"ProvisionedThroughput": {
"ReadCapacityUnits": 0,
"WriteCapacityUnits": 0
},
"StreamSpecification": {
"StreamEnabled": true,
"StreamViewType": "KEYS_ONLY"
},
"SSESpecification": {
"Enabled": true,
"SSEType": "KMS",
"KMSMasterKeyId": ""
},
"Tags": [
{
"Key": "",
"Value": ""
}
],
"TableClass": "STANDARD_INFREQUENT_ACCESS"
}
You can edit this file to create table as per your required specification. We will create a table with below specification-
Table Details:
- Table Name: Employee1
- Partition Key: EmployeeId
- Range Key/Sort Key: Department
- Global Secondary Index: City
- Billing Mode: On Demand
I edited this file, filled up information and saved the file as employee.json
Now, before we fire the command to create the table, make sure that you are in the same directory where you have this json file. You can use ls command as well to see if the file is in your working directory as shown below-
ls
All looks good.
So, let’s fire create table command using –
aws dynamodb create-table --cli-input-json file://dynamodb.json
Please make sure to choose the unique table name as they must be unique within a region. If you choose the same name, you will get below error-
An error occurred (ResourceInUseException) when calling the CreateTable operation: Table already exists: Employee
Once, everything is find, hit the below command-
As soon as you hit the command , you get response like below
{
"TableDescription": {
"AttributeDefinitions": [
{
"AttributeName": "City",
"AttributeType": "S"
},
{
"AttributeName": "Department",
"AttributeType": "S"
},
{
"AttributeName": "EmployeeId",
"AttributeType": "S"
}
],
"TableName": "Employee1",
"KeySchema": [
{
"AttributeName": "EmployeeId",
"KeyType": "HASH"
},
{
"AttributeName": "Department",
"KeyType": "RANGE"
}
],
"TableStatus": "CREATING",
"CreationDateTime": "2022-02-07T12:03:25.631000+00:00",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:ap-south-1:123456789012:table/Employee1",
"TableId": "e4b0adfe-a369-491e-a3ee-2157d8be9e17",
"GlobalSecondaryIndexes": [
{
"IndexName": "City-index",
"KeySchema": [
{
"AttributeName": "City",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "CREATING",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"IndexSizeBytes": 0,
"ItemCount": 0,
"IndexArn": "arn:aws:dynamodb:ap-south-1:123456789012:table/Employee1/index/City-index"
}
]
}
}
As I said earlier, table is in ‘Creating‘ state and will be marked ‘Active‘ once table is successfully created. Once the status is ‘Active‘ you can use this table to perform operations such as read and writes.
4. Verify the Created Tables
Login to AWS Management Console and open DynamoDB .
You will see that both our table Employee and Employee1 are created. Employee has no index and it is On-Demand capacity mode. Employee1 has one index and it has Provisioned capacity mode. You can click on individual tables to see more details.
Tables are created as expected 🙂 Happy days 🙂
5. Clean Up
If you are creating these tables just for learning purpose, you can delete it using below command so that you don’t accidently end-up paying something.
delete-table
--table-name Employee
delete-table
--table-name Employee1
Conclusion
In this post, We learnt to create DynamoDB table using AWS CLI. We did –
- Configured AWS CLI
- Learnt to use help with dynamodb command
- Created a simple table with on demand capacity
- Created a bit complex table with GSI and Provisioned Capacity
- Learnt to use describe-table command
- Cleaned up resources
Enjoyed the content?
Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.
Well That was my take on How to Create DynamoDB Table using AWS CLI. If you liked reading my post, you can motivate me by-
- Adding a comment below on what you liked and what can be improved.
- Follow us on Facebook, Twitter, LinkedIn, Instagram
Also Read:
One thought on “How to Create DynamoDB Table using AWS CLI”