darato
darato

Reputation: 95

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 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_subnets equals [var.main_cidr], if the variable is set, public_subnets 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 :)

Upvotes: 0

Views: 1346

Answers (1)

Ervin Szilagyi
Ervin Szilagyi

Reputation: 16775

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.

Upvotes: 2

Related Questions