Interpolation syntax using ternery operator.

79 views
Skip to first unread message

Steve Button

unread,
Mar 12, 2018, 9:29:00 AM3/12/18
to Terraform
Hi,

I have Terraform creating three VMs, and each of them have a rule in the interface which links back to the LB :-

load_balancer_inbound_nat_rules_ids

Which works fine for the SSH rules, using :-

    load_balancer_inbound_nat_rules_ids = ["${azurerm_lb_nat_rule.elastic_uk_ssh.*.id[count.index]}"]

However, I've got Kibana running on ONLY ONE of the nodes, so I'm trying to add a LB inbound nat rule for that one using :-

    load_balancer_inbound_nat_rules_ids = ["${azurerm_lb_nat_rule.elastic_uk_ssh.*.id[count.index]}", "${count.index == 0 ? azurerm_lb_nat_rule.elastic_uk_kibana.id : false}" ]

which has a conditional operator, to only put the rule ID in if it's instance 0. 

However this doesn't work, as it doesn't like "false" or "null" or "var.null" after the : in the conditional. 

I'm sure this will be really obvious to someone (just not me). Can anyone help?

Here's a bigger snippet. 

resource "azurerm_lb_nat_rule" "elastic_uk_kibana" {
  resource_group_name            = "${azurerm_resource_group.elastic_uk.name}"
  loadbalancer_id                = "${azurerm_lb.elastic_uk.id}"
  name                           = "uks-elastic-kibana"
  protocol                       = "Tcp"
  frontend_port                  = 443
  backend_port                   = 5601
  frontend_ip_configuration_name = "uks-elastic-lb-ipconfig"
}

resource "azurerm_lb_nat_rule" "elastic_uk_ssh" {
  count                          = "${var.instances}"
  resource_group_name            = "${azurerm_resource_group.elastic_uk.name}"
  loadbalancer_id                = "${azurerm_lb.elastic_uk.id}"
  name                           = "uks-elastic-ssh-${count.index}"
  protocol                       = "Tcp"
  frontend_port                  = "${22000 + count.index}"
  backend_port                   = 22
  frontend_ip_configuration_name = "uks-elastic-lb-ipconfig"
}

#################################### COMPUTE ##########################################

resource "azurerm_network_interface" "elastic_uk" {
  count               = "${var.instances}"
  name                = "uks-elastic-nic-${count.index}"
  location            = "${azurerm_resource_group.elastic_uk.location}"
  resource_group_name = "${azurerm_resource_group.elastic_uk.name}"

  ip_configuration {
    name                                = "uks-elastic-ipconfig-${count.index}"
    subnet_id                           = "${azurerm_subnet.elastic_uk.id}"
    private_ip_address                  = "${cidrhost("10.x.x.0/24", 10 + count.index)}"
    private_ip_address_allocation       = "static"
    load_balancer_inbound_nat_rules_ids = ["${azurerm_lb_nat_rule.elastic_uk_ssh.*.id[count.index]}", "${count.index == 0 ? azurerm_lb_nat_rule.elastic_uk_kibana.id : false}" ]
    # TODO: Also need a rule for https/443 but only for the first machine.
  }
}

Oh, and it works just fine without this but the "target virtual machine" and "network IP configuration" and just missing and have to be put in manually. 

Andrew Hodgson

unread,
Mar 12, 2018, 9:47:02 AM3/12/18
to terrafo...@googlegroups.com

Hi,

 

I would be interested to see what people suggest here.  I had a similar issue a while ago where I wanted a list to have extra options in specific environments using the conditionals, and ended up having to copy the whole list again with the extra options for the conditional which is not efficient.

 

Thanks,

Andrew.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/28e145d4-7fde-4904-b4ad-ff6690bede22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages