In our ongoing series of deploying WordPress on AWS using Terraform, the next critical component we’ll explore is Amazon RDS (Relational Database Service). RDS provides a managed database service, allowing you to set up, operate, and scale relational databases with ease. In this blog post, we’ll walk through the process of deploying a MySQL database using RDS and understand the Terraform code to automate this setup.

https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html

What is Amazon RDS?

Amazon RDS is a managed relational database service that supports multiple database engines, including MySQL, PostgreSQL, MariaDB, Oracle, and Microsoft SQL Server. It automates time-consuming administration tasks such as hardware provisioning, database setup, patching, and backups, allowing you to focus on your applications.

Key Features of Amazon RDS

  1. Managed Service:
    RDS is a fully managed service, meaning AWS handles the infrastructure and maintenance tasks, including software patching and backups. This allows you to focus on developing and running your applications.
  2. Scalability:
    RDS provides seamless scalability to handle increased workloads. You can easily scale compute and storage resources to accommodate your application’s growth without any manual intervention.
  3. Automated Backups and Snapshots:
    RDS automatically backs up your database and retains backups for a specified retention period. Additionally, you can create manual snapshots, providing you with point-in-time recovery options.
  4. High Availability:
    RDS offers high availability through Multi-AZ deployments. In a Multi-AZ configuration, your database is automatically replicated to a standby instance in a different Availability Zone, ensuring continuity in case of a failure.
  5. Security and Compliance:
    RDS provides security features such as encryption at rest and in transit, network isolation, and IAM database authentication. It also supports various compliance certifications to meet regulatory requirements.

Terraform Code for RDS MySQL

Now, let’s modify out terraform code in main.tf, add the snippet to create an RDS MYSQL instance:

# RDS MYSQL

# subnet group
resource "aws_db_subnet_group" "dev_db_subnet_group" {
  name        = "dev_db_subnet_group"
  description = "RDS subnet group"
  subnet_ids  = [aws_subnet.dev-private-data-subnet-AZ1.id, aws_subnet.dev-private-data-subnet-AZ2.id]

  tags = {
    Name = "dev_db_subnet_group"
    env  = "env"
  }
}

resource "aws_db_instance" "dev_database" {
  db_name                 = "<UPDATE_WITH_YOUR_DB_NAME>"
  engine                  = "mysql"
  storage_type            = "gp2"
  engine_version          = "5.7"
  instance_class          = "db.t3.micro"
  username                = "<UPDATE_WITH_YOUR_USERNAME>"
  password                = "<UPDATE_WITH_YOUR_PASSWORD>"
  parameter_group_name    = "default.mysql5.7"
  skip_final_snapshot     = true
  backup_retention_period = 7
  multi_az                = false
  availability_zone       = "eu-west-1a"

  #   Enable storage autoscaling
  allocated_storage     = 20
  max_allocated_storage = 1000

  vpc_security_group_ids = [aws_security_group.dev_database_security_group.id]
  db_subnet_group_name   = aws_db_subnet_group.dev_db_subnet_group.name


  tags = {
    Name = "dev_database"
    env  = "env"
  }
}

Breakdown of the Terraform Code:

  1. RDS Subnet Group: The aws_db_subnet_group resource creates a subnet group for the RDS instance. It specifies the subnets in which the RDS instance will be deployed.
  2. RDS MySQL Instance: The aws_db_instance resource defines the RDS MySQL instance configurations. Key parameters include the database name, engine version, instance class, credentials, storage details, and more.

Implementation Steps

Update Variables: Replace placeholders such as <UPDATE_WITH_YOUR_DB_NAME>, <UPDATE_WITH_YOUR_USERNAME>, and <UPDATE_WITH_YOUR_PASSWORD> with your actual values.

Apply changes

terraform apply

Check AWS Console.

claretcloud, RDS, AWS
Current Architecture

For the purpose of this tutorial we will be creating only one RDS instance however as the image suggest the best and recommend way is to create master and read replica in another AZ for availability.

Remember to destroy resources at the end of the tutorial..

terraform destroy

Categorized in: