Interview Prep/Terraform/Variables, Locals, Outputs and Types

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.

All Terraform questions Roadmap: Terraform

More on Variables, Locals, Outputs and Types