Linux

Amazon Relational Database Service: How to install Amazon RDS using Terraform

feature-photo-terraform

Amazon Relational Database Service (RDS) is one of Amazon Web Services (AWS) database offerings, and it often necessitates a lot of scrolling about and clicking in the AWS Management Console. This procedure might take several minutes, and if you’re a DevOps or just need to automate it, Terraform and AWS RDS are a perfect match! This article will walk you through creating a Terraform configuration for an AWS RDS instance and deploying it to the AWS cloud. Other guides can be found here: Squid Proxy: How to setup and configure a Proxy Server, How to install, configure Prometheus for Monitoring on a Linux Server, How to install Gradle on Ubuntu, how to install and configure Microsoft Teams on Ubuntu, how to add an EBS volume to AWS EC2, and, how to Deploy MVC Application to AWS EC2 Using RDP.

Quick Overview of Databases, Relational Databases & Relational Database Systems

A relational database management system (RDBMS is software that stores, manages, queries, and retrieves data from a relational database. The RDBMS acts as a bridge between users and applications and the database, as well as providing administrative services for data storage, access, and performance management.

What is a database

A database is a collection of data that has been arranged. The information can be saved in a variety of formats or files:
1. Data that is organized into lists or parent/child routes in hierarchical or navigational databases
2. Data that is stored in relational databases in pre-defined tables that are linked by common data elements.

In the 1960s, when computers began employing disk and drum storage systems, the term database was coined. This storage medium allowed for random data access, which was not possible with previous media (tape). In the 1960s, as the number and capability of computers increased, so did the amount of data be managed.

Interest in standards rose, and the group in charge of standardizing the Common Business Oriented Language (COBOL) computer programming language proposed the Conference on Data Systems Language database strategy (CODASYL).

Data was maintained in a navigational architecture in this technique.

Hierarchical Databases

To create a vast network of data, hierarchical databases (also known as navigational databases) use linked lists called data sets. The forward and reverse pointers to other elements of the data set were usually present in these linked lists. One of the numerous ways could be used to locate the data:

  • The main key may be generated using a hashing method.
  • Getting through a relationship (a set of data).
  • Records are scanned in sequential order.
  • Two of the most well-known databases of this sort were:

Relational Databases

The following are the fundamental concepts of a relational database: ​

  • Tables are used to organize data.
  • A common data element can be used to link tables together.
  • Tables can be created, modified, and deleted as needed.
  • Database Management Systems maintain the information stored on storage media and provide an interface to retrieve it. Standardized Query Language is the most widely used database interface (SQL). ​

Database Management System (DBMS)

The programs (managers) that allow access to the data are known as database management systems (DBMS). A database management system (DBMS) provides an interface that allows you to manipulate the data you’ve saved. The database is usually referred to by the management system’s name. MariaDB, Oracle, MySQL, PostgreSQL, and MongoDB are some of the most prominent DBMSs. The most popular database management system is a hot topic of discussion; one popular list can be found in this DB-Engines Ranking.

Because there are so many DBMSs to pick from, the finest ones usually come after a discussion that ends with the phrase “it depends.” For the few highlighted, below are some debate points:

  • MariaDB
    RDBMS is open-source, freely available, and a fork of MySQL. 
  • Oracle
    RDBMS, long history, supported “Enterprise Editions”.
  • MySQL
    RDBMS started out as an independent open source, now owned by Oracle.
  • PostgreSQL
    RDBMS, open-source, long history.
  • MongoDB
    Open source, Document DBMS.

Structured Query Language (SQL)

SQL can be used for data insert, query, update, and delete. It became a standard of the American National Standards Institute (ANSI) in 1986. Here is an example of a SQL statement: Add a Table:

CREATE TABLE Articles (
    -> Articleno INT NOT NULL ,
    -> Title VARCHAR(100) NOT NULL,
    -> Description VARCHAR(200),
    -> PRIMARY KEY ( Articleno )
    -> );

Every table is broken up into smaller entities called fields. The fields in the Articles table consist of Article no, Title, and Description. A field is a column in a table that is designed to maintain specific information about every record in the table.

Example: Select information from a table:

SELECT * FROM Readers;

SELECT is the command, the wildcard character * would pick every column of the

Example: Insert data into a table:

INSERT INTO Readers (Readno,First,Last)
    -> Values (01,'Raphael,'Christian'),
    -> (02,'Imoh','Billy'),
    -> (03,'Isaac','Brown');

Continuing with RDS using Terraform

Amazon Relational Database Service is an Amazon Web Services distributed relational database service. It’s a cloud-based web service that makes it easy for anyone to create, operate, and scale a relational database for use in applications. And we will be creating this using terraform

Prerequisites

This will be a stage process guide. If you really want to follow along, make sure you have the following items ready:

  1. AWS (Amazon Web Services) account
  2. A code editor — While any text editor may be used to work with Terraform configuration files, for the exercise in the guide, we used the inbuilt Linux editor vim (it can be installed with the command apt install vim)
  3. Terraform
  4. AWS CLI
  5. Authenticate the AWS Account

Creating Terraform and the AWS RDS Database Configuration

Terraform is indeed an infrastructure-as-code tool that allows you to construct, alter, and version infrastructure securely and effectively. Terraform employs a variety of configuration files. Each file is authored in one of two languages: Hashicorp Control Language (HCL) or JSON. This lesson will make use of HCL.

This tutorial will utilize three distinct files to construct an AWS RDS instance with Terraform, which when combined will include all of the instructions Terraform requires.

  1. main.tf — The Terraform config file includes the Terraform resource declaration as well as variable placeholders for the RDS instance properties.
  2. vars.tf — This is a Terraform variables file that contains all of the variables that main.tf will use. It’s a good idea to split configuration objects into variables, especially if you’re working with bigger Terraform settings.
  3. provider.tf — This is the configuration file for the AWS provider. Terraform connects to an Amazon account through AWS providers to manage or deploy/update hundreds of AWS services.

Let’s just get started by building the Terraform configuration file for the AWS RDS database instance.

  1. Open a terminal/console on your computer with Terraform installed. If you want to know how to install terraform check out our previous article How to Install Terraform on Linux
  2. Create a directory called /terraform and then change (cd) to it. All the configuration files you’ll be dealing with will be in this folder.
  3. Create folder for main.tf, vars.tf, provider.tf using the touch command.
create-folders
folders

The next thing to do is to edit the folders we created earlier. the first one will be the main.tf that we created with touch main.tf. we can do this with the command below:

vim main.tf

Paste the code below in the blank vim file. The AWS db instance resource is used in the configuration file. This document covers all of the procedures for using AWS APIs to provision an RDS instance based on a few parameters.

You’ll also note that a lot of the values begin with the word var. These are placeholders for Terraform input variables, which you’ll create in the next step with actual values.

resource "aws_db_instance" "default" {
# Allocating the storage for database instance.
  allocated_storage    = 10
# Declaring the database engine and engine_version
  engine               = var.engine
  engine_version       = var.engine_version
# Declaring the instance class
  instance_class       = var.instance_class
  name                 = var.name
# User to connect the database instance 
  username             = var.username
# Password to connect the database instance 
  password             = var.password
  parameter_group_name = var.parameter_group_name
}

We need to edit the vars.tf file that we created earlier with touch vars.tf command. we can do this with the command below:

vim vars.tf

The variables file is required to declare all variables that will be used in the main configuration file. You should notice that each of the variables listed below is referenced in the main.tf configuration file.

variable "engine" {}
variable "engine_version" {}     
variable "instance_class" {}
variable "name" {}       
variable "username" {}  
variable "password" {}
variable "parameter_group_name" {}
~                                                                              
~                                        

Terraform will require us to input the values below and as such when requested we must enter them accordingly. Terraform will utilize the values in this variables file to replace the variable references in the configuration file.

This AWS RDS instance will be created by Terraform as follows: On a t3.micro engine, a MySQL 5.7 database is running.
– With a password of password and a user1 admin username

  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t3.micro"
  name                 = "mydb"
  username             = "user1"
  password             = "password"
  parameter_group_name = "default.mysql5.7"

Finally, let’s edit the provider.tf file that was created with the touch provider.tf command we can do so with:

vim prov

we can use the provider information below:

provider "aws" {
   region = "us-east-1"
 }

To ensure that that the next command will run let’s install tree with the command below

apt install tree

output

root@ubuntu:~/terraform# apt install tree
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 106 not upgraded.
Need to get 43.0 kB of archives.
After this operation, 115 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu focal/universe amd64 tree amd64 1.8.0-1 [43.0 kB]
Fetched 43.0 kB in 1s (40.4 kB/s)
Selecting previously unselected package tree.
(Reading database ... 189943 files and directories currently installed.)
Preparing to unpack .../tree_1.8.0-1_amd64.deb ...
Unpacking tree (1.8.0-1) ...
Setting up tree (1.8.0-1) ...
Processing triggers for man-db (2.9.1-1) ...
root@ubuntu:~/terraform# 

By using the tree command, make sure the folder has all of the essential files.

tree
tree

Creating the AWS RDS Instance with Terraform

It’s time to start Terraform and create your first AWS RDS database instance now that you have the Terraform configuration files ready to go! . Terraform normally uses a three-stage approach to provision a Terraform configuration: terraform init, terraform plan, and terraform apply. Let’s have a look at each stage now.

Step1: Terraform init

We must remain in the directory where all our files were created. in this case, its the ~/terraform directory. In the same directory, run the terraform init command. The terraform init command sets up the necessary plugins and providers to work with AWS resources that need to be provided.

terraform-init
terraform init

Step2: Terraform Plan

The Terraform plan command gives you an overview of which resources in your infrastructure will be provisioned. You’ll also get a list of all the AWS resources Terraform plans to build as shown below.

Step3: Terraform Apply

When you run terraform apply, Terraform will generate a configuration from the configuration (main.tf) and other files, and then send that configuration to AWS as instructions to build the database instance.

Confirming Terraform was used to create the AWS RDS

Terraform should have generated the database instance by now. Let’s double-check in the AWS Management Console by manually looking for the database instance. Make sure that you are in the right region.

  1. Log in to the AWS Management Console by using your preferred web browser.
  2. While still in the Console, use the top-right search box to look for RDS and then select the RDS menu item.
terraform-created-db

Conclusion

In this guide, you discovered how to use Terraform and AWS RDS to create your first database. Terraform is the most extensively used automation tool.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x