简体   繁体   中英

how to create variable as soon as if condition is met in terraform

I have defined 3 different sizes in variable.tf as below

variable size {
  description = "size of the disk"
  type = number
}

variable size2 {
  description = "size of the disk "
  type = number
}

variable size3 {
  description = "size of the disk "
  type = number
}
variable drives {
  description = "drive value (max=3)"
  type = number
}

main.tf

drives=var.drives
size=var.size 
size2=var.drives==2 ? var.size2:null
size3 =var.drives==3 ? var.size3:null

Therefore, I'm attempting to create or take input for size2 and size3 variable only when the values of the cloud drives are 2 and 3, respectively. But even though I have set the cloud drivers variable to 1, the terraform plan command still asks for all three different sizes as input.Since I'm new to Terraform World, could you kindly assist me with this?

terraform plan command still asks for all three different sizes

Because you did not provide any default values. Thus you have to provide all values for all these parameters. Just define some defaults:

variable size2 {
  description = "size of the disk "
  type = number
  default = 0
}

variable size3 {
  description = "size of the disk "
  type = number
  default = 0
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM