I’m looking for a way to define a block once, and re-use it across multiple resource definitions. More specifically, I’d like to define a standard set of lifecycle rules, and reference them in a large number of S3 bucket definitions (aws_s3_bucket). Some buckets will have more lifecycle rules on top of the standard set.
For attributes, this pattern is straightforward, since you are able to use expressions:
someattribute = [concat(vars.shared_list, ["a","b","c"])]
With nested blocks, the closest I’ve been able to get is with dynamic
blocks. However, it is not quite what I need, as I would need to copy+paste the dynamic
block everywhere I wanted to use it, and the schema is fixed. Lifecycle rules vary in what attributes you need to set. I would need several dynamic
blocks, and would have to copy+paste them into every bucket, instead of referring to a single variable.
The best I’ve been able to come up with so far, which is still quite duplicative as I’d need to have this in every resource that uses the standard set:
dynamic "lifecycle_rule" { for_each = local.standard_retention_periods content { id = "${lifecycle_rule.value} day retention" prefix = "retention-${lifecycle_rule.value}days/" dynamic "expiration" { for_each = [1] content { days = lifecycle_rule.value expired_object_delete_marker = "false" } } enabled = "true" abort_incomplete_multipart_upload_days = "0" } }
dynamic "lifecycle_rule" {
# Rules for transitioning objects to glacier after X days
...
}
lifecycle_rule {
# Bucket-wide rule for removing versions older than X days
...
}
What I’d like to have:
lifecycle_rule = vars.standard_set
Any advice on how to minimize the duplication of configuration in this case?
Thanks!