2

I'm importing the existing resources (AWS RDS) but the terraform plan command showed a summary:

#aws_db_instance.my_main_db must be replaced 
+/- resource "aws_db_instance" "my_main_db" {
~ address = x
allocated_storage = x
+ apply_immediately = x
~ arn = x
~ username = x
+ password = x
(others arguments with alot of +/- and ~)
}

my_main_db is online with persistent data. My question is as the title; Is it safe for the existing database to run terrafrom apply? I don't want to lose all my customer data.

3
  • 2
    You should edit your question to show the full plan output for the resource and also show your Terraform code as well. But no, it's unlikely to be safe as that will destroy and rebuild the database without any data in (unless it's being built from a snapshot).
    – ydaetskcoR
    May 29, 2020 at 13:34
  • I added a sample for plan output. But yeah, I think you're right; Terraform destroy and create new instance for replacement.
    – Davuz
    May 29, 2020 at 13:42
  • 1
    That doesn't show what we'd need to see. Chance are you can avoid the replacement but we'd need to see the full plan output for the resource and the Terraform code for it. Without that the question can't really be answered.
    – ydaetskcoR
    May 29, 2020 at 14:14

2 Answers 2

7

"Replace" in Terraform's terminology means to destroy the existing object and create a new one to replace it. The +/- symbol (as opposed to -/+) indicates that this particular resource will be replaced in the "create before destroy" mode, where there will briefly be two database instances existing during the operation. (This may or may not be possible in practice, depending on whether the instance name is changing as part of this operation.)

For aws_db_instance in particular, destroying an instance is equivalent to deleting the instance in the RDS console: unless you have a backup of the contents of the database, it will be lost. Even if you do have a backup, you'll need to restore it via the RDS console or API rather than with Terraform because Terraform doesn't know about the backup/restore mechanism and so its idea of "create" is to produce an entirely new, empty database.

To sum up: applying a plan like this directly is certainly not generally "safe", because Terraform is planning to destroy your database and all of the contents along with it.

If you need to make changes to your database that cannot be performed without creating an entirely new RDS instance, you'll usually need to make those changes outside of Terraform using RDS-specific tools so that you can implement some process for transferring data between the old and new instances, whether that be backup and then restore (which will require a temporary outage) or temporarily running both instances and setting up replication from old to new until you are ready to shut off the old one. The details of such a migration are outside of Terraform's scope, because they are specific to whatever database engine you are using.

0
3

It's most likely not safe, but really only someone familiar with the application can make that decision. Look at the properties and what is going to change or be recreated. Unless you are comfortable with all of those properties changing, then it's not safe.

1
  • Thank you for your quick response. You deserve a big thank with accepted answer!
    – Davuz
    Jun 2, 2020 at 0:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.