Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: s3 object-lifecycle-rule to support different syntaxes for filter block #66

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 10 additions & 5 deletions _example/complete/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ module "s3_bucket" {
years = null
}

versioning = true
versioning = true
force_destroy = true
vpc_endpoints = [
{
endpoint_count = 1
Expand Down Expand Up @@ -211,9 +212,7 @@ module "s3_bucket" {
lifecycle_configuration_rules = [
{
id = "log"
prefix = null
enabled = true
tags = { "temp" : "true" }
enable_glacier_transition = false
enable_deeparchive_transition = false
enable_standard_ia_transition = false
Expand All @@ -228,12 +227,14 @@ module "s3_bucket" {
deeparchive_transition_days = 0
storage_class = "GLACIER"
expiration_days = 365
filter = {
prefix = "myfolder1/myfolder2/"
tags = { "temp" : "true" }
}
},
{
id = "log1"
prefix = null
enabled = true
tags = {}
enable_glacier_transition = false
enable_deeparchive_transition = false
enable_standard_ia_transition = false
Expand All @@ -248,6 +249,10 @@ module "s3_bucket" {
glacier_transition_days = 0
deeparchive_transition_days = 0
expiration_days = 365
filter = {
prefix = null
tags = {}
}
}
]

Expand Down
12 changes: 8 additions & 4 deletions _example/website-s3/example.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ module "s3_bucket" {
lifecycle_configuration_rules = [
{
id = "log"
prefix = null
enabled = true
tags = { "temp" : "true" }
enable_glacier_transition = false
enable_deeparchive_transition = false
enable_standard_ia_transition = false
Expand All @@ -67,12 +65,14 @@ module "s3_bucket" {
glacier_transition_days = 0
deeparchive_transition_days = 0
expiration_days = 365
filter = {
prefix = null
tags = { "temp" : "true" }
}
},
{
id = "log1"
prefix = null
enabled = true
tags = {}
enable_glacier_transition = false
enable_deeparchive_transition = false
enable_standard_ia_transition = false
Expand All @@ -86,6 +86,10 @@ module "s3_bucket" {
glacier_transition_days = 0
deeparchive_transition_days = 0
expiration_days = 365
filter = {
prefix = null
tags = {}
}
}
]

Expand Down
51 changes: 45 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,51 @@ resource "aws_s3_bucket_lifecycle_configuration" "default" {
status = rule.value.enabled == true ? "Enabled" : "Disabled"

# Filter is always required due to https://github.com/hashicorp/terraform-provider-aws/issues/23299
filter {
dynamic "and" {
for_each = (try(length(rule.value.prefix), 0) + try(length(rule.value.tags), 0)) > 0 ? [1] : []
content {
prefix = rule.value.prefix == null ? "" : rule.value.prefix
tags = try(length(rule.value.tags), 0) > 0 ? rule.value.tags : {}

# -- The `filter` block supports two types of syntaxes, with `and {}` & without `and`
# -- With `and {}` is used when user passes more than 1 attribute inside `filter` block OR if `tags` attribute is being used.
# -- Creating 3 dynamic block for `filter` to satisfy all 3 conditions -
#
# 1: required `filter` block for `aws_s3_bucket_lifecycle_configuration` resource
# 2: with `and`
# 3: without `and` + `tags` attribute

# -- `filter` block is required for `aws_s3_bucket_lifecycle_configuration` resource
dynamic "filter" {
for_each = length(try(flatten([rule.value.filter]), [])) == 0 ? [true] : []
content {}
}

# -- block without `and`
dynamic "filter" {
for_each = [for v in try(flatten([rule.value.filter]), []) : v if max(length(keys(v)), length(try(rule.value.filter.tags, rule.value.filter.tag, []))) == 1]
themaniskshah marked this conversation as resolved.
Show resolved Hide resolved

content {
object_size_greater_than = try(filter.value.object_size_greater_than, null)
object_size_less_than = try(filter.value.object_size_less_than, null)
prefix = try(filter.value.prefix, null)

dynamic "tag" {
for_each = try(filter.value.tags, filter.value.tag, [])

content {
key = tag.key
value = tag.value
}
}
}
}

# -- block with `and`
dynamic "filter" {
for_each = [for v in try(flatten([rule.value.filter]), []) : v if max(length(keys(v)), length(try(rule.value.filter.tags, rule.value.filter.tag, []))) > 1]

content {
and {
object_size_greater_than = try(filter.value.object_size_greater_than, null)
object_size_less_than = try(filter.value.object_size_less_than, null)
prefix = try(filter.value.prefix, null)
tags = try(filter.value.tags, filter.value.tag, null)
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ variable "enable_lifecycle_configuration_rules" {
variable "lifecycle_configuration_rules" {
type = list(object({
id = string
prefix = string
enabled = bool
tags = map(string)
filter = any
themaniskshah marked this conversation as resolved.
Show resolved Hide resolved

enable_glacier_transition = bool
enable_deeparchive_transition = bool
Expand Down