skip to Main Content

I am upgrading my RDS postgresql from 10.21 to 14.4 by creating a new family group as well , my idea is to create new family group and upgrade the rds with new family group and then delete the old family group (deleting the old family is not that important as I know that cost money ), I get an error , please check my code

My terraform version is 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"
}

module code , which has the old db parameter group creation

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
  }
}

error i face

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

i have already added create_before_destroy = true and tried adding the time_sleep as well but still i get the above error , the thing i see is , it first creates the new db parameter group and then start deleting the old db parameter group ,

my expectation is it first creates new db group then upgrade the db instance and then assing the new db group to the instance and then delete the old db group .

Am i missing something or overlooking ??

Thanks

I tried adding

lifecycle { create_before_destroy = true }
and also tried sleep time did not do the magic

2

Answers


  1. Chosen as BEST ANSWER

    fixed this , for any one in future

    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
      }
    }
    

    i created a parameter group and passed that as db_parameter_group_name to the module and in the module , the first parameter group is for family 10 and i dont want to delete it and the second db group for family 14, in this case i am checking for count if the value passed from root then dont create as the family is for psotgre14 is created from root itself and then in the resource for instance i pass the same , not sure if the solution is correct but this worked for me , thanks

    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
    

  2. I’ve gone through this recently and had to deal with the same error. The way I solved it was to create a second parameter group, so that you have one parameter group for the old RDS version, and one for the new version. Then apply the Terraform RDS upgrade. Then finally remove the old parameter group from Terraform and run apply again so that is is deleted in AWS.

    It’s unfortunate that Terraform doesn’t handle this more smoothly. It looks like there was a fix for this submitted a few months ago, but it hasn’t been merged yet.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search