Search code examples
azureterraformterraform-provider-azureterraform0.12+

terraform conditional blocks with null inputs


I have this terraform code in a resource

administrator_login = try(var.mode, "Default") == "Default" ? try(var.administrator_username, "testuser") : null

when var mode is "Default" the first part of the condition should be returning true and when administrator_username is null, it should return testuser, instead its returning null, any ideas why that could be happening?

it should return testuser when var.administrator_username is null and var.mode is "Default"


Solution

  • var.administrator_username equal to null is still valid. It does not produce any errors, so null is returned. its better to use coalesce instead of try:

    administrator_login = coalesce(var.mode, "Default") == "Default" ? coalesce(var.administrator_username, "testuser") : null