Horje
AWS CLI for Relational Database Service

As we know, Amazon Web Services (AWS) provides more than 200 IT and infrastructure management services. The question arises: can we only access and manage those services through the AWS Management Console? The answer is no. We can access and interact with those services through the AWS Management Console, which is a web-based interface, the AWS Command Line Interface (AWS CLI), which is used through a terminal; and the AWS SDK (AWS Software Development Kit), which is basically used in applications. Here we are deep diving into the AWS CLI and exploring how it interacts with the AWS RDS. Here, RDS supports various popular database engines such as MySQL, PostgreSQL, MariaDB, Oracle, and Microsoft SQL Server. A basic understanding of best practices that enhance your skill to use AWS CLI with RDS to automate database tasks, manage resources, and integrate RDS with other resources in an efficient manner.

Primary Terminologies

  • AWS CLI: The AWS Command Line Interface (CLI) is a powerful and open-source command-line Interface that is basically used to interact with various AWS services. through this tool you can download, configure and manage your AWS services and automate them though writing scripts.
  • RDS (Relational Database Service): Amazon Relational Database Service (RDS) is a fully managed AWS Relational Database Service that is easy to set up, operate, and automatically scale a relational database in the cloud. Here, you only focus on your application optimization; other parts will be handled automatically, such as installing and patching software and databases, high availability and scaling, server maintenance, database backups, etc.
  • DB Instance: A DB instance is an isolated database environment in the cloud. Each instance runs a single database engine and can use standard database client applications.
  • DB Snapshot: A DB snapshot is a backup of your DB instance that is stored in Amazon S3, and in another way, we can say that it is a static view of our SQL server database that is read-only.
  • DB Parameter Group: A DB parameter group acts as a container for engine configuration that controls how your database operates.
  • Endpoint: An endpoint is a URL that includes the network address (DNS address) and port number of your database instance.

Step-by-Step Guide for Relational Database Service using AWS CLI

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

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 following command.
aws configure

After that you enter some necessary information such as,

Configuring AWS CLI

IAM Permission

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

  • rds:CreateDBInstance
  • rds:ModifyDBInstance
  • rds:DeleteDBInstance
  • rds:CreateDBSnapshot
  • rds:RestoreDBInstanceFromDBSnapshot

Creating and attaching custom IAM Policy

aws iam create-policy \
--policy-name RDSManagementPolicy \
--policy-document '{
"Version": "2023-05-25",
"Statement": [
{
"Effect": "Allow",
"Action": [
"rds:CreateDBInstance",
"rds:ModifyDBInstance",
"rds:DeleteDBInstance",
"rds:CreateDBSnapshot",
"rds:RestoreDBInstanceFromDBSnapshot",
"rds:DescribeDBInstances",
"rds:RebootDBInstance"
],
"Resource": "*"
}
]
}'

Attach the policy to your IAM user or role:

aws iam attach-user-policy     
--policy-arn arn:aws:iam::aws:policy/RDSManagementPolicy
--user-name your-iam-username

Step 2: Creating a DB Instance

To create a new DB instance, use the following command:

aws rds create-db-instance     
--db-instance-identifier mydbinstance
--db-instance-class db.t2.micro
--engine mysql
--allocated-storage 20
--master-username admin
--master-user-password mypassword

Above command will create a MySQL DB instance named ‘mydbinstance‘ with the specified instance class, storage, and credentials. You can check the result in the AWS Command Line interface by using the command given above.

create db

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

db

Step 3: Modifying a DB Instance

To modify an existing DB instance, such as changing the allocated storage, use the following command:

aws rds modify-db-instance     
--db-instance-identifier mydbinstance
--allocated-storage 30
--apply-immediately

This command increases the storage size of ‘mydbinstance‘ to 30 GiB. You can check the result in the AWS Command Line interface by using the command given above.

modify

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

console

Step 4: Creating a DB Snapshot

To create a snapshot of your DB instance, use the following command:

aws rds create-db-snapshot 
--db-instance-identifier mydbinstance
--db-snapshot-identifier mydbsnapshot

This command creates a snapshot named ‘mydbsnapshot’.

Note: When you write this command, that time snapshot will be created automatically, but in the terminal, there is no output data until you restore a DB instance from a snapshot.

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

snapshot

Step 5: Restoring a DB Instance from a Snapshot

Here we are restoring DB instance from a snapshot, use the following command:

aws rds restore-db-instance-from-db-snapshot 
--db-instance-identifier mynewdbinstance
--db-snapshot-identifier mydbsnapshot

Above command through we can create a new DB instance ‘mynewdbinstance‘ from existing ‘mydbsnapshot‘ snapshot. You can check the result in the AWS Command Line interface by using the command given above.

new DB instance

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

AWS Management Console

Step 6: Finally Deleting a DB Instance

To delete a DB instance, use the following command:

aws rds delete-db-instance 
--db-instance-identifier mydbinstance
--skip-final-snapshot

This command deletes ‘mydbinstance’ without creating a final snapshot.

To delete a DB instance that was created through a snapshot, use the following command:

aws rds delete-db-instance     
--db-instance-identifier mynewdbinstance
--skip-final-snapshot

This command deletes ‘mynewdbinstance‘ without creating a final snapshot. You can check the result in the AWS Command Line interface by using the command given above.

Picture 5.1: That shows when you delete ‘mydbinstance’ instance.

delete

Picture 5.1

Picture 5.2: That shows when you delete ‘mynewdbinstance’ instance.

shows when you delete

Picture 5.2

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

Picture 5.1: That shows when you delete ‘mydbinstance‘ instance.

console

Picture 5.1

Picture 5.2: That shows when you delete ‘mynewdbinstance‘ instance.

deleting

Picture 5.2

Picture 5.3: That shows finally all the instances are deleted, Nothing any instance here.

deleted

Picture 5.3

Conclusion

The AWS CLI is a powerful tool for managing AWS services, including Amazon RDS, that enables high-performance deployment and scripting of database services. With the necessary IAM permissions, you can create, modify, copy, restore, and delete RDS instances with simple commands, saving time and reducing human error. This approach optimizes resource management, ensures scalability and cost effectiveness in your cloud infrastructure deployment.

AWS CLI for AWS RDS – FAQs

Why we are using AWS CLI ?

Think of that when we are manually performing all the tasks through the AWS Management Console. But the problem is that whenever we want to create or update any configuration at that time, it will take huge human effort and time. Instead of this, we use the AWS CLI, which solves this problem through scripting and automation.

Why we are using AWS RDS?

Imagine that if you have a database that is hosted on your AWS EC2 Instance, you should always provide the necessary resources manually, such as database backups, software installation and patching, database patching, and configuration settings for high availability and scalability. But if we use AWS RDS, then application optimization is your main responsibility; other things will be handled by AWS RDS itself.

How much charge AWS CLI takes?

The AWS CLI is free to use. You don’t need to pay to use it. But you should pay for whatever AWS services you are using through the AWS CLI as per the predefined rule of service charges.

How much charge AWS RDS takes?

AWS RDS charges depend on several factors, such as instance type, storage type, and data transfer.

  • Instance type: It will take charges per hour base for computing resources (CPU, memory) as per your selected instance type.
  • Storage type: It will incur charges per GB of storage on your database.
  • Data Transfer: Here, inbound data transfer is totally free, but you pay for outbound data transfer to the internet.

If we stop the AWS RDS service, will there be any charges?

When your AWS RDS service is in the stopped state, that means it saves costs related to computing power, but you still need to pay for storage resources.




Reffered: https://www.geeksforgeeks.org


Amazon Web Services

Related
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
How to Install Docker on Amazon Linux How to Install Docker on Amazon Linux

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