terraform에서 AWS RDS (Aurora) 설정을 변경하지 않았지만 must be replaced가되는 경우

무엇이 어려웠습니까?


  • terraform apply 할 때마다 RDS가 destroy(replace) 되어 버린다

  • 왜 그렇게 곤란한가?



  • 데이터베이스를 건너 뛰는 위험이 높습니다 (rds 용 tf 파일 만 다른 디렉토리에 배치하고 피했습니다).
  • terraform apply 할 때마다 주름 길게 기다린다 (인생을 깎는)

  • 그 곤란한 거야 너만이 아닌 거야?


  • No, 같은 고민을 가진 사람이 있었습니다.
  • Terraform wants to recreate cluster on every apply
  • RDS instance: Terraform insisting on destroy-recreate RDS instances although their IDs are unchanged
  • aws_rds_cluster gets destroyed / re-created even if no changes are made to Terraform code because of wrong AZs management

  • 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"]
      }
    }
    

    좋은 웹페이지 즐겨찾기