Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: how to not delete resource during destroy?

What I need is terraform analog for CloudFormation's DeletionPolicy: Retain.

The resource should be left as is during terraform destroy, that's all.

prevent_destroy does not fit because the whole environment going to be deleted during terraform destroy

ignore_changes does not fit because there's no parameter's change.

How can I do it?

like image 551
Putnik Avatar asked Jul 04 '19 09:07

Putnik


People also ask

Does Terraform apply destroy resources?

The terraform destroy command terminates resources managed by your Terraform project. This command is the inverse of terraform apply in that it terminates all the resources specified in your Terraform state. It does not destroy resources running elsewhere that are not managed by the current Terraform project.

How do you skip destroy Terraform?

One option here is to tell Terraform to “forget about” this object, which will sever the binding between the object in the remote system and this block in your configuration. The object will continue to exist but Terraform will no longer have any record of it, and so terraform destroy will not propose to delete it.

Is the Terraform destroy command the only way to destroy resources?

Currently, there is no way to specify the resources NOT to be destroyed using Terraform.


1 Answers

You could break down the destroy to a set of tasks

  1. Use terraform state rm, to remove resources/modules you want to retain, from your state. Now they are no longer tracked by terraform.
  2. Remove these resources/modules, from your .tf files
  3. Run terraform plan. You should see that there are no changes to be applied. This is to ensure that the selected resources have been safely removed from your terraform state files and terraform code.
  4. Run terraform destroy. This should destroy all other resources.
like image 127
Shabin Muhammed Avatar answered Oct 27 '22 02:10

Shabin Muhammed