Skip to content

Instantly share code, notes, and snippets.

@a-nldisr
Last active November 18, 2022 16:15
Show Gist options
  • Save a-nldisr/bf43f19cac2687fa50e3768e40143f7f to your computer and use it in GitHub Desktop.
Save a-nldisr/bf43f19cac2687fa50e3768e40143f7f to your computer and use it in GitHub Desktop.
terraform_mapped_vars_for_each
# This is the resource to create
module "config_generator" {
source = "map_module_main.tf" // ? usually point to github url or place on disk where the module exists
name = var.config_names
}
variable "config_names" {
type = list(any)
default = [
"app",
"db"
]
description = "Two configs generated"
}
# How to create a map and add two variables to it in Terraform. This would be the module
variable "environment" {
type = list(any)
default = [
"test",
"acce",
"prod",
]
description = "Environments to create, hardcoded"
}
variable "name" {
type = list(any)
default = []
description = "Config name"
}
locals {
environment = [
for key, env in var.environment : {
key = key
env = env
}
]
name = [
for key, name in var.name : {
key = key
name = name
}
]
mapped = [
for pair in setproduct(local.environment, local.name) : {
env = pair[0].env
name = pair[1].name
}
]
}
// Resource does not exist, so dont copy and paste it
resource "config" "main" {
for_each = {
for mapped in local.mapped : "${mapped.env}.${mapped.name}" => mapped
}
environment = each.value.env
name = each.value.name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment