Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
YAML has anchors and references, which are useful for reducing duplication and still allowing overrides of default values.
Would allow default values without having to use (currently experimental) optional fields in variable definitions paired with coalesce statements.
variable "probes" {
type = object({
name = string
interval = number
minimum_servers = number
path = string
pick_host_name_from_backend_http_settings = bool
port = number
protocol = string
timeout = number
unhealthy_threshold = number
success_status_codes = set(string)
})
}
Attempted Solutions
Yaml files can be loaded in as a local value, but this lacks the checking and safety of variable declarations.
Doesn't support custom validators or secret values.
variable "probes" {
type = object({
name = string
interval = number
minimum_servers = number
path = string
pick_host_name_from_backend_http_settings = bool
port = number
protocol = string
timeout = number
unhealthy_threshold = number
success_status_codes = set(string)
})
default=yamldecode(file("terraform.yml.tfvars"))
}
Proposal
Terraform already supports terraform.json.tfvars files.
One hacky solution would be a preprocessor to convert the yaml to json with existing HCL functions before passing it to whatever mechanisms consume the normal json file.
Current Terraform Version
Use-cases
YAML has anchors and references, which are useful for reducing duplication and still allowing overrides of default values.
Would allow default values without having to use (currently experimental) optional fields in variable definitions paired with
coalesce
statements.Example
terraform.yml.tfvars
variables.tf
Attempted Solutions
Yaml files can be loaded in as a local value, but this lacks the checking and safety of variable declarations.
Doesn't support custom validators or secret values.
(terraform docs on variables for reference)
Additionally, for optional fields,
try
must be used instead ofcoalesce
due to undefined fields throwing errors rather than returning null.Functions aren't allowed in variable defaults, so something like this doesn't work:
Proposal
Terraform already supports
terraform.json.tfvars
files.One hacky solution would be a preprocessor to convert the yaml to json with existing HCL functions before passing it to whatever mechanisms consume the normal json file.
References