How to Create a DynamoDB Table Using AWS CLI
Introduction
In this lesson, we will learn how to create a DynamoDB table using AWS CLI. DynamoDB is a NoSQL database service provided by Amazon Web Services (AWS). It is a fully managed, highly scalable, and cost-effective database service that can be used to store and retrieve data. With DynamoDB, you can create tables, add and delete items, and query data.
In this lesson, we will focus on how to create a DynamoDB table using AWS CLI. We will discuss the different commands and options available for creating a DynamoDB table. We will also discuss how to use the AWS CLI to create a DynamoDB table.
What is AWS CLI?
AWS CLI (Command Line Interface) is a command line tool that allows you to manage AWS services from the command line. It is a powerful tool that can be used to manage AWS services such as EC2, S3, DynamoDB, and more. With AWS CLI, you can create, delete, and query AWS services from the command line.
Prerequisites
Before we begin, there are a few prerequisites that you need to have in order to create a DynamoDB table using AWS CLI.
- You must have an AWS account.
- You must have the AWS CLI installed on your machine.
- You must have an IAM user with the necessary permissions to create a DynamoDB table.
Creating a DynamoDB Table Using AWS CLI
Now that we have the prerequisites out of the way, let’s get started with creating a DynamoDB table using AWS CLI.
Step 1: Create a Table
The first step is to create a table. To do this, we will use the aws dynamodb create-table
command. This command takes a few parameters, such as the table name, the primary key, and the read and write capacity units.
For example, to create a table called my_table
with a primary key of id
and a read and write capacity of 10, we can use the following command:
aws dynamodb create-table \
--table-name my_table \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=10
This command will create a table called my_table
with a primary key of id
and a read and write capacity of 10.
Step 2: Add Items
Once the table has been created, we can add items to it. To do this, we will use the aws dynamodb put-item
command. This command takes a few parameters, such as the table name, the item data, and the condition expression.
For example, to add an item to the my_table
table, we can use the following command:
aws dynamodb put-item \
--table-name my_table \
--item '{"id": {"S": "12345"}, "name": {"S": "John Doe"}}'
This command will add an item to the my_table
table with an id
of 12345
and a name
of John Doe
.
Step 3: Query the Table
Once the items have been added to the table, we can query the table to retrieve the data. To do this, we will use the aws dynamodb query
command. This command takes a few parameters, such as the table name, the key condition expression, and the filter expression.
For example, to query the my_table
table for items with an id
of 12345
, we can use the following command:
aws dynamodb query \
--table-name my_table \
--key-condition-expression "id = :id" \
--expression-attribute-values '{":id": {"S": "12345"}}'
This command will query the my_table
table for items with an id
of 12345
.
Conclusion
In this lesson, we learned how to create a DynamoDB table using AWS CLI. We discussed the different commands and options available for creating a DynamoDB table. We also discussed how to use the AWS CLI to create a DynamoDB table. Finally, we discussed how to add items to the table and how to query the table.
By following the steps outlined in this lesson, you should now have a better understanding of how to create a DynamoDB table using AWS CLI.