Dlayknee
Dlayknee

Reputation: 143

How to define global variables for a Terragrunt deployment

How do I define/reference a set of variables to be re-used throughout my Terragrunt deployment? I'm currently specifying the same values (e.g., subscription, resource_group, environment, etc) in a locals{} block at each level of my deployment, but that seems completely opposite of the way to use Terragrunt.

My folder structure is similar to:

├── Folder A
│   └── main.tf
│   └── terragrunt.hcl
├── Folder B
│   └── main.tf
│   └── terragrunt.hcl
└── global_vars.yaml
└── main.tf
└── terragrunt.hcl

I read up on the Locals feature so created the global_vars.yaml in the root with contents such as:

environment = "prod"

and tried adding a block to the parent terragrunt.hcl:

locals {
  global_vars = yamldecode(file(find_in_parent_folders("global_vars.yaml")))
}

and then in the FolderA main.tf:

locals {
  ...
  environment = local.common_vars.environment
  ...
}

but I get errors that the local value with the name "global_vars" hasn't been declared.

To add another layer of fun, there's the possibility that I may want to override a given variable at a child level - but I feel like once I can tackle this declaration issue, the rest should be pretty easy (...right?).

Upvotes: 3

Views: 1228

Answers (1)

Dlayknee
Dlayknee

Reputation: 143

Turns out I was making it way too complicated. To fix this, I had to fix my yaml formatting to actually be a map:

locals:
  environment : "dev"

and add a line in the locals{} block of the children main.tf's with:

global_vars = yamldecode(file("../../global_vars.yaml"))

Then I reference the values like this:

environment = local.global_vars.locals.environment

There are probably prettier ways to do it, but this one is mine!

Upvotes: 2

Related Questions