Terraform choosing between different providers based on condition

Viewed 3052

I'm trying to use Terraform to automate the deployment of an environment, but there's something tricky with the setup:

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 Dyn DNS or Azure DNS (and they're mutually exclusive)

And, for the same reason, it wouldn't make sense for me to, for example, initialize the Terraform Dyn provider when I want to provision in Azure.

What I thought would work is to include the two providers:

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
}

And then, when running tf plan, 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 here, I thought I can provision resources conditionally:

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
}

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 Dyn provider for an environment, with the existing logic, I would still need to pass correct values (and not empty strings) to its required variables.

Is there any way to get around this? Maybe something along the lines of "only using a provider if I actually need it"

0 Answers
Related