How To Create MariaDB RDS in AWS using Terraform

In this article, we’ll walk through the steps of setting up a MariaDB RDS in AWS using Terraform | Create MariaDB RDS in AWS using Terraform | Connect MariaDB RDS using the MySQL Workbench | We’ll also explore the process of creating an RDS instance using Terraform. So, let’s break it down and make it easy to understand!

What is Terraform?

terra image
  • Terraform is a tool that helps you manage your infrastructure by writing code. Instead of manually setting up servers and resources, you describe your infrastructure in a file using a language that Terraform understands example JSON.
  • With Terraform, you declare what you want your infrastructure to look like, specifying resources and their configurations. Terraform then figures out how to make your actual infrastructure match that description.
  • It’s not limited to a specific cloud provider. You can use Terraform to manage resources on various cloud platforms like AWS, Azure, Google Cloud, and others.
  • You can version your infrastructure code using version control systems like Git. This means you can track changes, collaborate with others, and roll back to previous configurations if needed.
  • Terraform keeps track of the current state of your infrastructure. This state is crucial for Terraform to understand what changes need to be made to your infrastructure.

What is Amazon Relational Database Service (Amazon RDS)?

rds image
  • Amazon RDS is a service provided by Amazon Web Services (AWS) that simplifies the process of setting up, operating, and scaling a relational database.
  • It supports various popular relational database engines, such as MySQL, PostgreSQL, Oracle, SQL Server, and MariaDB. You can choose the one that best fits your application’s requirements.
  • Amazon RDS automatically backs up your database, allowing you to restore to any point in time within a retention period. This helps in data recovery and ensures data durability.
  • You can easily scale your database resources up or down based on your application’s needs. This is done without causing downtime or affecting your database’s performance.

Amazon RDS for MariaDB

  • Amazon RDS for MariaDB is a fully managed service offered by Amazon Web Services (AWS) that takes care of the administrative tasks involved in setting up and running MariaDB databases.
  • Amazon RDS for MariaDB automatically takes regular backups of your database. This ensures that you can restore your data to a specific point in time in case of accidental deletions or data corruption.
  • Amazon RDS supports Multi-AZ deployments for MariaDB, which means your database is replicated to another Availability Zone for failover. This helps ensure high availability and fault tolerance.

Prerequisites

Before starting this tutorial, you will need:

  • You’ll need an Amazon Web Services (AWS) account. If you don’t have one, you can sign up for a free AWS account.
  • Basic familiarity with AWS services.
  • Visual Studio on your system.
  • Hashicorp Terraform Extension. You can download it from the official Terraform website.

How to Create MariaDB RDS in AWS using Terraform

Steps for implementation to this project

  • Let’s create the following organizational structure as shown below.
    • Create a Folder – terraform-mariadb
    • Create 4 Files in our terraform-mariadb – 1) variables.tf  2) terraform.tfvars 3) main.tf and 4) outputs.tf.
terraform mariadb files 1
  • variables.tf In Terraform, the variables.tf file is used to declare and define variables that can be referenced in your Terraform configuration files (typically files with a .tf extension). These variables act as parameters that allow you to customize your Terraform configuration and provide values dynamically. The variables.tf file is where you define the variables, their types, and, in some cases, their default values.
    • Variable Declaration: This is where you define your variables using the variable keyword, followed by the variable name. For example:
    • Using a variables.tf file: It helps in making your Terraform configuration more modular and adaptable. By centralizing variable definitions, you can easily manage and update configuration values without having to modify multiple places in your code. It also promotes reusability and clarity in your Terraform projects.
variable "example_variable" {
  type        = string
  description = "An example variable"
}
  • terraform.tfvars In Terraform, the terraform.tfvars file is used to store input variables and their values in a key-value pair format. This file allows you to set values for variables used in your Terraform configuration without hardcoding them directly in the Terraform files (e.g., .tf files). The name terraform.tfvars is the default filename, but you can also use other filenames and specify them with the -var-file option when running Terraform commands. This separation of variable values makes it easier to manage and share configurations with different values for different environments.
  • main.tf– In Terraform, main.tf is a default and conventional name for the main configuration file. This file is where you define the infrastructure resources and their configurations using HashiCorp Configuration Language (HCL). The main.tf file is essentially the entry point for Terraform to understand how to create, update, and manage your infrastructure. Key points about main.tf:
    • main.tf is a naming convention, not a strict requirement. You can name your main configuration file differently if you prefer, but main.tf is widely used and recognized.
    • Inside main.tf, you specify the resources you want to create (e.g., AWS instances, databases, networks) and their configurations using Terraform syntax. This includes resource types, attributes, and any other settings required.
    • main.tf can also include declarations for variables and outputs. Variables allow you to parameterize your configuration, making it more reusable, while outputs provide a way to expose certain values to be easily queried or used by other systems.
    • Here’s a basic example of what a simple main.tf file might look like:
# main.tf

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

resource "aws_db_instance" "myrds" {
  engine = "mariadb"
  instance_class = "db.t2.micro"
}
  • outputs.tf– In Terraform, the outputs.tf file is used to define output values that can be displayed after the execution of the Terraform configuration. This file allows you to expose specific information about the resources you’ve created or modified during the Terraform run. Outputs are useful for providing useful information or data to users or external systems.
    • In the outputs.tf file, you define one or more output blocks. Each block specifies a named output and the value you want to expose.
    • Using outputs.tf enhances the usability and interoperability of your Terraform configurations by making key information easily accessible. It’s a way to communicate important data between different parts of your infrastructure or share it with users and systems consuming the Terraform outputs.
    • In this example, an output named “example_output” is defined, and its value is set to the ID of an aws_db_instance created in the Terraform configuration.
output "example_output" {
  value = aws_db_instance.example.id
}

Create a variables.tf file in terraform-mariadb folder

  • Enter the below code
    • Create a variables.tf file.
#variables.tf
variable "access_key" {
    description = "Access key to AWS console"
}
variable "secret_key" {
    description = "Secret key to AWS console"
}
variable "region" {
    description = "AWS region"
}
variables

Create a terraform.tfvars file in terraform-mariadb folder

  • Create a terraform.tfvars file.
  • You are defining the dynamic values for the variables declared in variables.tf file.
#terraform.tfvars
region = "us-east-1"
access_key = "<YOUR AWS CONSOLE ACCESS ID>"
secret_key = "<YOUR AWS CONSOLE SECRET KEY>"
terraform.tfvar

Create a main.tf file in terraform-mariadb folder

  • Create a main.tf file.
#main.tf
#defining the provider as aws
provider "aws" {
    region     = "${var.region}"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"
}



#create a RDS Database Instance
resource "aws_db_instance" "myrds" {
  engine               = "mariadb"
  identifier           = "myrds"
  allocated_storage    =  20
  engine_version       = "10.6.14"
  instance_class       = "db.t2.micro"
  username             = "admin"
  password             = "admin123"
  skip_final_snapshot  = true
  publicly_accessible =  true
}
main.tf new

Create a output.tf file in terraform-mariadb folder

  • Create an outputs.tf file.
  • Outputs the security group id and RDS Database Instance endpoint to confirm that they are created.
#outputs.tf

output "db_instance_endpoint" {
  value       = aws_db_instance.myrds.endpoint
}
output.tf

Initialize and Apply Terraform Configuration

  • Open a terminal in the directory containing your main.tf file and run the following commands:
terraform init
init 1
init 2
  • By running terraform plan, you get a detailed list of these three categories— Additions, Modifications, and Deletions. It’s a way for you to review and confirm that Terraform understands your intentions correctly.
terraform plan
plan 1
plan 2
  • Once you’re satisfied with the plan, you can proceed to apply those changes using terraform apply.
  • This two-step process—terraform plan followed by terraform apply—helps prevent unintended or accidental changes to your infrastructure. It gives you a chance to review and approve the proposed changes before Terraform makes any modifications to your environment.
terraform apply -auto-approve
apply 1
apply 2
apply 3
  • Wait for 5-6 minutes till all the resources have been created.
  • Go to RDS Console, Click on the Databases on the left navigation panel, You can see RDS Database Instance created successfully.
  • Once confirmed, it will create the Mariadb RDS instance. You can monitor the progress in your AWS Management Console or by running
terraform show
show
show 2
  • To check the syntax and to validate the configuration files, ensuring they are adhere to the HashiCorp Configuration Language (HCL) and are structurally correct we use Terraform Validate command
terraform validate
validate

Connect MariaDB RDS using the MySQL Workbench

  • To connect to a database on a DB instance using Workbench monitor, find the endpoint (DNS name) and port number for your DB Instance.
  •  Go to databases and click on “myrds“.
  • Under the Connectivity & security section, copy and note the endpoint and port.
  • Endpoint: “myrds.ccftlak6ynkq.us-east-1.rds.amazonaws.com”
  • Port: 3306
  • Open Workbench. Click on the plus icon.
  • Connection Name: myhostconnection
  • Host Name: “myrds.ccftlak6ynkq.us-east-1.rds.amazonaws.com”
  • Port: 3306
  • Username: admin
  • Password: admin123. Click on ok.
mysql conn 1 1
  • Click on Test Connection to make sure that you are able to connect to the database properly.
mysql con 1
  • Click on ok and ok again to save the connection.
  • Click on it to open the database. Enter the database password if prompted.
  • After successfully connecting and opening the database, you can create tables and perform various queries over the connected database.
mysql conn 2 1

Delete AWS RDS

  • Run this below command to destroy your resources.
terraform destroy
des 1
des 2
des 3

Conclusion
In this article, we’ve demonstrated How to Create Mariadb RDS in AWS using Terraform and Tested the connection using workbench and destroyed it later not to incur charge

Reference:-

About Sohail Behlim

Hey, I am Sohail Behlim, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link