{Terraform} クロスリージョンリードレプリカ

 

参考文献: Terraform Up & Running (Yevgeniy Brikman著)


mac(Monterey)ではTERRAFORM CRASH発生のため、ubuntu(22)で実施

 


-- 1. 作業ディレクトリ作成

cd ~
mkdir 20231119
cd 20231119

mkdir -p live/dev/db/mysql
mkdir -p modules/db/mysql


-- 2. モジュール作成

 


cat <<-'EOF' > ~/20231119/modules/db/mysql/variables.tf


variable "db_name" {
  description = "db_name"
  type = string
  default = null
}

 

variable "db_username" {
  description = "db_username"
  type = string
  sensitive = true
  default = null
}

variable "db_password" {
  description = "db_password"
  type = string
  sensitive = true
  default = null
}


variable "backup_retention_period" {
  description = "backup_retention_period"
  type = number
  default = null
}

variable "replicate_source_db" {
  description = "replicate_source_db"
  type = string
  default = null
}


EOF


cat <<-'EOF' > ~/20231119/modules/db/mysql/main.tf

terraform {
  required_version = ">= 1.0.0, < 2.0.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}


resource "aws_db_instance" "instance01" {
  identifier = "instance01"
  allocated_storage = 10
  instance_class = "db.t2.micro"
  skip_final_snapshot = true
  
  
  backup_retention_period = var.backup_retention_period
  replicate_source_db = var.replicate_source_db
  
  engine = var.replicate_source_db == null ? "mysql" : null
  db_name = var.replicate_source_db == null ? var.db_name : null
  username = var.replicate_source_db == null ? var.db_username : null
  password = var.replicate_source_db == null ? var.db_password : null
}

EOF


cat <<-'EOF' > ~/20231119/modules/db/mysql/outputs.tf

output "address" {
  value = aws_db_instance.instance01.address
  description = "address"
}

output "port" {
  value = aws_db_instance.instance01.port
  description = "port"
}

output "arn" {
  value = aws_db_instance.instance01.arn
  description = "arn"
}


EOF

 


-- 3. メイン処理作成


cat <<-'EOF' > ~/20231119/live/dev/db/mysql/variables.tf

variable "db_username" {
  description = "db_username"
  type = string
  sensitive = true
  default = "root"
}

variable "db_password" {
  description = "db_password"
  type = string
  sensitive = true
  default = "password"
}

EOF

 


cat <<-'EOF' > ~/20231119/live/dev/db/mysql/main.tf

terraform {
  required_version = ">= 1.0.0, < 2.0.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}


provider "aws" {
  region = "ap-northeast-1"
  alias = "primary"
}

provider "aws" {
  region = "ap-southeast-1"
  alias = "replica"
}

 

module "mysql_primary" {
  
  source = "../../../../modules/db/mysql"

  providers = {
    aws = aws.primary
  }
  
  db_name = "db01"
  db_username= var.db_username
  db_password= var.db_password

  backup_retention_period = 1

}

module "mysql_replica" {
  
  source = "../../../../modules/db/mysql"

  providers = {
    aws = aws.replica
  }

  replicate_source_db = module.mysql_primary.arn

}

EOF


cat <<-'EOF' > ~/20231119/live/dev/db/mysql/outputs.tf

output "primary_address" {
  value = module.mysql_primary.address
  description = "primary_address"
}

output "primary_port" {
  value = module.mysql_primary.port
  description = "primary_port"
}

output "primary_arn" {
  value = module.mysql_primary.arn
  description = "primary_arn"
}

output "replica_address" {
  value = module.mysql_replica.address
  description = "replica_address"
}

output "replica_port" {
  value = module.mysql_replica.port
  description = "replica_port"
}

output "replica_arn" {
  value = module.mysql_replica.arn
  description = "replica_arn"
}

EOF


-- 4. 実行

cd ~/20231119/live/dev/db/mysql

terraform init
terraform fmt
terraform -version

terraform apply -auto-approve

 

terraform destroy -auto-approve