Last active
May 7, 2021 22:25
-
-
Save dayglojesus/206a95c390487c1310a681f497218498 to your computer and use it in GitHub Desktop.
Not sure why this does what it does
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########################################################### | |
# Setup | |
########################################################### | |
terraform { | |
required_version = ">= 0.15" | |
experiments = [ | |
module_variable_optional_attrs | |
] | |
} | |
########################################################### | |
# Some variables to act as defaults | |
########################################################### | |
variable "default_string" { | |
default = "string" | |
type = string | |
} | |
variable "default_number" { | |
default = 42 | |
type = number | |
} | |
variable "default_list" { | |
default = [] | |
type = list(string) | |
} | |
variable "default_map" { | |
default = {} | |
type = map(string) | |
} | |
########################################################### | |
# Var representing an INPUT as from a module's output or | |
# another source (it doesn't matter) | |
########################################################### | |
variable "input" { | |
type = map(object({ | |
string = optional(string) | |
number = optional(number) | |
list = optional(list(string)) | |
map = optional(map(string)) | |
})) | |
default = { | |
foo = { | |
number = 99 | |
map = { | |
bingo = "bango" | |
} | |
} | |
} | |
} | |
########################################################### | |
# Synthesis | |
########################################################### | |
locals { | |
###################### | |
# Collect the defaults | |
defaults = { | |
string = var.default_string | |
number = var.default_number | |
list = var.default_list | |
map = var.default_map | |
} | |
input = { for name, overrides in var.input: | |
name => { | |
for attr, obj in overrides: | |
attr => obj | |
if obj != null | |
} | |
} | |
###################### | |
# Combine defaults with INPUT | |
combined = { | |
for name, overrides in local.input: | |
name => merge(local.defaults, overrides) | |
} | |
} | |
###################### | |
# RESULT | |
output "combined" { | |
value = local.combined | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so weird