通过环境变量文件 .tfvars 在 Terraform 中 variables.tf 中提到的变量的值 [英] Values to variables mentioned in variables.tf in Terraform through environment variables file .tfvars

查看:16
本文介绍了通过环境变量文件 .tfvars 在 Terraform 中 variables.tf 中提到的变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何传递 variable.tf 中提到的 "list" 或 "map" 类型的变量的值.下面提到的 input.tfvars 文件中的语法是否有错误?

How do I pass the values of variables mentioned in variable.tf of type "list" or "map" . Are there any mistakes in the syntax in the input.tfvars file mentioned below?

目标不是硬编码 variables.tfmain.tf 文件中的任何值.在 Terraform.io 文档中,我验证了可以以这种格式提供值.

The goal is not to hard code any values in the variables.tf or the main.tf file. In the Terraform.io docs I verified that the values can be provided in this format.

这是来自网站:
列表可以显式或隐式定义:

This is from the site:
Lists are defined either explicitly or implicitly:

 # implicitly by using brackets [...]
 variable "cidrs" { default = [] }

 # explicitly
 variable "cidrs" { type = "list" }

您可以在 terraform.tfvars 文件中指定列表:

You can specify lists in a the terraform.tfvars file:

 cidrs = [ "10.0.0.0/16", "10.1.0.0/16" ]

现在,当我尝试这样做时

Now, when I try to do

terraform plan -var-file=input.tfvars 

无法从 tfvars 文件中读取变量值并出现以下错误:

it is unable to read the variable values from the tfvars file and presents the following error:

错误:模块根目录:
模块 vpc:未设置所需变量vpccidr"
模块 vpc:未设置所需变量vpcname"

Error: module root:
module vpc: required variable "vpccidr" not set
module vpc: required variable "vpcname" not set

variables.tf 文件如下所示:

variable "vpccidr" { type = "list"}
variable "vpcname" { type = "list" }

input.tfvars 文件如下所示:

vpccidr=[ "10.1.0.0/16", "10.2.0.0/16", "10.3.0.0/16" ]
vpcname=[ "vpc1", "vpc2", "vpc3" ]

main.tf 文件如下所示:

module "vpc"{
  source = "modules/network/vpc"
}

modules/network下的VPC模块有如下配置文件main.tf和上面提到的variables.tf变量文件:

The VPC module under modules/network has the following configuration file main.tf and the variable file mentioned above in variables.tf:

resource "aws_vpc" "customVpc" {
  count = "${length(var.vpccidr)}"
  cidr_block = "${element(var.vpccidr, count.index)}"
  tags {
    count = "${length(var.vpcname)}"
    Name = "${element(var.vpcname, count.index)}"
  }
}          

推荐答案

您需要将变量传递给您的模块,即

You need to pass the variables to your module, i.e

module "vpc"{
  source = "modules/network/vpc"

  vpccidr = "${var.vpccidr}"
  vpcname = "${var.vpcname}"
}

这篇关于通过环境变量文件 .tfvars 在 Terraform 中 variables.tf 中提到的变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆