AzureRm „global Tags“ and modules

We are using many self created modules for our terraform configuration.

We would like to extend the modules, so that they will do the tagging automatically.

One Tag we want to add is the environment, so like env=production.
We have first try to define

locals { env = “production“ }

In the root and to use local.env in the module. But as expected it fails as the local.env will only work in the root, but not in used modules.

Is it really the only way to define a variable for the module and pass local.env To that variable?

Hi @joerg.lang,

Each module has an independent namespace, and so the only way data passes between modules is either inwards with input variables or outwards with output values. This encapsulation is important to make modules actually be modular, since otherwise their meaning would be different depending on which other module you’re calling from.

There is still some design flexibility here to think about what is the best way for the child module to consume this information, though.

One way is to just directly pass in the environment string as its own variable as you’ve described. This means that all of the modules will share this common concept of “environment” and have to each interpret it in a consistent way.

However, a second approach which can work well in some settings is to use a different level of abstraction for the child modules so that they are focused more on what actual objects are being created and not on high-level ideas like “environment”. A common version of that principle is to make the child module just accept a generic set of tags to apply verbatim to all taggable objects, with the module then not making any assumptions at all about what those tags might mean and therefore keeping the tagging scheme defined centrally in the root module.

In the root module, then:

locals {
  env = "production"

  azure_common_tags = {
    env = local.env
  }
}


module "something" {
  source = "./modules/azure/something"

  common_tags = local.azure_common_tags

}

Inside the module:

variable "common_tags" {
  type = map(string)
}

Then use var.common_tags as part of every taggable resource declared in the module, so that they will all be tagged consistently using the central tagging scheme even though the child module need not actually know what the tagging scheme is, and so you can modify it centrally in future if you need to add a new tag or change the definition of an existing one.