Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

BATIAI-2511 - Adding lifecycle rules for bucket versioning #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ No modules.
| <a name="input_s3_bucket_names"></a> [s3\_bucket\_names](#input\_s3\_bucket\_names) | n/a | `list(string)` | `[]` | no |
| <a name="input_sse_algorithm"></a> [sse\_algorithm](#input\_sse\_algorithm) | The server-side encryption algorithm to use. Valid values are AES256 and aws:kms, defaults to aws:kms. | `string` | `"aws:kms"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | n/a | `map(any)` | `{}` | no |
| <a name="input_version_lifecycle_expiration_days"></a> [version\_lifecycle\_expiration\_days](#input\_version\_lifecycle\_expiration\_days) | Number of days for a bucket version's lifecycle to expire. Defaults to 0, which disables the rule | `number` | `"0"` | no |
| <a name="input_versioning_enabled"></a> [versioning\_enabled](#input\_versioning\_enabled) | n/a | `bool` | `false` | no |

## Outputs
Expand Down
19 changes: 17 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "bucket" {
# Lifecycle configuration for the dev buckets to remove all objects older than var.lifecycle_expiration_days.
resource "aws_s3_bucket_lifecycle_configuration" "lifecycle_expiration_days" {
## Iterate over the list from var's to avoid some chicken/egg problems
for_each = var.lifecycle_expiration_days > 0 ? toset(var.s3_bucket_names) : []
for_each = (var.lifecycle_expiration_days > 0) || (var.version_lifecycle_expiration_days > 0) ? toset(var.s3_bucket_names) : []
## Refer to the id from the bucket resource to retain the dependency
bucket = aws_s3_bucket.landing_zone_buckets[each.value].id

dynamic "rule" {
for_each = var.lifecycle_expiration_days > 0 ? [1] : []
for_each = var.lifecycle_expiration_days > 0 ? var.s3_bucket_names : []

content {
id = "delete-old-objects"
Expand All @@ -154,4 +154,19 @@ resource "aws_s3_bucket_lifecycle_configuration" "lifecycle_expiration_days" {
}
}
}

rule {
#for_each = var.version_lifecycle_expiration_days > 0 ? var.s3_bucket_names : []

content {
id = "delete-old-versions"
status = "Enabled"
expiration {
days = var.version_lifecycle_expiration_days
}
noncurrent_version_expiration {
noncurrent_days = 7
}
}
}
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ variable "lifecycle_expiration_days" {
description = "Number of days for object lifecycle to expire the objects in dev env. Defaults to 0, which disables the rule"
}

variable "version_lifecycle_expiration_days" {
type = number
default = "0"
description = "Number of days for a bucket version's lifecycle to expire. Defaults to 0, which disables the rule"
}

variable "versioning_enabled" {
type = bool
default = false
Expand Down
Loading