如何将环境变量传递给terraform模块

时间:2019-02-06 14:55:10

标签: terraform terraform-provider-aws

我对terraform还是比较陌生,正在这里寻求帮助!我想引用一个模块并将其部署在多个AWS区域中。我还想将一些环境变量传递给模块,如下所示:

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

我希望我的源代码跨这些区域部署,并希望将环境变量传递给模块。该怎么做?谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您将变量传递给terraform可执行文件:

TF_VAR_REGION=eu-central-1 terraform plan

这将创建REGION变量,然后您可以将其传递给模块:

module "aws-eu-central-1" {
  source="git::https://<git-repo-url>"
  region="{var.REGION}"
}
相关问题