-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Description
Current Terraform Version
v1.1.8
Use-cases
It's reasonable to want to protect important resources by marking them with prevent_destroy = true. This is often a useful guard when using some automated deployment tool like Atlantis -- it gives you peace of mind that your very important resource won't get destroyed due to a bug.
The trouble is when you do want to destroy the resource and it's located in a nested module. There is a constraint for the lifecycle block, the value of prevent_destroy needs to be known at runtime. But how can you convey at runtime that you want to destroy a resource you don't have access to edit?
Attempted Solutions
- Assigning
prevent_destroyto a variable. This results in the following error:
│ Error: Unsuitable value type
│
│ on .terraform/modules/some_module/main.tf line 23, in resource "some_resource" "something":
│ 23: prevent_destroy = !local.disable_dns_protection
│
│ Unsuitable value: value must be known
╵
╷
│ Error: Variables not allowed
│
│ on .terraform/modules/some_module/main.tf line 23, in resource "some_resource" "something":
│ 23: prevent_destroy = !local.disable_dns_protection
│
│ Variables may not be used here.
- Adding a destroy-time provisioner which exits with a non-zero code if a variable is not set. This works but is obviously not ideal.
Proposal
I'm bad at naming, but perhaps we could have some environment variable, which when set, any specified resource paths can be destroyed regardless of their prevent_destroy value.
TF_ALLOW_DESTROY=module.my-module.provider.resource.my-special-resource,module.my-other-module.* terraform destroy
This would override the prevent_destroy for:
resource.my-special-resourcein modulemy-module- any resource in the
my-other-moduleresource