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



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

我希望我的源代码部署在这些区域,并希望将环境变量传递给模块。 如何做到这一点? 感谢您的帮助!

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

TF_VAR_REGION=eu-central-1 terraform plan

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

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

最新更新