Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terraform: How to override AWS RDS final snapshot

Have the following DB resource in tf file:

resource "aws_db_instance" "app_db" {
  count = local.db_count

  allocated_storage     = 5
  max_allocated_storage = 10
  engine                = "postgres"
  instance_class        = "db.t3.micro"
  name                  = var.db_creds["db_name"]
  port                  = 5432
  username              = var.db_creds["username"]
  password              = var.db_creds["password"]

  db_subnet_group_name   = aws_db_subnet_group.database_sg.name
  vpc_security_group_ids = [aws_security_group.app.id]

  final_snapshot_identifier = "${var.environment_deployment_tag}-app-db-snaphot"
}

After destroying DB resource I get this error:

DBSnapshotAlreadyExists: Cannot create the snapshot because a snapshot with the identifier app-db-snaphot already exists

I understand that it is because snapshot with such identifier already exists but I'd like to ask if there is a way to override previous snapshot?

If no and all snapshots should have unique name, I guess format something like this should be fine: final_snapshot_identifier = "${var.environment_deployment_tag}-app-db-snaphot-${timestamp()}"

I wonder how to clean up previous snapshots so on RDS won't be a lot of them? And what is the best approach to manage final snapshot?

like image 880
fo1n Avatar asked Oct 16 '25 16:10

fo1n


1 Answers

If you do want to maintain a final database snapshot and use the final_snapshot_identifier = "${var.environment_deployment_tag}-app-db-snaphot-${timestamp()}" snippet, be sure to add this lifecycle block so that subsequent Terraform plans don't see a "change" every time due to the use of the timestamp() function:

lifecycle {
    ignore_changes = [
      final_snapshot_identifier,
    ]
}

You may also need to replace the : characters from the timestamp() function with - characters by using replace(timestamp(), ":", "-") like so:

final_snapshot_identifier = "${var.environment_deployment_tag}-app-db-snaphot-${replace(timestamp(), ":", "-")}"

like image 131
Adil B Avatar answered Oct 18 '25 23:10

Adil B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!