I'm trying to dynamically create s3 buckets using a map, but changing certain parameters for each bucket.
custom_s3_buckets = {
"bucket1" = {
"bucket" = "aaaa",
"acl" = "private"
},
"bucket2" = {
"bucket" = "bbbb",
"versioning" = true
},
"bucket3" = {
"bucket" = "cccc"
}
}
The resource should expect variables:
resource "aws_s3_bucket" "s3_buckets" {
bucket = var.bucket_name
acl = var.acl
versioning = {
enabled = var.versioning
}
}
And the variables should be filled with the above custom_s3_buckets map and create as many buckets as there are keys in the map, filling only changed values and leaving the rest as default in variables.tf
variable "bucket_name" {
type = string
}
variable "acl" {
type = string
default = "private"
}
variable "versioning" {
type = bool
default = false
}
I assume I need a for_each here, but can't seem to make it work.
Relevant sources: Terraform - iterate over nested map