Skip to content

Instantly share code, notes, and snippets.

@bilbof
Created June 12, 2024 13:47
Show Gist options
  • Save bilbof/584fe61486e2fcad6ebf2cdb157de2e1 to your computer and use it in GitHub Desktop.
Save bilbof/584fe61486e2fcad6ebf2cdb157de2e1 to your computer and use it in GitHub Desktop.

Terraform Puppet Hiera

Here's how to do Puppet-like data hierarchy in pure Terraform. No need for extra tooling like Terragrunt / Terraform Cloud / Pulumi etc. if you're just loading envs, common, region and app data...

Input

terraform plan -var="env=test" -var="deployment=apps/api" -var="region=us-west-1"

Output

Changes to Outputs:
  + data = {
      + bar     = "buzz"
      + environ = "test"
      + foo     = "bar"
    }
module "vars" {
source = "./modules/variables"
environment = "test"
deployment = "apps/api"
}
output "data" {
value = module.vars.data
}
// Provides Puppet Hiera-like functionality
// Loads YAML files and merges them into a single map
variable "environment" {
type = string
}
variable "deployment" {
type = string
}
output "data" {
value = merge(
yamldecode(file("./data/common.yaml")),
yamldecode(file("./data/environments/${var.environment}.yaml")),
yamldecode(file("./data/deployments/${var.deployment}.yaml"))
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment