Coding_ninja
Coding_ninja

Reputation: 131

How can I remove old cluster after upgrading AWS RDS aurora from serverless v1 to serverless v2

I've upgraded AWS rds aurora postgress cluster from serverless v1 to serverless v2 using terraform.

The steps that I followed:

  1. Creating snapshot of the serverless v1 cluster
  2. Creating provisioned db cluster from that snapshot
  3. Creating serverless db instance

Below is my terraform config:

 resource "aws_rds_cluster" "aurora_rds_serverless_v2" {
   cluster_identifier = "aurora-v2-serverless-cluster-new-197789"
   engine = local.env_config.rds_engine
 }

resource "aws_db_cluster_snapshot" "aurora_rds_db_snapshot" {
  db_cluster_identifier = aws_rds_cluster.aurora.id
  db_cluster_snapshot_identifier = "aurora-rds-db-cluster-snapshot"
}

resource "aws_rds_cluster" "aurora_rds_provisioned_db_v2" {
  cluster_identifier = "aurora-cluster-new-provisioned-v29071"
  engine  = local.env_config.rds_engine
  engine_version  = local.env_config.rds_engine_version
  engine_mode = "provisioned"
  depends_on = [aws_db_cluster_snapshot.aurora_rds_db_snapshot]
}

resource "aws_rds_cluster_instance" "aurora_provisioned_instance" {
  cluster_identifier = aws_rds_cluster.aurora_rds_provisioned_db_v2.id
  instance_class = "db.serverless"
}

My problem is I cannot simply comment out the old cluster code because snapshot resource is dependent on it. Is there a terraform resource that I can use to delete the old cluster? Or do I need to make the snapshot resource independent after first TF apply?

Upvotes: 0

Views: 128

Answers (1)

Lorenzo Felletti
Lorenzo Felletti

Reputation: 553

Terraform version < 1.7.0

You could follow steps similar to this:

  1. use terraform state rm command to delete the snapshot from the terraform state
  2. remove the snapshot and the instance from the IaC code. Also remove the depends_on
  3. run a terraform plan/apply cycle.

Since you've removed the snapshot from the state, and from the code, before running plan/apply Terraform's will not detect that as a change, and will not attempt to delete the snapshot.

Terraform version >= 1.7.0

In the same commit, do:

  • Add a removed block for the snapshot resource
  • Delete both the snapshot and the v1 instance. Also remove the depends_on. Then, run a plan/apply cycle. Terraform will figure out by itself that it does not need to delete the snapshot because of the removed block.

Here's the updated version of your snippet that works as described:

resource "aws_rds_cluster" "aurora_rds_serverless_v2" {
   cluster_identifier = "aurora-v2-serverless-cluster-new-197789"
   engine = local.env_config.rds_engine
 }

resource "aws_db_cluster_snapshot" "aurora_rds_db_snapshot" {
  db_cluster_identifier = aws_rds_cluster.aurora.id
  db_cluster_snapshot_identifier = "aurora-rds-db-cluster-snapshot"
}

resource "aws_rds_cluster" "aurora_rds_provisioned_db_v2" {
  cluster_identifier = "aurora-cluster-new-provisioned-v29071"
  engine  = local.env_config.rds_engine
  engine_version  = local.env_config.rds_engine_version
  engine_mode = "provisioned"
  # removed the depends_on
}

resource "aws_rds_cluster_instance" "aurora_provisioned_instance" {
  cluster_identifier = aws_rds_cluster.aurora_rds_provisioned_db_v2.id
  instance_class = "db.serverless"
}

removed {
  from = aws_db_cluster_snapshot.aurora_rds_db_snapshot

  lifecycle {
    destroy = false # so that the snapshot is not removed from AWS
  }
}

References

Upvotes: 0

Related Questions