简体   繁体   中英

Is there an equivalent to omit in Terraform?

I'm working on my Terraform code, and I would like to use the same code whether or a variable is set or equal "" . But if I pass the variable with "" in the aws_vpc module, there is an exception raised, saying a valid cidr is required.

If it were Ansible, I'd use the | omit | omit filter which omit the parameter and don't pass it to the module if the variable is not set.

There is the code.

module "server_vpc" {
  source = "terraform-aws-modules/vpc/aws"

  name = "servers_vpc"
  cidr = var.main_cidr
  secondary_cidr = [var.secondary_cidr]

  azs             = var.azs
  public_subnets  = local.public_subnets
  private_subnets = var.private_subnets

  enable_nat_gateway = true
  create_igw         = true
}

On this code, if the variable is equal to "" (default), then public_su.nets equals [var.main_cidr] , if the variable is set, public_su.nets equals [var.main_cidr, var.secondary_cidr] .

But the part that didn't work like I'd like is the line with secondary_cidr = [var.secondary_cidr] .
When the variable contains a valid cidr, it works, but when the variable is not set and equals to the default "" , Terraform raise an error from the aws vpc module explaining that a valid cidr is needed.

So I would like a way to create this vpc with the module, but not set the parameter secondary_cidr if the variable equals "" .

I hope I was clear:)

According to the documentation for this specific module secondary_cidr is not a required argument. In that case you don't have to provide an empty string. You either leave out secondary_cidr entirely or you assign null to it. Since secondary_cidr expects an array, most likely an empty array [] will work as well.

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