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

如何将环境变量传递给terraform模块,terraform,terraform-provider-aws,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=

我对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"
}
模块“aws-eu-central-1”{
source=“git::https://”
出口TF_VAR_REGION=“eu-central-1”
导出TF\u VAR\u表\u NAME=“euc账户”
导出TF_VAR_ES_ENDPOINT=“euc elasticsearch”
导出TF\u VAR\u LOG\u LEVEL=“INFO”
}
模块“aws-eu-west-1”{
source=“git::https://”
出口TF_VAR_REGION=“eu-west-1”
导出TF\u VAR\u表\u NAME=“euw账户”
导出TF_VAR_ES_ENDPOINT=“euw elasticsearch”
导出TF\u VAR\u LOG\u LEVEL=“INFO”
}
模块“aws-eu-west-2”{
source=“git::https://”
出口TF_VAR_REGION=“eu-west-2”
导出TF\u VAR\u表\u NAME=“euw账户”
导出TF_VAR_ES_ENDPOINT=“euw elasticsearch”
导出TF\u VAR\u LOG\u LEVEL=“INFO”
}

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

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

TF_VAR_REGION=eu-central-1 terraform plan
这将创建区域变量,然后可以将该变量传递给模块:

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

谢谢@victor m,如果我想传递相同的变量(具有相同的值),我会这样做,但在我的例子中,我想传递不同的区域值、ES_端点和TABLE_名称到模块,这样我就可以一次性运行terraform,而不是多次导出变量。。有什么方法可以很容易做到吗?谢谢,这是我试过的,但是运气不好。我的脚本将包含:
TF_VAR_env=$ENVIRONMENT aws vault exec$2--terraform$1 TF_VAR_eucentral1=eu-central-1 aws vault exec$2--terraform$1
,并将其添加到terraform
模块“awsviz-eu-central-1”{source=“git::https://”REGION=“{VAR.eucentral1}”表名=“{VAR.env}-config accounts”ES\u ENDPOINT=”{var.env}-aws elasticsearch}module“awsviz-eu-west-1”{source=“git::https://”REGION=“{var.euwest1}”TABLE_NAME=“{var.env}-config accounts”ES_ENDPOINT=“{var.env}-aws elasticsearch”}