How would you design a variable that creates several similar resources?
IntermediateConceptVariables, Locals, Outputs and Types
Answer
Use map(object(...)) keyed by a stable name and feed it to for_each.
variable "buckets" {
type = map(object({
versioning = optional(bool, true)
expire_days = optional(number)
}))
}
resource "aws_s3_bucket" "this" { for_each = var.buckets bucket = each.key }
Adding or removing a key touches only that resource.
More on Variables, Locals, Outputs and Types
How do you declare an input variable?BeginnerIn what order does Terraform load variable values?BeginnerHow do you define an output and what is it for?BeginnerWhat is the difference between locals and variables?BeginnerWhat are the primitive types in Terraform?BeginnerHow do list, set and map differ?Intermediate