Create MySQL DB on AWS With Terraform

AWS Managed relational database service (RDS) offer multiple databases option, and one of these options is MySQL.

Today we will use Terraform to create a MySQL 8.0.25 database on AWS using a configuration code.

Terraform and AWS make provisioning very smooth, clean and cost-effective as resources are not left behind after decommissioning.

Configuration

In the following configuration, I will create a MySQL 8.0.25 database. We also use the Terraform random module to generate the admin password for the database.

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "3.47.0"
    }
  }
}

provider "aws" {
  region = "us-west-2"
}

provider "random" {
  # Configuration options
}

resource "random_password" "password" {
  length = 20
  special = true
  override_special = "_%@"
}

resource "aws_db_instance" "default" {
  allocated_storage    = 50
  engine               = "mysql"
  engine_version       = "8.0.25"
  instance_class       = "db.t2.micro"
  name                 = "db001"
  username             = "dbadmin1"
  password             = random_password.password.result
  skip_final_snapshot  = true
}

Leave a comment

Your email address will not be published. Required fields are marked *