Horje
DynamoDB Dynamo: AWS CLI Commands for NoSQL Database

As we know, there are three main ways to access and manage AWS services, AWS CLI (command line interface), the AWS Management Console, and the AWS SDK(Software (software kit). Here we are working with the first tool, which is AWS CLI (command line interface) which is basically used for interacting with AWS Resources through scripting and automation functionality of the AWS CLI. In this article, we are deep-diving into working with DynamoDB through AWS CLI and we know that AWS DynamoDB is a managed NoSQL database service that is serverless, AWS and we use it when we have large amounts of data and need to read/write frequently.

Key Points 

Before we start to explore our main topic, we just need to understand some of the following points: 

  • Table: It is a collection of data in DynamoDB, and it’s similar to a relational database table.
  • Item: It is a single record in the table or collection of attributes, and it’s similar to raw in the relational database. 
  • Attribute: It basically stores the data into items like key-value pairs. and it’s similar to a column in the relational database. 
  • Primary Key: It is a unique identifier for each item that is present in a table, It can be
    • partition key: For a single attribute.
    • partition key and sort key: For a combination of two attributes.
  • Provisioned Throughput: It is basically capacity that is assigned to the DynamoDB table, and it is defined in terms of units.
  • Global Secondary Index (GSI): It is an index with partition and sort keys, and literally, it is different from those on the base table.
  • Local Secondary Index (LSI): It is also an index with the same partition key as those on the base table, but the sort key is different.

Step-by-step guidance to use AWS CLI for DynamoDB

Before starting, Make sure that you have an AWS Account with the needed permission to manage DynamoDB resources and AWS CLI installed and configured on your system.

Note: When you work with the Windows command prompt, some syntax issues come up. We should write instead of and \” instead of . For example, “{\”: year\”:{“N”:”2023″}}”.

Step 1: Configuring AWS CLI

  • Install AWS CLI: Follow the instructions from the AWS CLI documentation for installing AWS CLI as per your operating system.
  • Configure AWS CLI: After Installing AWS CLI you configure your AWS CLI through the following command.
aws configure

After that you enter some necessary information such as,

Configuring AWS CLI

IAM Permissions

If you want to manage the DynamoDB resources using AWS CLI, first check that the IAM user or role has the following permissions:

  • dynamodb:CreateTable
  • dynamodb:PutItem
  • dynamodb:GetItem
  • dynamodb:Query
  • dynamodb:UpdateItem
  • dynamodb:DeleteItem
  • dynamodb:DeleteTable

Creating and attaching custom IAM Policy

aws iam create-policy \
--policy-name DynamoDBManagementPolicy \
--policy-document '{
"Version": "2024-05-25",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:CreateTable",
"dynamodb:PutItem",
"dynamodb:GetItem",
"dynamodb:Query",
"dynamodb:UpdateItem",
"dynamodb:DeleteItem",
"dynamodb:DeleteTable"
],
"Resource": "*"
}
]
}'

Attach the policy to your IAM user or role

aws iam attach-user-policy 
--policy-arn arn:aws:iam::YOUR_ACCOUNT_ID:policy/DynamoDBManagementPolicy
--user-name YOUR_IAM_USERNAME

Step 2: Creating a DynamoDB Table

Here, we use the create-table command for DynamoDB table creation. Here, I create a movie table with a primary composed of year and title in the below example.

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

You can check the result in the AWS Command Line interface by using the command given above.

Picture 2.1: You can check the result in the AWS Command Line interface by using the command given above.

Creating a DynamoDB Table

Picture 2.1

Picture 2.2: You can check the result in the AWS Management Console by using the command given above.

Creating a DynamoDB Table

Picture 2.2

Step 3: Inserting Data into the Table

Here, we use the put-item command to put an item into the table. In the below example, it shows that a movie record is being inserted.

aws dynamodb put-item     
--table-name Movies
--item
'{"year": {"N": "2023"}, "title": {"S": "Example Movie"}, "info": {"S": "Sample data"}}'

Picture 3.1: You can check the result in the AWS Command Line interface by using the command given above.

Inserting Data into the Table

Picture 3.1

Picture 3.2: You can check the result in the AWS Management Console by using the command given above.

Inserting Data into the Table

Picture 3.2

Step 4: Querying Data from the Table

Here, we use the query command to query data in the table. In the below example, it basically shows that it retrieves all movies that were released in 2023.

aws dynamodb query     
--table-name Movies
--key-condition-expression "year= :year"
--expression-attribute-values '{":year":{"N":"2023"}}'

Picture 4.1: You can check the result in the AWS Command Line interface by using the command given above.

Note: When you work with the Windows command prompt, some syntax issues come up. In the above command ‘year‘, you need to change ‘#yr’ because year is a reserved keyword in DynamoDB. and We Should write ” ” Instead of ‘ ‘ and \” Instead of . For example, “{\”:year\”:{“N”:”2023″}}”.

Querying Data from the Table

Picture 4.1

Picture 4.2: You can check the result in the AWS Management Console by using the command given above.

Querying Data from the Table

Picture 4.2

Step 5: Updating Data

Here, we use the update-item command to update existing item into the table. In the below example, it shows that updates the info attribute for a specific movie.

aws dynamodb update-item    
--table-name Movies
--key '{"year": {"N": "2023"}, "title": {"S": "Example Movie"}}'
--update-expression "set info = :info"
--expression-attribute-values '{":info":{"S":"Updated info"}}'

Picture 5.1: You can check the result in the AWS Command Line interface by using the command given above.

Updating Data

Picture 5.1

Picture 5.2: You can check the result in the AWS Management Console by using the command given above.

Updating Data

Picture 5.2

Step 6: Deleting Data

Here, we use the delete-item command to delete an item from the table. In the below example, it shows that it deletes a specific movie record.

aws dynamodb delete-item     
--table-name Movies
--key '{"year": {"N": "2023"}, "title": {"S": "Example Movie"}}'

Picture 6.1: You can check the result in the AWS Command Line interface by using the command given above.

Deleting Data

Picture 6.1

Picture 6.2: You can check the result in the AWS Management Console by using the command given above.

Deleting Data

Picture 6.2

Step 7: Deleting a Table

Here, we use the delete-table command, which deletes a table.

aws dynamodb delete-table     
--table-name Movies

Picture 7.1: You can check the result in the AWS Command Line interface by using the command given above.

Deleting a Table

Picture 7.1

Picture 7.2: You can check the result in the AWS Management Console by using the command given above.

Deleting a Table

Picture 7.2

Conclusion

Using the AWS CLI to manage DynamoDB objects allows for efficient automation and scripting of database operations. This guide covered important operations such as creating tables, inserting data, querying, updating and deleting data. AWS CLI increases performance and stability, especially for highly managed data with frequent read/write operations. The benefits of these commands simplify operations and help maintain robust, scalable, and reliable applications in a server-less environment.

AWS CLI and AWS DynamoDB – FAQs

Why we are using AWS DynamoDB?

AWS DynamoDB is a no-SQL and serverless database service that is scaling horizontally, which means automatically adding more capacity to handle request traffic so you don’t need to manually provide the new resource or manage configuration. It provides single-digit millisecond latency for massive reads and writes.

How much charge AWS DynamoDB takes?

AWS DynamoDB pricing depends on several factors, such as data transfer, consumed storage capacity, and the amount of read/write provisioned. You should use the AWS DynamoDB Pricing Calculator for the most accurate estimate for your specific needs. 

When we use the AWS Management console instead of the AWS CLI?

AWS Management Console is basically used by beginners for different service explorations and quick action.

AWS CLI basically automates the task through a few lines of scripting. For example, handling large deployments.

In short, we use the AWS Management Console for ease of use and the AWS CLI for automation power.

In the first step of configuration, is the output format mandatory?

It is optional, if you are not select any format then ‘json‘ format will selected by default. It supports various types of format such as, json, text, table, and yaml.




Reffered: https://www.geeksforgeeks.org


Amazon Web Services

Related
AWS CLI for Relational Database Service AWS CLI for Relational Database Service
Using AWS-CDK (Java) to automate creation of IAM User Using AWS-CDK (Java) to automate creation of IAM User
How to Duplicate an AWS Lambda Function How to Duplicate an AWS Lambda Function
Building a Data Lake on AWS with Terraform Building a Data Lake on AWS with Terraform
How to Install AWS Lambda in Visual Studio Code How to Install AWS Lambda in Visual Studio Code

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19