State Based Deletion Protection
Community note
[!TIP] Hi there, OpenTofu community! The OpenTofu team prioritizes issues based on upvotes. Please make sure to upvote this issue and describe how it affects you in detail in the comments to show your support.
OpenTofu Version
1.12.6The problem in your OpenTofu project
I would like to be able to use first class OpenTofu functionality to allow me to mimic the behavior of the Google provider's deletion_protection/deletion_policy/etc on any resource that I specify, not just ones where the provider has specifically built this protection into that specific resource type. This means that deletion protection information is stored in the state file and any plans or applies that would cause the protected resource to be deleted fail with an error saying that I'm trying to delete a resource that has deletion protection enabled and deletion protection must first be disabled before I attempt to do the deletion.
This deletion protection field must be able to reference a dynamic value such as variables and locals, it is not viable to use the historical behavior of the prevent_destroy lifecycle rule where it could only be hardcoded true/false even if it lives on a resource inside a module.
Note to maintainers - feel free to edit this issue.
Attempted Solutions
See Workarounds and Alternatives section below
Proposal
Implement state based deletion protection, similar to how the Google provider does it, so that it will generate an error if deletion protection is enabled and the plan shows a deletion of the resource because one of the following conditions is met:
- A module call is removed
- A resource block is removed
- An instance of a module call is removed (
count/for_each/enabled) - An instance of a resource is removed (
count/for_each/enabled) - An instance of a resource will be deleted and re-created
This must be able to reference a dynamic value such as variables, it is not viable to use the historical behavior of the prevent_destroy lifecycle rule where it could only be hardcoded true/false even if it lives on a resource inside a module.
I can see a few different options for how someone would delete or re-create a resource that is currently protected by this deletion protection:
- Copy the current behavior of the Google provider which involves the following steps. This is the most toil (2-3 separate PRs/applies) but is the most explicit and also matches the workflow that users sometimes have to follow to disable cloud-side deletion protection.
- Do an apply that disabled deletion protection
- Make the change that results in deletion or re-creation and apply it
- (If necessary) Do an apply that re-enables deletion protection
- Allow users to change deletion protection in the same apply as the action that performs the destruction.
- If the resource instance is being re-created, this means that you would
- Change the deletion protection value and make the change that triggers the re-creation in the same PR/apply
- Do an apply that re-enables deletion protection
- If the module or resource instance is being removed, do something similar to the existing
removedblock that tells OpenTofu this module or resource instance has been deliberately removed and overrides the deletion protection that is saved in the state file.- Remove the module or resource instance and add the
deletedblock (or whatever you name it) in a single PR/apply - Clean up the
deletedblock, same as how you can remove aremoved,import, ormovedblock once they have been executed
- Remove the module or resource instance and add the
- If the resource instance is being re-created, this means that you would
- Have a CLI option similar to
taintthat removes the deletion protection for just the next apply (or potentially allow the existingtaintfunctionality to override the deletion protection). Given the general move away from CLI only operations, I don't love this option, but wanted to include for completeness. I don't think this would be good to do in a.tffile similar to how themovedblock works since a user could easily forget to remove the block after the apply has occurred and it would be better for the user to set and feed the value through as a variable to their resource or module call where they can do variable validation rules or additional checks on their end.
Workarounds and Alternatives
prevent_destroy, but this has significant limitations, as listed in https://github.com/opentofu/opentofu/issues/4569 and doesn't actually prevent destruction when the resource or module block is removed.- Using provider created resource specific functionality like the Google provider has. However, this has significant limitations in that it is rarely available, has to be added per specific individual resource, adds a lot of overhead for provider maintainers, and provider maintainers have to add this functionality per individual resource instead of users being able to determine which resources they do or do not want to use it with.
- Using OPA policies. However, this adds significant overhead and complexity to CI/CD tooling compared to a first class, native OpenTofu functionality. Additionally, these are often not evaluated as part of PR plans or local development plans, which creates a poor developer experience and longer cycle times.
References
- https://github.com/opentofu/opentofu/issues/4569
- https://opentofu.org/docs/language/resources/behavior/#lifecycle-customizations
- https://github.com/hashicorp/terraform/issues/17599
- https://github.com/hashicorp/terraform-provider-google/issues/20729
- https://github.com/hashicorp/terraform-provider-google/issues/25072
- https://github.com/hashicorp/terraform/issues/3116
Source: opentofu/opentofu