Skip to content

Commit 5de0fb5

Browse files
author
Shanawaze
committed
adding lifecycle rules for bucket versioning
1 parent 48147d6 commit 5de0fb5

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

main.tf

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "bucket" {
136136
# Lifecycle configuration for the dev buckets to remove all objects older than var.lifecycle_expiration_days.
137137
resource "aws_s3_bucket_lifecycle_configuration" "lifecycle_expiration_days" {
138138
## Iterate over the list from var's to avoid some chicken/egg problems
139-
for_each = var.lifecycle_expiration_days > 0 ? toset(var.s3_bucket_names) : []
139+
for_each = var.lifecycle_expiration_days || var.version_lifecycle_expiration_days > 0 ? toset(var.s3_bucket_names) : []
140140
## Refer to the id from the bucket resource to retain the dependency
141141
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id
142142

@@ -154,4 +154,19 @@ resource "aws_s3_bucket_lifecycle_configuration" "lifecycle_expiration_days" {
154154
}
155155
}
156156
}
157+
158+
dynamic "version_rule" {
159+
for_each = var.version_lifecycle_expiration_days > 0 ? [1] : []
160+
161+
content {
162+
id = "delete-old-versions"
163+
status = "Enabled"
164+
expiration {
165+
days = var.version_lifecycle_expiration_days
166+
}
167+
noncurrent_version_expiration {
168+
noncurrent_days = 7
169+
}
170+
}
171+
}
157172
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ variable "lifecycle_expiration_days" {
3131
description = "Number of days for object lifecycle to expire the objects in dev env. Defaults to 0, which disables the rule"
3232
}
3333

34+
variable "version_lifecycle_expiration_days" {
35+
type = number
36+
default = "0"
37+
description = "Number of days for a bucket version's lifecycle to expire. Defaults to 0, which disables the rule"
38+
}
39+
3440
variable "versioning_enabled" {
3541
type = bool
3642
default = false

0 commit comments

Comments
 (0)