Skip to content

Instantly share code, notes, and snippets.

@sl1pm4t
Last active August 28, 2019 00:48
Show Gist options
  • Save sl1pm4t/d9ca17fda47c3fe266d07d3afd49799b to your computer and use it in GitHub Desktop.
Save sl1pm4t/d9ca17fda47c3fe266d07d3afd49799b to your computer and use it in GitHub Desktop.
variable "env" {
default = ["dev", "test", "stage", "prod"]
}
locals {
env = toset(var.env)
}
resource "template_file" "list_toset" {
for_each = local.env
template = "key = $${key}, value = $${value}"
vars = {
key = each.key
value = each.value
}
}
resource "local_file" "list_toset" {
for_each = template_file.list_toset
filename = "list_toset-${each.value.vars.value}-${each.key}"
content = <<EOF
each.key = ${jsonencode(each.key)}
each.value = ${jsonencode(each.value)}
EOF
}
###############
variable "envmap" {
default = {
dev = "dev"
test = "test"
stage = "stage"
prod = "prod"
}
}
resource "template_file" "mapped" {
for_each = local.env
template = "key = $${key}, value = $${value}"
vars = {
key = each.key
value = each.value
}
}
resource "local_file" "mapped" {
for_each = template_file.mapped
filename = "mapped-${each.value.vars.value}-${each.key}"
content = <<EOF
each.key = ${jsonencode(each.key)}
each.value = ${jsonencode(each.value)}
EOF
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment