7
votes

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?

1

1 Answers

9
votes

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.