title: Terraform assign default to local variable
body: <p>I have the following code in my variables.tfvars:</p>
  <pre><code>variable &quot;is_premium_service_bus_required&quot; {
    description = &quot;When set to 1, premium service bus will be created&quot;
    type        = &quot;string&quot;
    default     = &quot;0&quot;
  }
  
  variable &quot;service_bus_type_cloud&quot; {
    description = &quot;Type of service bus to be created, value can only be standard or premium&quot;
    type        = &quot;string&quot;
  }
  </code></pre>
  <p>Following code to create the azure resource:</p>
  <pre><code>resource &quot;azurerm_servicebus_namespace&quot; &quot;premium-ffdac-cloud-servicebus&quot; {
    count               = &quot;${var.is_premium_service_bus_required}&quot;
    name                = &quot;${terraform.workspace}-ffdac-cloud-sbus&quot;
    location            = &quot;west europe&quot;
    resource_group_name = &quot;fusson&quot;
    sku                 = &quot;Premium&quot;
    capacity            = &quot;1&quot;   
  }
  
  resource &quot;azurerm_servicebus_namespace&quot; &quot;ffdac-cloud-servicebus&quot; {
    name                = &quot;${terraform.workspace}-ffdac-cloud-servicebus&quot;
    location            = &quot;west europe&quot;
    resource_group_name = &quot;fusson&quot;
    sku                 = &quot;Standard&quot;
  }
  </code></pre>
  <p>As it can be seen, we manipulate the creation of a resource on Azure by setting the variable value: is_premium_service_bus_required either to 0(not created) or 1 (gets created)</p>
  <p>And I have the following local variables:</p>
  <pre><code>locals {
    standard_service_bus_cloud_connection_string = &quot;${azurerm_servicebus_namespace.ffdac-cloud-servicebus.default_primary_connection_string};TransportType=Amqp&quot;
  }
  
  
  locals {
    premium_service_bus_cloud_connection_string = &quot;${azurerm_servicebus_namespace.premium-ffdac-cloud-servicebus.default_primary_connection_string};TransportType=Amqp&quot;
  }
  </code></pre>
  <p>which simply is responsible for manipulating the default connection string and adding the protocol at the end.</p>
  <p>and we have the following code which determines based on the value of &quot;service_bus_type_cloud&quot;, what value should be assigned to &quot;service_bus_cloud_connection_string&quot; variable which later is used in a different terraform file.</p>
  <pre><code>locals {
    service_bus_type_cloud_names = {
      &quot;premium&quot;  = &quot;${local.premium_service_bus_cloud_connection_string}&quot;
      &quot;standard&quot; = &quot;${local.standard_service_bus_cloud_connection_string}&quot;
    }
  
    cloud_connection_string_default = &quot;${local.standard_service_bus_cloud_connection_string}&quot;
  
    service_bus_cloud_connection_string = &quot;${lookup(local.service_bus_type_cloud_names, var.service_bus_type_cloud, local.cloud_connection_string_default)}&quot;
  }
  </code></pre>
  <p>The problem now is, when the value of &quot;is_premium_service_bus_required&quot; is set to &quot;0&quot;, premium service bus azure service is not created and the local variable &quot;premium_service_bus_cloud_connection_string&quot; value is empty, and we end up getting the following error:</p>
  <pre><code>Error: Error running plan: 1 errors occurred:
      * local.premium_service_bus_cloud_connection_string: local.premium_service_bus_cloud_connection_string: Resource 'azurerm_servicebus_namespace.premium-ffdac-cloud-servicebus' not found for variable 'azurerm_servicebus_namespace.premium-ffdac-cloud-servicebus.default_primary_connection_string'
  </code></pre>
  <p>Is there a way we circumvent this error or a better way to rewrite the logic? We are running Terraform version 0.11, hence conditional operator mentioned <a href="https://www.hashicorp.com/blog/terraform-0-12-conditional-operator-improvements/" rel="nofollow noreferrer">here</a> cannot be used as using the following code:</p>
  <pre><code>locals {
    service_bus_cloud_connection_string = &quot;${var.service_bus_type_cloud == &quot;premium&quot; ? &quot;${azurerm_servicebus_namespace.premium-ffdac-cloud-servicebus.default_primary_connection_string};TransportType=Amqp&quot; : &quot;${azurerm_servicebus_namespace.ffdac-cloud-servicebus.default_primary_connection_string};TransportType=Amqp&quot;}&quot;
  }
  </code></pre>
  <p>will evaulate both value expressions even though only one is ever returned and it seems to be a bug that was fixed in 0.12 as seen <a href="https://github.com/hashicorp/terraform/issues/15605" rel="nofollow noreferrer">here</a></p>