fix: Object Lock enabling on existing buckets - #403
Open
vladislavdedushkin wants to merge 1 commit into
Open
Conversation
bryantbiggs
reviewed
Jul 28, 2026
| } | ||
|
|
||
| resource "aws_s3_bucket_object_lock_configuration" "this" { | ||
| count = local.create_bucket && var.object_lock_enabled && try(var.object_lock_configuration.rule.default_retention, null) != null ? 1 : 0 |
Member
There was a problem hiding this comment.
this is an incorrect change
bryantbiggs
requested changes
Jul 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Allows Object Lock to be enabled on existing S3 buckets without recreating them.
Two changes in
main.tf:Decoupled
aws_s3_bucket_object_lock_configurationfromvar.object_lock_enabled. The resource is now created wheneverobject_lock_configuration.rule.default_retentionis set, regardless of theobject_lock_enabledflag:Added
depends_on = [aws_s3_bucket_versioning.this]toaws_s3_bucket_object_lock_configurationso versioning is configured before the Object Lock configuration is applied.var.object_lock_enabledstill feedsaws_s3_bucket.object_lock_enabledand remains the way to enable Object Lock at bucket creation time.Motivation and Context
object_lock_enabledon theaws_s3_bucketresource isForceNew— flipping it fromfalsetotrueon an existing bucket makes Terraform destroy and recreate the bucket, which is a non-starter for any bucket holding data.AWS itself does not require recreation: Object Lock can be turned on for an existing bucket via
PutObjectLockConfiguration, which is exactly whataws_s3_bucket_object_lock_configurationcalls (itsobject_lock_enabledargument defaults toEnabled). The module was gating that resource behindvar.object_lock_enabled, so the only path it offered was the bucket-recreating one — users who setobject_lock_configurationalone got it silently ignored.The
depends_onaddition covers the second half of the problem: AWS requires bucket versioning to be enabled before Object Lock can be configured. Without an explicit dependency there is no reference between the two resources, so Terraform is free to order them arbitrarily and the apply can fail withInvalidBucketStateon a first run that enables both at once.With both changes, enabling Object Lock on an existing bucket is:
— no
object_lock_enabled = true, no bucket replacement.Breaking Changes
No backwards-incompatible changes for existing working configurations.
How Has This Been Tested?
examples/*to demonstrate and validate my change(s)examples/*projectsValidated in a live AWS environment:
object_lock_configurationon a second apply — Object Lock was enabled in place, plan showed no bucket replacement, and the retention rule was verified withaws s3api get-object-lock-configuration.depends_onproducing the correct ordering.object_lock_enabled = trueplan clean (no diff) against previous state.pre-commit run -aon my pull request