Deepak Prasad
Deepak Prasad

Reputation: 301

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!

Upvotes: 3

Views: 3538

Answers (1)

victor m
victor m

Reputation: 2175

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}"
}

Upvotes: 4

Related Questions