slipperypete
slipperypete

Reputation: 6276

How to define global variables in terraform?

I have a terraform project I am working on. In it, I want a file to contain many variables. I want these variables to be accessible from any module of the project. I have looked in the docs and on a Udemy course but still don't see how to do this. How does one do this in terraform?

Upvotes: 23

Views: 31818

Answers (4)

Dzintars
Dzintars

Reputation: 1571

You can use -var-file to pass the overrides. Example:

terraform -chdir=staging/000base apply -var-file ../../global.tfvars

Other way can be dedicated shared module. For example you have module with:

output "my_var" {
  value = "apollo440"
}

And in other modules you can use it like:

module "gvars" {
  # source = "[email protected]:user/terraform-global-vars.git?ref=v0.0.1-alpha"
  source="../../../modules/global_vars"
}

provider "something" {
  name="${module.gvars.my_var}"
}

Third way could be to use https://registry.terraform.io/providers/hashicorp/http/latest/docs/data-sources/http to query some http endpoint.

Ouh... and forth way could be to utilize Vault Provider https://registry.terraform.io/providers/hashicorp/vault/latest/docs

Upvotes: 2

Alex Malyshev
Alex Malyshev

Reputation: 321

For pathing constants between lots of modules we can use the following simple way.

/modules/global_constants/outputs.tf:

Describe module with global constants as outputs:

output "parameter_1" {
  value     = "value_1"
  sensitive = true
}

output "parameter_2" {
  value     = "value_2"
  sensitive = true
}

/example_1.tf

After we can use in any *.tf

module "global_settings" {
   source = "./modules/global_constants"
}

data "azurerm_key_vault" "keyvault" {
   name                = module.global_settings.parameter_1
   resource_group_name = module.global_settings.parameter_2
}

/modules/module2/main.tf

Or in other any modules:

module "global_settings" {
   source = "../global_constants"
}

data "azurerm_key_vault" "keyvault" {
   name                = module.global_settings.parameter_1
   resource_group_name = module.global_settings.parameter_2
}

Upvotes: 14

ha9u63a7
ha9u63a7

Reputation: 6864

I try to do this

  1. Define a the set of common variables e.g. common_tags at the bottom/top of the variables.tf file for all modules. Usually, your tech ops admin/cloud admin will have a template project created for this.

  2. For each module, I add the following as the last item

    global_var = var.global_var_val

An example is common tags. In any root/child module I combine them using merge() function. I hope that makes sense.

Upvotes: 6

pabloxio
pabloxio

Reputation: 1493

I don't think this is possible. There are several discussions about this at Github, but this is not something the Hashicorp team wants.

In general we're against the particular solution of Global Variables, since it makes the input -> resources -> output flow of Modules less explicit, and explicitness is a core design goal.

I know, we have to repeat a lot of variables between different modules

Upvotes: 17

Related Questions