简体   繁体   中英

How to use Element for Azure Availability Zones in Terraform

I'm trying to set the Azure Availability Zone(AAZ) using the Terraform Element Function but I'm getting the following error twice. Since there are 3 AAZ 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

The error message is telling us that the zones argument expects a list of strings, but the expression given here seems to return a single string.

It looks like your goal is to select just a single zone from the list, in which case the two "arms" of the conditional expression should return a single-element list and an empty list respectively:

    zones = var.avzones ? [element(["1", "2", "3"], count.index + 1)] : []

The above assumes that it's valid for there to be zero zones specified. If not, you might need to use null instead of [] to leave the zones argument unspecified and accept a default selected by the remote system, or you might need to select a suitable default value yourself.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM