title: Terraform choosing between different providers based on condition
body: <p>I'm trying to use Terraform to automate the deployment of an environment, but there's something tricky with the setup:</p>
  
  <p>I need to provision a DNS CNAME record, but my problem is that, based on the environment, I would need to provision that record either in <a href="https://dyn.com/dns/" rel="nofollow noreferrer">Dyn DNS</a> or Azure DNS (and they're mutually exclusive)</p>
  
  <p>And, for the same reason, it wouldn't make sense for me to, for example, initialize the Terraform <code>Dyn</code> provider when I want to provision in Azure.</p>
  
  <p>What I thought would work is to include the two <code>providers</code>:</p>
  
  <pre><code>provider azurerm {
    version = "=1.44.0"
  
    client_id       = var.dns_provider_client_id
    client_secret   = var.dns_provider_client_secret
    subscription_id = var.dns_provider_subscription_id
    tenant_id       = var.dns_provider_tenant_id
  }
  
  provider dyn {
    version = "=1.2.0"
  
    customer_name = var.dyn_customer_name
    password      = var.dyn_password
    username      = var.dyn_username
  }
  </code></pre>
  
  <p>And then, when running <code>tf plan</code>, I would set the variables related to the provider I'm not using to empty string, and then, when provisioning the record, with the help from <a href="https://github.com/hashicorp/terraform/issues/15281#issuecomment-308275154" rel="nofollow noreferrer">here</a>, I thought I can provision resources conditionally:</p>
  
  <pre><code>resource azurerm_dns_cname_record dns_name {
    count = var.dyn_dns_zone.name == "" ? 1 : 0
  
    name                = "..."
    record              = "..."
    resource_group_name = "..."
    ttl                 = 60
    zone_name           = "..."
  }
  
  resource dyn_record dyn {
    count = var.dyn_dns_zone.name == "" ? 0 : 1
  
    zone  = "..."
    name  = "..."
    value = "..."
    type  = "..."
    ttl   = 60
  }
  </code></pre>
  
  <p>But my problem is that no matter what record I'm creating I need to initialize both providers, so even though I wouldn't need the <code>Dyn</code> provider for an environment, with the existing logic, I would still need to pass correct values (and not empty strings) to its required variables.</p>
  
  <p>Is there any way to get around this? Maybe something along the lines of "only using a provider if I actually need it"</p>