Skip to content

Instantly share code, notes, and snippets.

@grimm26
Created November 11, 2022 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grimm26/fdd47765e2942307e11d2ec5ce662203 to your computer and use it in GitHub Desktop.
Save grimm26/fdd47765e2942307e11d2ec5ce662203 to your computer and use it in GitHub Desktop.
handle presets and custom v2
variable "object_lifecycle_preset" {
description = <<-EOL
Choose a pre-configured set of object lifecycle rules for your bucket.
- empty_bucket: Delete all objects from the bucket. Use this in preparation to destroy the bucket. This is non-recoverable.
EOL
type = string
default = "default"
validation {
condition = contains(["empty_bucket", "default"], var.object_lifecycle_preset)
error_message = "You must select from one of the available presets: empty_bucket, default."
}
}
variable "lifecycle_object_rules" {
description = "List of object describing object lifecycle rules on the noncurrent versions of objects."
type = list(object(
{
id = string
enabled = bool
prefix = optional(string)
tags = optional(map(string), {})
object_size_greater_than = optional(number)
object_size_less_than = optional(number)
abort_incomplete_multipart_upload_days = optional(number)
transitions = optional(map(string), {})
noncurrent_transitions = optional(map(string), {})
}
))
default = []
}
locals {
lifecycle_rule_template = {
id = null
prefix = null
tags = {}
enabled = null
transitions = {}
object_size_greater_than = null
object_size_less_than = null
abort_incomplete_multipart_upload_days = null
noncurrent_transitions = {}
transitions = {}
}
lifecycle_presets = {
default = [
merge(local.lifecycle_rule_template,
{
id = "default noncurrent transition"
# prefix = "foo/bar/"
# tags = { "key" = "value", "key2" = "value2" }
enabled = true
# transitions = {
# standard_ia = 120
# }
object_size_greater_than = 1024 * 128
noncurrent_transitions = {
standard_ia = 30
}
}),
merge(local.lifecycle_rule_template,
{
id = "default noncurrent expiration"
enabled = true
abort_incomplete_multipart_upload_days = 1
noncurrent_transitions = {
expiration = 120
}
transitions = {
expired_object_delete_marker = true
}
}),
]
empty_bucket = [
merge(local.lifecycle_rule_template,
{
id = "empty bucket1"
enabled = true
noncurrent_transitions = {
expiration = 1
}
transitions = {
expiration = 1
}
abort_incomplete_multipart_upload_days = 1
}),
merge(local.lifecycle_rule_template,
{
id = "empty bucket2"
enabled = true
transitions = {
expired_object_delete_marker = true
}
}),
]
}
# If we have custom rules specified, use those. Otherwise use the preset.
lifecycle_object_rules = length(var.lifecycle_object_rules) > 0 ? var.lifecycle_object_rules : local.lifecycle_presets[var.object_lifecycle_preset]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment