Amazon S3 buckets should have versioning enabled to protect against accidental or malicious deletion and overwriting of objects. Without versioning (versioning { enabled = false }), deleted or overwritten files cannot be recovered, potentially leading to permanent data loss or loss of critical information. Enabling versioning (versioning { enabled = true }) allows you to preserve, retrieve, and restore every version of every object stored in the bucket, significantly improving data resiliency.
resource"aws_s3_bucket""positive10"{bucket="my-tf-test-bucket-unversioned"}resource"aws_iam_role""unrelated"{name="unrelated-role"assume_role_policy= jsonencode({Version="2012-10-17"Statement=[{Action="sts:AssumeRole"Effect="Allow"Principal={Service="s3.amazonaws.com"}}]})}# Targets a bucket name derived from an unrelated, unresolved resource
# attribute - not aws_s3_bucket.positive10, which remains genuinely
# unversioned. The single-bucket-in-document heuristic previously assumed
# this must target positive10 since it's the only bucket in the
# document - it doesn't; the reference here has nothing to do with
# positive10 at all.
resource"aws_s3_bucket_versioning""external_target"{bucket=aws_iam_role.unrelated.unique_idversioning_configuration{status="Enabled"}}