azureterraformelementazure-availability-zones

How to use Element for Azure Availability Zones in Terraform


I'm trying to set the Azure Availability Zone(A.A.Z) using the Terraform Element Function but I'm getting the following error twice. Since there are 3 A.A.Z. I'd like to use Element for this as it would work really well, if i could figure out the syntax or where i'm going wrong that is. Element will just keep cycling through the same number set which i want as my module may request 3 servers or 300. Below the code is the error from main.tf: I'm getting the error twice.

    zones = "${var.avzones}" ? "${element(["1", "2", "3"], "${count.index + 1}")}" : ""
Error: Incorrect attribute value type

  on ..\main.tf line 283, in resource "azurerm_virtual_machine" "vm":
  283:   zones                         = "${var.avzones}" ? "${element(["1", "2", "3"], "${count.index + 1}")}" : ""

Inappropriate value for attribute "zones": list of string required.

Any ideas would be greatly appreciated --if i find an answer I'll come back and post--

Cheers, -Sam Kachar


Solution

  • I found an answer.

    Martin your correct, that's what the error message was saying and getting the list of strings to the zones property was turning out to be a real PITA. I tried every variation of syntax. I tried tolist function. Nothing. What ended up working though was the Split function. Here is what my code looks like now:

    zones = "${var.avzones}" ? split("","${element(["1","2","3"], "${count.index}")}") : null
    

    Martin the other comment you made about me sending null instead of "" makes more sense and that was what i chose to do. I haven't tested it yet. I've only thus far validated the zones. I couldn't believe when split worked, it may be a bit hackish, but it got the job done. I validated that it did in fact build all of the zones just as i want it to. 1 through 3 every time. If the null doesn't work out I'll update my answer...but for now; anyone struggling with Azure Availability Zones and getting Terraform to recognize the values 1 through 3 use the code i have posted above. It works!