CodexBloom - Programming Q&A Platform

Terraform: implementing S3 Bucket Versioning and Lifecycle Rules in AWS

πŸ‘€ Views: 11 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-12
terraform aws s3 versioning lifecycle hcl

Hey everyone, I'm running into an issue that's driving me crazy..... I'm deploying to production and I'm working with a question with configuring S3 bucket versioning and lifecycle rules using Terraform... I want to enable versioning on my S3 bucket and set up lifecycle rules to delete older versions after 30 days, but it seems that the lifecycle rules are not being applied correctly. Here's the code I'm currently using: ```hcl resource "aws_s3_bucket" "my_bucket" { bucket = "my-unique-bucket-name" acl = "private" versioning { enabled = true } lifecycle_rule { id = "delete_old_versions" enabled = true noncurrent_version_expiration { days = 30 } } } ``` I’ve double-checked the bucket name and IAM permissions, and I can confirm that Terraform applies the changes successfully. However, after applying the configuration, I check the S3 bucket in the AWS Console, and I see that versioning is enabled, but the lifecycle rules don’t seem to be taking effect. When I run `terraform plan`, it indicates that there are no changes detected, so it seems like the lifecycle rule is being ignored altogether. I've also tried adding a `scope` block to specify the rules, like this: ```hcl lifecycle_rule { id = "delete_old_versions" enabled = true noncurrent_version_expiration { days = 30 } prefix = "" status = "Enabled" } ``` However, that didn't help either. I wonder if there are any specific requirements or limitations for configuring lifecycle rules with versioned buckets that I might be missing. Could anyone provide insight into why the lifecycle rules aren't being applied? Is there something I'm overlooking with the configuration or Terraform's behavior? I’m using Terraform version 1.0.0 and the AWS provider version 3.40.0. For context: I'm using Hcl on Ubuntu. I'd really appreciate any guidance on this. What are your experiences with this? I've been using Hcl for about a year now. Is there a better approach? I'm on Ubuntu 20.04 using the latest version of Hcl. How would you solve this?