Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass environment variables to terraform modules

i am relatively new to terraform and looking for some help here! i want to refer a module and have it deployed across multiple AWS regions. I also want to pass few environment variables into the module something like this:

module "aws-eu-central-1" {
  source="git::https://<git-repo-url>"
  export TF_VAR_REGION="eu-central-1"
  export TF_VAR_TABLE_NAME="euc-accounts"
  export TF_VAR_ES_ENDPOINT="euc-elasticsearch"
  export TF_VAR_LOG_LEVEL="INFO"
}

module "aws-eu-west-1" {
  source="git::https://<git-repo-url>"
  export TF_VAR_REGION="eu-west-1"
  export TF_VAR_TABLE_NAME="euw-accounts"
  export TF_VAR_ES_ENDPOINT="euw-elasticsearch"
  export TF_VAR_LOG_LEVEL="INFO"
}

module "aws-eu-west-2" {
  source="git::https://<git-repo-url>"
  export TF_VAR_REGION="eu-west-2"
  export TF_VAR_TABLE_NAME="euw-accounts"
  export TF_VAR_ES_ENDPOINT="euw-elasticsearch"
  export TF_VAR_LOG_LEVEL="INFO"
}

i want my source code to get deployed across these regions and want to pass the environment variables to the module. how this can be done? thanks for the help!

like image 910
Deepak Prasad Avatar asked Jan 20 '26 13:01

Deepak Prasad


1 Answers

You pass the variable to the terraform executable:

TF_VAR_REGION=eu-central-1 terraform plan

That creates the REGION variable which you can then pass to the module:

module "aws-eu-central-1" {
  source="git::https://<git-repo-url>"
  region="{var.REGION}"
}
like image 66
victor m Avatar answered Jan 23 '26 18:01

victor m