S3 bucket
Some things people ask the terraform-aws-modules/s3-bucket/aws module for
cannot be implemented by any module, in any registry: they are limits of
Terraform and OpenTofu themselves. This page is for engineers who hit one of
them. For each it says what causes it, gives the native workaround in full, and,
where one exists, shows the Operational Rule that removes the need for a fork.
Read the native option first; several are the right answer on their own.
terraform destroy deleted a production S3 bucket
A terraform destroy in the wrong workspace, or a rename that Terraform reads as a replacement, deletes the bucket this module manages. prevent_destroy is the built-in guard against exactly that, and there is no module input for it.
Why Terraform cannot fix this. lifecycle is a meta-argument block, and Terraform evaluates it before it evaluates the rest of the configuration. Its arguments therefore cannot reference a variable, a local, or anything else that is computed. That is the whole reason no module can expose prevent_destroy, ignore_changes, or create_before_destroy as an input: there is no expression a module author could put there that Terraform would accept.
The request was first filed in 2015 (#3116, closed in 2023 without a change). The module-specific asks, #18367 and #22544, have been open since 2018 and 2019, and OpenTofu carries the same request. Until one of them ships a way to set a meta-argument from outside the resource block, every module in every registry has the same limitation.
| Repository | Issue | Title |
|---|---|---|
| hashicorp/terraform | #22544 | "Variables may not be used here" for prevent_destroy |
| hashicorp/terraform | #18367 | Feature request: support prevent_destroy for modules |
| hashicorp/terraform | #24746 | Ability to use locals or wildcards in ignore_changes lifecycle block |
| hashicorp/terraform | #24188 | Support for dynamic blocks and meta-arguments |
| hashicorp/terraform | #27360 | A method to override configuration and meta arguments within a module |
| opentofu/opentofu | #1329 | Support variables in lifecycle blocks |
The native workaround. Fork this module and add the lifecycle block to the resource yourself. That works, and module maintainers do it when they need it. The cost is ongoing rather than one-off: the fork has to be re-synced with every upstream release, and each sync needs a review to confirm the block still lands on the resource it was meant for. A wrapper module does not avoid this, because the lifecycle block still has to sit inside the resource block, which is inside the module you did not write.
Two partial native options are worth knowing. An S3 bucket policy with an explicit Deny on s3:DeleteBucket stops the delete at the API, though it also has to be lifted by hand when you do want the bucket gone. And a review checklist on the plan output catches it when someone reads the plan, which is the failure mode the guard exists for.
The rule that answers it. lifecycle_prevent_destroy_data adds the
block for you, server-side, while the module is being downloaded. What arrives
in .terraform/modules/ is ordinary HCL with this change already in it, and
nothing else about the module changes: same inputs, same outputs, same version.
resource "aws_s3_bucket" "this" {
# ... the module's own configuration, unchanged ...
+
+ lifecycle {
+ prevent_destroy = true
+ }
}Terraform keeps reverting tags on my S3 buckets
Cost-allocation tooling, a CSPM scanner, or an AWS Config remediation rule writes its own tag onto the bucket. Terraform sees the extra tag as drift and removes it on the next apply, and the external tool writes it back, indefinitely.
Why Terraform cannot fix this. Terraform's model is that it owns every attribute it can see. When a controller, an autoscaler, a scanner, or a deployment pipeline writes to one of those attributes, Terraform reads the new value as drift and plans to put its own value back. Both systems are behaving correctly; they simply disagree about who owns the field.
The only mechanism Terraform offers for splitting ownership is ignore_changes, and that is a lifecycle argument, which brings the literal-value restriction above with it. A module cannot accept the list of attributes to ignore as an input, so a module consumer has no way to declare the split.
| Repository | Issue | Title |
|---|---|---|
| hashicorp/terraform | #27360 | A method to override configuration and meta arguments within a module |
| hashicorp/terraform | #24188 | Support for dynamic blocks and meta-arguments |
| hashicorp/terraform-provider-aws | #19583 | Provider produced inconsistent final plan / an invalid new value for .tags_all |
The native workaround. Take the tag back into Terraform first, if you can. default_tags on the AWS provider applies a tag set to every resource the provider manages, and it removes the disagreement outright when the external writer can be turned off.
When the external writer cannot be turned off - a Kubernetes or load balancer controller writing its own tags is the usual case - the only native option left is ignore_changes = [tags, tags_all] inside the resource block, and that means forking the module. Fork this module and add the lifecycle block to the resource yourself. That works, and module maintainers do it when they need it. The cost is ongoing rather than one-off: the fork has to be re-synced with every upstream release, and each sync needs a review to confirm the block still lands on the resource it was meant for. A wrapper module does not avoid this, because the lifecycle block still has to sit inside the resource block, which is inside the module you did not write.
The rule that answers it. lifecycle_ignore_tags adds the
block for you, server-side, while the module is being downloaded. What arrives
in .terraform/modules/ is ordinary HCL with this change already in it, and
nothing else about the module changes: same inputs, same outputs, same version.
resource "aws_s3_bucket" "this" {
# ... the module's own configuration, unchanged ...
+
+ lifecycle {
+ ignore_changes = [tags, tags_all]
+ }
}Use the rule
This source line applies the rules above during terraform init. The inputs
and outputs stay the upstream module's.
module "s3_bucket" {
source = "https://registry.compliance.tf/terraform-aws-modules/s3-bucket/aws?version=x.y.z&add_rules=lifecycle_ignore_tags,lifecycle_prevent_destroy_data"
# ... the same inputs you pass today; the version is in the URL above ...
}- Pin the version in the URL.
?version=takes an exact release; without it the registry serves the latest. Delete theversionargument, which Terraform accepts only on registry-protocol sources. Several rules go in?add_rules=, separated by commas. - Keep the token out of the source line. Register a free compliance.tf
account and configure an access token; it never
goes in the
sourceline or in git. - Preview in the Playground, within its limits. The
Rules Playground shows the diff with no account, at
the version it pins for S3 bucket, which need not be yours. It reads the
module root only: a rule whose resource lives in a submodule shows no diff
there even though the download applies it, and a module that publishes
nothing but submodules cannot be previewed. It takes the list as
?rules=because it previews rules on their own rather than against your Baseline.
On registry.compliance.tf, or on a framework host, ?add_rules= adds to
whatever Baseline your organization has Enforced; on an organization host that
organization's configuration decides. A module compliance.tf builds for you
carries a compliancetf-manifest.json naming every rule the build received,
each with an outcome recording what it did.
Related
- Rules overview - what rules are, and how they differ from compliance controls
- Rule definitions - the HCL of every selectable rule
- lifecycle_ignore_tags - the rule page, with limits and failure modes
- lifecycle_prevent_destroy_data - the rule page, with limits and failure modes
- Stop paying the Terraform fork tax - why forking a module to add a
lifecycleblock costs more than it looks - S3 bucket module reference - inputs, outputs, and the controls applied to it