postgresql Terraform RDS从10升级到14

zsohkypk  于 2023-05-28  发布在  PostgreSQL
关注(0)|答案(2)|浏览(182)

我升级我的RDS postgresql从10.21到14.4通过创建一个新的家庭组,以及,我的想法是创建新的家庭组和升级的RDS与新的家庭组,然后删除旧的家庭组(删除旧的家庭并不重要,因为我知道,成本钱),我得到一个错误,请检查我的代码
我的terraform版本是0.13.7

module "rds_pgsql" {
  source = "./modules/aws-rds-pgsql"

  stack                               = var.stack
  environment                         = var.environment
  app_name                            = var.app
  description                         = var.app
  db_username                         = data.aws_kms_secrets.rds.plaintext["master_username"]
  db_password                         = data.aws_kms_secrets.rds.plaintext["master_password"]
  db_name                             = var.db_name
  security_group_db_access            = flatten([module.db_subnets.cidr_blocks, module.private_app_subnets.cidr_blocks])
  db_param_group_family               = aws_db_parameter_group.rds_parameter_group.name
  db_monitoring_interval              = var.db_monitoring_interval
  db_engine_version                   = var.db_engine_version
  db_instance_type                    = var.db_instance_type
    db_apply_immediately                    = var.db_apply_immediately
  allow_major_version_upgrade         = var.db_allow_major_version_upgrade
  db_backup_plan                      = var.backup_plan_pgsql
    db_backup_retention_period          = 35
    db_backup_window                    = "00:00-02:00"
}

resource "aws_db_parameter_group" "rds_parameter_group" {
  name_prefix = "postgres14"
  description = var.stack
  family      = var.db_param_group_family

  parameter {
    apply_method = "pending-reboot"  
    name  = "client_encoding"
    value = "UTF8"
  }

  lifecycle {
    create_before_destroy = true
  }

  tags = {
    Name        = "${var.environment}-${var.stack}-rds-db-pg"
    Stack       = var.stack
    Environment = var.environment
    Description = var.stack
  }
}

resource "time_sleep" "wait_30_seconds" {
  depends_on = [module.rds_pgsql]

  create_duration = "90s"
}

模块代码,其中包含旧的db参数组创建

resource "aws_db_parameter_group" "db_parameter_group" {
  name        = "${var.stack}-${var.app_name}-rds-db-pg-${var.environment}"
  description = var.description
  family      = var.db_param_group_family

  parameter {
    name  = "client_encoding"
    value = "UTF8"
  }

  tags = {
    Name        = "${var.stack}-${var.app_name}-rds-db-pg-${var.environment}"
    Stack       = var.stack
    Environment = var.environment
    Description = var.description
  }
}

误差i面

Error: Error deleting DB parameter group: InvalidDBParameterGroupState: One or more database instances are still members of this parameter group events-consents-events-consents-rds-db-pg-gentst, so the group cannot be deleted
    status code: 400, request id: a9cb62d6-7607-4b8d-9d7d-ae9baf4e1f90

我已经添加了create_before_destroy = true,并尝试添加time_sleep,但仍然得到上面的错误,我看到的是,它首先创建新的db参数组,然后开始删除旧的db参数组,
我的期望是它首先创建新的数据库组,然后升级数据库示例,然后将新的数据库组分配到示例,然后删除旧的数据库组。
我是错过了什么还是忽略了什么??
谢谢
我试着加上
lifecycle { create_before_destroy = true }和也试过睡眠时间没有做魔术

ylamdve6

ylamdve61#

我最近经历了这个,不得不处理同样的错误。我解决这个问题的方法是创建第二个参数组,这样您就有一个参数组用于旧RDS版本,另一个参数组用于新版本。然后应用Terraform RDS升级。最后,从Terraform中删除旧的参数组,并再次运行apply,以便在AWS中删除它。
不幸的是,Terraform并没有更顺利地处理这一点。看起来几个月前提交了a fix for this,但尚未合并。

kkbh8khc

kkbh8khc2#

解决了这个问题,在未来的任何一个

module "rds_pgsql" {
  source = "./modules/aws-rds-pgsql"

  stack                               = var.stack
  environment                         = var.environment
  app_name                            = var.app
  description                         = var.app
  db_username                         = data.aws_kms_secrets.rds.plaintext["master_username"]
  db_password                         = data.aws_kms_secrets.rds.plaintext["master_password"]
  db_name                             = var.db_name
  security_group_db_access            = flatten([module.db_subnets.cidr_blocks, module.private_app_subnets.cidr_blocks])
  db_monitoring_interval              = var.db_monitoring_interval
  db_engine_version                   = var.db_engine_version
  db_parameter_group_name             = aws_db_parameter_group.rds_parameter_group.name
  db_instance_type                    = var.db_instance_type
    db_apply_immediately                    = var.db_apply_immediately
  allow_major_version_upgrade         = var.db_allow_major_version_upgrade
  db_backup_plan                      = var.backup_plan_pgsql
    db_backup_retention_period          = 35
    db_backup_window                    = "00:00-02:00"
  
}

resource "aws_db_parameter_group" "rds_parameter_group" {
  name = "events-consents-events-consents-postgres14"
  description = var.stack
  family      = var.db_param_group_family

  parameter {
    apply_method = "pending-reboot"  
    name  = "client_encoding"
    value = "UTF8"
  }

  lifecycle {
    create_before_destroy = true
  }

  tags = {
    Name        = "${var.environment}-${var.stack}-rds-db-pg"
    Stack       = var.stack
    Environment = var.environment
    Description = var.stack
  }
}

我创建了一个参数组,并将其作为db_parameter_group_name传递给模块,在模块中,第一个参数组用于family 10,我不想删除它,第二个db组用于family 14,在这种情况下,我正在检查count,如果从root传递的值,则不创建,因为psotgre14的family是从root本身创建的,然后在资源中创建,例如i通过相同的,不知道如果解决方案是正确的,但这对我来说工作,谢谢

resource "aws_db_parameter_group" "db_parameter_group" {
  
  name        = "${var.stack}-${var.app_name}-rds-db-pg-${var.environment}"
  description = var.description
  family      = var.db_param_group_family

  parameter {
    name  = "client_encoding"
    value = "UTF8"
  }

  tags = {
    Name        = "${var.stack}-${var.app_name}-rds-db-pg-${var.environment}"
    Stack       = var.stack
    Environment = var.environment
    Description = var.description
  }
}

resource "aws_db_parameter_group" "db_parameter_group_20230523" {
  count = var.db_parameter_group_name == "" ? 1 : 0
  name        = "${var.stack}-${var.app_name}-rds-db-pg-${var.environment}"
  description = var.description
  family      = var.db_param_group_family

  parameter {
    name  = "client_encoding"
    value = "UTF8"
  }

  tags = {
    Name        = "${var.stack}-${var.app_name}-rds-db-pg-${var.environment}"
    Stack       = var.stack
    Environment = var.environment
    Description = var.description
  }
}

resource "aws_db_instance" "db_master" {
 
  parameter_group_name            = var.db_parameter_group_name == "" ? aws_db_parameter_group.db_parameter_group_20230523[0].name : var.db_parameter_group_name

相关问题