hashicorp / hashicorp/terraform
prevent_destroy does not prevent destruction when removing a resource from the configuration
- Dominant language
- Go
- Stars
- 49.7k
- Forks
- 10.6k
- Avg merge
- 21h 30m
- Merged PRs (30d)
- 100
Description
The documentation for prevent_destroy says
> This flag provides extra protection against the destruction of a given resource. When this is set to true, any plan that includes a destroy of this resource will return an error message
However, removing or commenting out a resource seems to still destroy it with no warning, even though technically it was a plan that includes the destruction of the resource. What I expected was that the state file would remember that it was created with prevent_destroy, and prevent its destruction even if it was no longer part of the parsed configuration. It would be reasonable to expect that the user would be forced to remove the prevent_destroy flag, terraform apply, then remove it from the configuration if they were absolutely sure they wanted to go ahead.
### Terraform Version
```
Terraform v0.11.3
+ provider.aws v1.11.0
```
### Terraform Configuration Files
```hcl
locals {
dynamo_db_mutext_attribute = "LockID" # The name must be exactly this
}
provider "aws" {
profile = "test"
region = "ap-southeast-2"
}
resource "aws_s3_bucket" "test_environment_terraform_backend_storage" {
region = "ap-southeast-2"
bucket_prefix = "SOME-BUCKET-NAME_PREFIX-"
versioning {
enabled = true
}
tags {
Environment = "test"
}
# We explicitly prevent destruction using terraform. Remove this only if you really know what you're doing.
lifecycle {
prevent_destroy = true
}
}
resource "aws_dynamodb_table" "test_environment_terraform_backend_mutex" {
"attribute" {
name = "${local.dynamo_db_mutext_attribute}"
type = "S"
}
hash_key = "${local.dynamo_db_mutext_attribute}"
name = "test_environment_terraform_backend_mutex"
read_capacity = 5
write_capacity = 5
# We explicitly prevent destruction using terraform. Remove this only if you really know what you're doing.
lifecycle {
prevent_destroy = true
}
}
```
### Expected Behavior
Resource is prevented from being destroyed because it was created with lifecycle { prevent_destroy = true }
### Actual Behavior
Resource with prevent_destroy set to true is destroyed when commenting out or removing from configuration.
### Steps to Reproduce
1. Enter the above Terraform configuration into a new module.
2. terraform apply and say yes.
3. Now, comment out the s3 bucket resource.
4. terraform apply again, and say yes
### Additional Context
### References
Contributor guide
Research direction
Reproduce the behavior using the Terraform configuration and steps provided, first applying the resources and then removing the resource blocks. Trace how lifecycle.prevent_destroy is handled when a resource disappears from configuration, and compare the result with the documented expectation. Done means the behavior is covered by a regression test and the intended protection or warning is implemented consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100