AWS CLI for Relational Database Service
Last Updated :
02 Jul, 2024
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,
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.
You can check the result in the AWS Management Console by using the command given above.
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.
You can check the result in the AWS Management Console by using the command given above.
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.
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.
You can check the result in the AWS Management Console by using the command given above.
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.
Picture 5.1 Picture 5.2: That shows when you delete 'mynewdbinstance' instance.
Picture 5.2You 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.
Picture 5.1 Picture 5.2: That shows when you delete 'mynewdbinstance' instance.
Picture 5.2Picture 5.3: That shows finally all the instances are deleted, Nothing any instance here.
Picture 5.3Conclusion
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.
Similar Reads
AWS Database Migration Service (DMS)
The AWS Database Migration Service (DMS) helps you quickly and safely transfer your databases. It also lets you build, analyze, transform, and relocate databases and analytics platforms all in one place, saving you time, resources, and money. In that process, application downtime, based on the sourc
9 min read
AWS CLI for Service Health Monitoring
AWS has numerous services, and for your application to run smoothly, it is necessary to keep a check on the status of the services. AWS CLI provides a unified set of commands that enable you to programmatically monitor the health and status of AWS services and resources. Monitoring service health th
6 min read
KMS Commands: AWS CLI for Key Management Service
AWS Key Management Service is a fully managed service that enables the user to create and control the encryption keys that encrypt their data. AWS KMS is very instrumental in securing sensitive information for data integrity in a secure cloud environment. AWS KMS integrates well with different AWS s
5 min read
AWS RDS - Launching RDS Database Instance for Free
In this article we will look into how you can launch an Amazon RDS database instance that's covered by the AWS free tier. To do so follow the below steps: Amazon RDS free tier is available to you only in the first 12 months of your AWS account creation. Each calendar month, the free tier allows you
3 min read
How to Open a Database in SQL Server?
Opening a database in SQL Server is a fundamental task for database administrators and developers. It involves establishing a connection to the server instance and selecting a database to work with. In this article, we will explore two methods to open a database in SQL Server such as using SQL Serve
3 min read
AWS CLI for Continuous Integration
Quick and efficient delivery of quality code is at the core of software development in the fast-paced arena. Practically, Continuous Integration (CI) has emerged as a lynchpin practice to this aim, where developers regularly integrate changes in the code into the shared repository. These integration
6 min read
AWS CLI for Conversational Interfaces
Chatbots and voice assistants have now made it easy for users to have conversations with applications, AWS has servicesâAmazon Lex and Amazon Polly, to name a fewâthat make the development of intelligent applications easier. But the AWS CLI makes it easy to manage these services programmatically. AW
7 min read
Python SQLite - Creating a New Database
In this article, we will discuss how to create a Database in SQLite using Python. Creating a Database You do not need any special permissions to create a database. The sqlite3 command used to create the database has the following basic syntax Syntax: $ sqlite3 <database_name_with_db_extension>
3 min read
AWS CLI for Serverless Function Management
Serverless computing is changing everything about how we build and deliver applications. No need for the onerous task of managing serversâdevelopers can focus all their attention entirely on writing code and implementing functionalities. Among today's top serverless platforms, AWS Lambda supports ru
6 min read
How to Use Cloud SQL to Create and Manage Relational Databases
Data is everywhere, and managing data is a challenge. Most web websites and applications, businesses, and organizations use databases to store their data. while everything is moving online in a rapid manner managing databases becomes a challenge for organizations. Google Cloud Platform solves all ma
8 min read