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

Allows S3 Lifecycle for RDS Dump Bucket #82

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,32 @@ Type: `string`

Default: `""`

### <a name="input_rds_s3_dump_lifecycle_rules"></a> [rds\_s3\_dump\_lifecycle\_rules](#input\_rds\_s3\_dump\_lifecycle\_rules)

Description: RDS S3 Dump Lifecycle rules

Type:

```hcl
list(object({
id = string
status = optional(string, "Enabled")
prefix = string
expiration = optional(list(object({
days = optional(number)
date = optional(string)
expired_object_delete_marker = optional(bool)
})), [])
transition = optional(list(object({
days = optional(number)
date = optional(string)
storage_class = string
})), [])
}))
```

Default: `[]`

### <a name="input_rds_identifier_override"></a> [rds\_identifier\_override](#input\_rds\_identifier\_override)

Description: RDS identifier override. Use only lowercase, numbers and -, \_., only use when it needs to be different from var.name
Expand Down
30 changes: 30 additions & 0 deletions examples/rds/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,36 @@ module "ms_sample_rds" {
# rds_enabled enables RDS
rds_enabled = true

rds_s3_dump_lifecycle_rules = [
{
id = "all-cleanup"
status = "Enabled"
prefix = ""
expiration = [{
days = 90
}]
},
{
id = "tmp"
status = "Enabled"
prefix = "tmp/"
expiration = [{
days = 1
}]
},
{
id = "MoveAllToGlacierAfterTwoWeeks"
status = "Enabled"
prefix = ""
transition = [
{
days = 28
storage_class = "GLACIER"
}
]
}
]

# rds_allowed_subnet_cidrs specifices the allowed subnets
#rds_allowed_subnet_cidrs = ["127.0.0.1/32"]

Expand Down
38 changes: 38 additions & 0 deletions rds-s3-dumps.tf
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,41 @@ resource "aws_db_instance_role_association" "this" {
feature_name = "S3_INTEGRATION"
role_arn = var.rds_s3_dump_role_arn == "" ? aws_iam_role.rds_dumps[0].arn : var.rds_s3_dump_role_arn
}

resource "aws_s3_bucket_lifecycle_configuration" "rds_dumps" {
depends_on = [aws_s3_bucket_versioning.rds_dumps]

count = local.rds_dumps_enabled && length(var.rds_s3_dump_lifecycle_rules) > 0 ? 1 : 0
bucket = aws_s3_bucket.rds_dumps[count.index].id

dynamic "rule" {
for_each = var.rds_s3_dump_lifecycle_rules

content {
id = rule.value.id
status = rule.value.status
filter {
prefix = rule.value.prefix
}

dynamic "expiration" {
for_each = rule.value.expiration

content {
date = expiration.value.date
days = expiration.value.days
expired_object_delete_marker = expiration.value.expired_object_delete_marker
}
}

dynamic "transition" {
for_each = rule.value.transition
content {
date = transition.value.date
days = transition.value.days
storage_class = transition.value.storage_class
}
}
}
}
}
51 changes: 49 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,51 @@ variable "rds_s3_dump_role_arn" {
default = ""
}

variable "rds_s3_dump_lifecycle_rules" {
description = "RDS S3 Dump Lifecycle rules"
default = []
type = list(object({
id = string
status = optional(string, "Enabled")
prefix = string
expiration = optional(list(object({
days = optional(number)
date = optional(string)
expired_object_delete_marker = optional(bool)
})), [])
transition = optional(list(object({
days = optional(number)
date = optional(string)
storage_class = string
})), [])
}))

validation {
condition = length(var.rds_s3_dump_lifecycle_rules) > 0 ? alltrue([
for k, v in var.rds_s3_dump_lifecycle_rules : (length(v["expiration"]) <= 1)
]) : true
error_message = "Only one `expiration` block is allowed."
}

validation {
condition = length(var.rds_s3_dump_lifecycle_rules) > 0 ? alltrue(flatten([
for k, v in var.rds_s3_dump_lifecycle_rules : [
for bk, bv in v["expiration"] : (bv["days"] == null || bv["date"] == null)
]
])) : true
error_message = "Either `days` or `date` value should be set for `expiration`, but not both."
}

validation {
condition = length(var.rds_s3_dump_lifecycle_rules) > 0 ? alltrue(flatten([
for k, v in var.rds_s3_dump_lifecycle_rules : [
for bk, bv in v["transition"] : (bv["days"] == null || bv["date"] == null)
]
])) : true
error_message = "Either `days` or `date` value should be set for `transition`, but not both."
}
}

variable "rds_identifier_override" {
description = "RDS identifier override. Use only lowercase, numbers and -, _., only use when it needs to be different from var.name"
default = ""
Expand Down Expand Up @@ -1164,15 +1209,17 @@ variable "s3_lifecycle_rules" {
condition = length(var.s3_lifecycle_rules) > 0 ? alltrue(flatten([
for k, v in var.s3_lifecycle_rules : [
for bk, bv in v["expiration"] : (bv["days"] == null || bv["date"] == null)
]])) : true
]
])) : true
error_message = "Either `days` or `date` value should be set for `expiration`, but not both."
}

validation {
condition = length(var.s3_lifecycle_rules) > 0 ? alltrue(flatten([
for k, v in var.s3_lifecycle_rules : [
for bk, bv in v["transition"] : (bv["days"] == null || bv["date"] == null)
]])) : true
]
])) : true
error_message = "Either `days` or `date` value should be set for `transition`, but not both."
}
}
Expand Down
Loading