Deploy MySQL on AWS RDS Using Terraform: A Beginner’s Guide.

Deploy MySQL on AWS RDS Using Terraform: A Beginner's Guide.

Introduction.

In the ever-evolving world of cloud computing and DevOps, automation has become more than just a convenience—it’s a necessity. Infrastructure as Code (IaC) has revolutionized the way we manage and provision infrastructure, allowing teams to define, deploy, and manage resources using code rather than manual processes. One of the most widely adopted tools in the IaC space is Terraform, an open-source project by HashiCorp that allows you to define infrastructure in a declarative way. Using Terraform, you can automate the creation of a wide range of resources across multiple cloud providers, including AWS, Azure, and Google Cloud. In this tutorial, we’ll focus on using Terraform to create a MySQL database instance on Amazon RDS (Relational Database Service)—a fully managed, scalable, and highly available cloud database service provided by AWS.

Amazon RDS makes it incredibly easy to set up, operate, and scale a relational database in the cloud. It automates time-consuming tasks such as hardware provisioning, database setup, patching, and backups. MySQL, one of the most popular open-source relational database systems in the world, is a supported engine on RDS, making it a powerful combination for many applications. Whether you’re developing a web app, mobile backend, or a business platform, MySQL on RDS can provide the scalability and reliability you need. But instead of manually creating the database through the AWS Management Console, we’ll take a more efficient route by defining our entire infrastructure setup using Terraform.

By the end of this guide, you’ll have a fully functional MySQL RDS instance created via Terraform scripts. We’ll walk through setting up a provider, defining your infrastructure, creating networking components like VPC and subnets, applying security groups, and provisioning the actual database instance. Not only will this save you time and reduce errors, but it will also make your infrastructure reproducible and version-controllable—two key benefits of using IaC in modern DevOps workflows. You’ll also learn best practices around managing credentials, securing access, and destroying infrastructure safely when it’s no longer needed.

Whether you’re a developer, sysadmin, or cloud enthusiast looking to upskill, this guide will provide a hands-on introduction to using Terraform in real-world scenarios. No prior Terraform experience is required—just a basic understanding of AWS and cloud databases will do. So, if you’re ready to take your cloud infrastructure skills to the next level and say goodbye to click-ops, let’s dive into the world of Terraform and MySQL on AWS RDS.

Prerequisites

  • Terraform installed
  • AWS CLI configured with credentials
  • IAM permissions to create RDS, VPC, and related resources

Terraform File Structure

You can put everything in a single main.tf to get started.

Example main.tf

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

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "subnet1" {
  vpc_id            = aws_vpc.main.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-east-1a"
}

resource "aws_db_subnet_group" "rds_subnet_group" {
  name       = "rds-subnet-group"
  subnet_ids = [aws_subnet.subnet1.id]
}

resource "aws_security_group" "rds_sg" {
  name        = "rds_sg"
  description = "Allow MySQL access"
  vpc_id      = aws_vpc.main.id

  ingress {
    from_port   = 3306
    to_port     = 3306
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"] # Only for testing! Lock this down in prod!
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_db_instance" "mysql" {
  identifier         = "my-mysql-db"
  engine             = "mysql"
  instance_class     = "db.t3.micro"
  allocated_storage  = 20
  username           = "admin"
  password           = "admin1234"
  db_subnet_group_name = aws_db_subnet_group.rds_subnet_group.name
  vpc_security_group_ids = [aws_security_group.rds_sg.id]
  skip_final_snapshot = true
  publicly_accessible = true
}

To Deploy

  1. Initialize Terraform:
terraform init

Plan the deployment:

terraform plan

Apply the configuration:

terraform apply

Conclusion.

Automating the creation of a MySQL RDS instance with Terraform is a game-changer for anyone managing cloud infrastructure. Instead of manually clicking through the AWS console, you now have a repeatable, scalable, and version-controlled way to spin up your database environments. In this tutorial, we walked through the process of defining AWS infrastructure using Terraform—starting from the provider setup, creating networking and security resources, to finally provisioning a fully functional MySQL RDS instance.

By adopting Infrastructure as Code, you gain consistency across environments, reduce human error, and dramatically speed up your development and deployment processes. As your infrastructure needs grow, Terraform makes it easy to scale, update, and manage resources with minimal overhead. Plus, you can collaborate more effectively with your team by storing your infrastructure definitions in source control.

Now that you’ve mastered the basics, consider exploring more advanced topics like parameter groups, automated backups, multi-AZ deployments, secrets management, or even integrating Terraform with CI/CD pipelines. The possibilities are endless once your infrastructure is truly defined in code.

Thanks for following along! If this guide helped you, feel free to share it or leave a comment with your thoughts and questions. Happy Terraforming! 🚀

shamitha
shamitha
Leave Comment
Share This Blog
Recent Posts
Get The Latest Updates

Subscribe To Our Newsletter

No spam, notifications only about our New Course updates.