Im looking to manage hundreds of AWS S3 buckets using Terraform and would like to create all of them by defining a variable list array of bucket names along with a few other s3 resource specific parameters, loop thru that list with a for_each and then use the results from that array to define the bucket configuration. The issue that we're running into are the lifecycle rules. Our buckets have several different lifecycle rules based om the bucket requirements, but out of say 1000 buckets, we have maybe 10 different lifecycle rule. Using the aws cli, its easy enough to manage bucket lifecycles, it is its own specific command:
aws s3api put-bucket-lifecycle-configuration --bucket my-bucket --lifecycle-configuration file://lifecycle_1.json
and we can choose from a short list of lifecycle_X.json files to modify the rules.
The problem we're running into is that the lifecycle attribute in the terraform resource "aws_s3_bucket" is not, or cannot be a template? Apparently it was a template back in version 11, but since version 12 it has to be some block of code. What I'd like to do is something like this:
locals {
lifecycle_rule_1 = [
id = "rule1"
prefix = ""
enabled = true
expiration = [
{
days = 30
}
]
lifecycle_rule_2 = [
id = "rule2"
prefix = ""
enabled = true
expiration = [
{
days = 60
}
]
}
resource "aws_s3_bucket" "bucket" {
for_each = locals.bucket_definitions
bucket = "my-bucket"
acl = "private"
lifecycle_rule = [locals.lifecycle_rule_1]
}
Just pinging here is anyone has come up with a similar problem and a solution? Thanks