Skip to content

Allow overriding lifecycle.prevent_destroy with environment variable #30957

@0xch4z

Description

@0xch4z

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_destroy to 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-resource in module my-module
  • any resource in the my-other-module resource

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions