Skip to content

Instantly share code, notes, and snippets.

@ru-rocker
Created October 7, 2022 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ru-rocker/1e9fd31787740c4b69c6cdfcf14ef4e9 to your computer and use it in GitHub Desktop.
Save ru-rocker/1e9fd31787740c4b69c6cdfcf14ef4e9 to your computer and use it in GitHub Desktop.
# sample for lifecycle config
variable "bucket_lifecycle_config" {
type = list(any)
description = "Bucket lifecycle. Set empty list if you want to ignore the lifecycle configuration."
default = []
}
bucket_lifecycle_config = [{
"id" = "DisposePLIST"
"prefix" = "disposable"
"status" = "Enabled"
"noncurrent_version_expiration" = {
"noncurrent_days" = 1
"newer_noncurrent_versions" = null
}
"expiration" = {
"expiration_days" = 1
"expired_object_delete_marker" = true
}
"transition" = []
"noncurrent_version_transition" = []
}]
# resource
resource "aws_s3_bucket_lifecycle_configuration" "lifecycle_config" {
bucket = aws_s3_bucket.bucket.id
for_each = {
for index, v in var.bucket_lifecycle_config :
v.id => v
}
rule {
id = each.value.id
filter {
prefix = each.value.prefix
}
dynamic "expiration" {
for_each = try(each.value.expiration, -1) != -1 ? tolist([each.value.expiration]) : []
content {
days = expiration.value.expiration_days
expired_object_delete_marker = expiration.value.expired_object_delete_marker
}
}
dynamic "transition" {
for_each = each.value.transition
content {
days = transition.value.transition_days
storage_class = transition.value.transition_storage_class
}
}
dynamic "noncurrent_version_transition" {
for_each = each.value.noncurrent_version_transition
content {
noncurrent_days = noncurrent_version_transition.value.noncurrent_version_transition_days
storage_class = noncurrent_version_transition.value.noncurrent_version_storage_class
}
}
dynamic "noncurrent_version_expiration" {
for_each = try(each.value.noncurrent_version_expiration, -1) != -1 ? tolist([each.value.noncurrent_version_expiration]) : []
content {
noncurrent_days = noncurrent_version_expiration.value.noncurrent_days
newer_noncurrent_versions = noncurrent_version_expiration.value.newer_noncurrent_versions
}
}
status = each.value.status
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment