Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a field optional inside a terraform resource?

I am trying to create VMs for both cluster and single node deployments.

resource "azurerm_virtual_machine" "app" {
    name   = "${var.name_prefix}-${format("%02d", count.index+1)}"
    location = "${var.location}"
    resource_group_name = "${azurerm_resource_group.resourcegroup.name}"
    availability_set_id   = "${azurerm_availability_set.avset.id}"
    network_interface_ids = ["${element(azurerm_network_interface.networkinterface-app.*.id, count.index+1)}"]
    vm_size = "${var.vm_size}"
    count = "${var.app_nodes}"

The setup works well for the cluster node deployments with availability_set_id configured but fails as in single node I don't have a need to configure availability_set_id.

I am using Terraform v0.11.2 and * provider.azurerm: version = "~> 1.0" as versions of terraform and azurerm provider currently.

Can anyone help me how to use a conditional in this case to handle both scenarios?

like image 586
Rahul Avatar asked Oct 28 '22 20:10

Rahul


1 Answers

I think you need to wrap it in a module and then have two resources and use count = "${var.somecond ? 1 : 0}" do determine which one is active.

like image 82
matti Avatar answered Nov 15 '22 09:11

matti