DEV Community

Cover image for AWS RDS Blue/Green Upgrade while Managing Infrastructure in Terraform
Muhammad Salman Zahid
Muhammad Salman Zahid

Posted on

AWS RDS Blue/Green Upgrade while Managing Infrastructure in Terraform

AWS RDS Blue/Green Upgrade while Managing Infrastructure in Terraform
If you're managing your AWS RDS MySQL 5.7 database using Terraform and need to upgrade to MySQL 8.0, the process isn't as straightforward as a simple terraform apply. AWS does not support in-place upgrades between major versions in Terraform directly. Instead, you need to orchestrate a blue/green deployment to ensure zero downtime and data integrity - and yes, you can still keep everything under Terraform management.

In this article, I'll walk you through how to perform a blue/green upgrade from RDS MySQL 5.7 to 8.0 using the AWS Console for the upgrade process, while also ensuring your new RDS instance is properly represented and managed in Terraform.
Let's get started with the pre-requisites and step-by-step implementation.

1. Update parameter group

Before you can perform a blue/green deployment for a version upgrade, it's essential to ensure your MySQL 5.7 instance is using compatible binary logging parameters.
In your Terraform configuration, locate the custom parameter group associated with your RDS instance and update it like this:

resource "aws_db_parameter_group" "mysql_57_param_group" {
  name        = "rds-bg-demo-5-7-db-pg"
  family      = "mysql5.7"
  description = "Parameter group for MySQL 5.7 with binlog settings compatible with MySQL 8.0"

  parameter {
    name  = "binlog_format"
    value = "ROW"
  }
  parameter {
    name  = "binlog_row_image"
    value = "FULL"
  }
}
Enter fullscreen mode Exit fullscreen mode

2. Reboot Cluster to apply changes.

3. Provision Custom Parameter Group for Green Cluster

Next, define a new parameter group for the green environment:

resource "aws_db_parameter_group" "mysql_8_0_param_group" {
  name        = "rds-bg-demo-8-0-db-pg"
  family      = "mysql8.0"
  description = "Parameter group for MySQL 8.0"
}
Enter fullscreen mode Exit fullscreen mode

4. Create Blue/Green Deployment.

Now head to the AWS Console and navigate to your RDS cluster:

  • Click "Create Blue/Green Deployment"
  • Name your deployment appropriately.
  • Update Engine version to 8.0.x and review instance, storage, and network configurations.
  • Click Next, review your settings and pricing, then click Create.
  • AWS will now provision a new green environment running MySQL 8.0, cloned from your 5.7 setup.

  • Name your blue/green deployment:

  • Set Engine configurations as recommended below:

  • Update Instance configuration as required:

  • Similarly update the Storage as required for Green environment and click on Next:

  • Review your settings for Green Cluster:

  • After reviewing settings and cost click on Create:

  • It will go into provisioning state:

  • Once provisioning is complete, you'll see Green Cluster Upgrading:

5. Verify Green Environment

  • Once provisioning completes:
  • Open the new Green Cluster
  • Copy its hostname
  • Update the hostname in your application (temporarily) to verify functionality
  • Test the DB with your existing workloads and ensure compatibility

6. Switchover to Green

After validation:

  • Click "Switch over" in the AWS Console.
  • Review the switchover summary and timeout settings.
  • Click Switch over.

  • Review switchover summary, update Timeout setting if necessary & hit Switch over:

Post-switchover:

  • The Green environment adopts the original DB identifier.
  • The Blue environment is renamed with a -old1 suffix.
  • The hostname of the upgraded instance matches the original.
  • After successful verification, Switch over to the Green environment:

The DB Hostname of upgraded cluster will revert to the original hostname as it was before this Blue/Green deployemnt creation. If you updated hostname in your environment for testing before switchover, make sure to update/revert to current hostname of upgraded db identifier.

7. Cleanup Blue/Green Deployment

After a successful switchover:

  • Delete the Blue/Green deployment from the Console.

  • Manually delete the old cluster (if no longer needed).

8. Update Terraform

Now update your existing terraform only with engine_version = "8.0.42" or whatever your upgraded RDS Cluster has and parameter_group_name value to custom parameter group name as we created in Step 3. Terraform will look like:

resource "aws_db_instance" "dev" {
  identifier                  = "rds-bg-dev-db"
  allocated_storage           = 30
  engine                      = "mysql"
  engine_version              = "8.0.42"
  instance_class              = var.instance_class
  manage_master_user_password = true
  username                    = var.username
  vpc_security_group_ids      = [module.rds_sg.security_group_id]
  availability_zone           = var.availability_zone
  deletion_protection         = true
  parameter_group_name        = aws_db_parameter_group.mysql_8_0_param_group.name
  backup_retention_period     = 7
}
Enter fullscreen mode Exit fullscreen mode

9. Terraform apply

Now run terraform apply and it will only update the parameter group to your RDS Cluster.

Conclusion

By following this method, you can safely upgrade your RDS MySQL 5.7 instance to 8.0 using AWS's Blue/Green deployment - while continuing to manage your infrastructure declaratively using Terraform.
This approach provides the best of both worlds: cloud-native orchestration and infrastructure-as-code consistency.

Top comments (0)