Version 0.11.13 - How to handle optional attribute in resources

I noticed in old versions of terraform, don’t have “null” type to omit optional attribute. Is there any workaround to omit optional attribute for older versions where “null” type is not supported.

In my specific case, I am trying to create route in AWS Route table, I have 2 optional attribute in resource nat_gateway_id or transit_gateway_id. only one can be added at a time.

 variable "tgw" {
   default = true
 }

resource "aws_route" "r" {
  route_table_id              = "rtb-"
  destination_cidr_block = "0.0.0.0/0"
  nat_gateway_id = "${var.tgw ? "" : aws_nat_gateway.nat_gateway.id}"
  transit_gateway_id = "${var.tgw ? var.tgw_id : "" }"
}

it’s failing currently with error,

Error: more than 1 target specified. Only 1 of gateway_id, egress_only_gateway_id, nat_gateway_id, instance_id, network_interf****_id or vpc_peering_connection_id is allowed.

Is there any way to get this working?
Creating 2 resources separately and add count attribute based on condition is last option for me. Wants to see if there is any way to get this working on 1 resource.

Thanks,

Hi @meet101,

Unfortunately having two mutually-exclusive conditional resource blocks is the only option for Terraform 0.11. The conditional null syntax you are referring to was added in Terraform 0.12 as the solution to that limitation.

If i create 2 resources conditionally, i have noticed update fails sometime as terraform try to create a new resource before deleting old resource. is there a way i can add dependency in a way to have terraform delete resource A before try to create resource B?

@apparentlymart - is there any way to manage dependency in update workflow. resource A should be deleted before resource B. can we create that dependency explicitly? My update is failing because it try to create a route before it delete the existing route.