terraformでAWS RDS(Aurora)の設定を変更してないのに must be replacedになってしまう件


何に困っていたの?

  • terraform applyする度にRDSがdestroy(replace)されてしまう

なぜそれで困るの?

  • databaseを飛ばすリスク大 (rds用のtfファイルだけ別ディレクトリに配置して回避してた)
  • terraform applyする度にでら長いこと待たされる(人生を削られる)

それ困ってるの君だけじゃないの?

tfファイル見せてみなよ

rds.tf
resource "aws_db_subnet_group" "sample" {
  name       = "sample"
  subnet_ids = [aws_subnet.private_0.id, aws_subnet.private_1.id]
}
resource "aws_db_parameter_group" "sample" {
  name   = "sample"
  family = "aurora-mysql5.7"
}

resource "aws_rds_cluster_parameter_group" "sample" {
  name        = "sample"
  family      = "aurora-mysql5.7"
  description = "Cluster parameter for sample"

  parameter {
    name  = "character_set_client"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_connection"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_database"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_filesystem"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_results"
    value = "utf8mb4"
  }

  parameter {
    name  = "character_set_server"
    value = "utf8mb4"
  }

  parameter {
    name  = "collation_connection"
    value = "utf8mb4_general_ci"
  }

  parameter {
    name  = "collation_server"
    value = "utf8mb4_general_ci"
  }

  parameter {
    name  = "time_zone"
    value = "Asia/Tokyo"
  }
}

resource "aws_rds_cluster" "sample-cluster" {
  cluster_identifier              = "sample-cluster"
  engine                          = "aurora-mysql"
  availability_zones              = ["ap-northeast-1a", "ap-northeast-1c"]
  database_name                   = "sample"
  master_username                 = "admin"
  master_password                 = "DeraDeraderaDeraDera"
  deletion_protection             = true
  apply_immediately               = false
  backup_retention_period         = 30
  preferred_backup_window         = "09:10-09:40" # UTC (JST-9)
  preferred_maintenance_window    = "wed:09:45-wed:10:45"
  port                            = 3306
  vpc_security_group_ids          = [module.mysql_sg.security_group_id]
  db_subnet_group_name            = aws_db_subnet_group.sample.name
  storage_encrypted               = true
  db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.sample.name
  skip_final_snapshot             = true

  lifecycle {
    ignore_changes = ["master_password"]
  }
}

resource "aws_rds_cluster_instance" "sample" {
  count                   = 2
  identifier              = "sample-${count.index}"
  engine                  = "aurora-mysql"
  engine_version          = "5.7.12"
  cluster_identifier      = aws_rds_cluster.sample-cluster.id
  instance_class          = "db.r3.xlarge"
  db_subnet_group_name    = aws_db_subnet_group.sample.name
  db_parameter_group_name = aws_db_parameter_group.sample.name
  publicly_accessible     = false
}

module "mysql_sg" {
  source      = "./security_group"
  name        = "mysql-sg"
  vpc_id      = aws_vpc.sample.id
  port        = 3306
  cidr_blocks = [aws_vpc.sample.cidr_block]
}

ふーん、で、どこ直したの?

  • ココに書いてある方法で直すことに成功したよ!

lifecycleのignore_changesのところに"availability_zones"を追加したら期待どおりの動きをするようになりました!同じ問題で困っている人の助けになれば幸いです

rds.tf
resource "aws_rds_cluster" "sample-cluster" {
  cluster_identifier              = "sample-cluster"
  engine                          = "aurora-mysql"
  availability_zones              = ["ap-northeast-1a", "ap-northeast-1c"]
  database_name                   = "sample"
  master_username                 = "admin"
  master_password                 = "DeraDeraderaDeraDera"
  deletion_protection             = true
  apply_immediately               = false
  backup_retention_period         = 30
  preferred_backup_window         = "09:10-09:40" # UTC (JST-9)
  preferred_maintenance_window    = "wed:09:45-wed:10:45"
  port                            = 3306
  vpc_security_group_ids          = [module.mysql_sg.security_group_id]
  db_subnet_group_name            = aws_db_subnet_group.sample.name
  storage_encrypted               = true
  db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.sample.name
  skip_final_snapshot             = true

  lifecycle {
    ignore_changes = ["master_password", "availability_zones"]
  }
}