title: How can I use Terraform to create a service principal and use that principal in a provider?
body: <p>I have read the write-ups online but they dont seem to cover this topic completely and was hoping someone who has done it may have some direction for me.
  We are setting up a complicated Terraform template to satisfy our IaC requirements relating to our SaaS offering. In doing so we want the template to use the user's credentials at launch to create a new service principal in Azure AD (This part I have no problem doing). Then in the next portion of the template we are using that service principal as the provider. Problem is that it throws errors in the plan/apply because the service principal doesnt exist (aka the id is non existent due to the service provider section not running yet).
  So is there a way that I can do this? Create a service principal and then us it in a provider alias that uses that service principal without splitting this into multiple templates?</p>
  
  <p>In the end I want this template to create the service provider using the local user's permissions or MSI, give it RBAC to a subscription, then use that service provider to create assets in that subscription.</p>
  
  <p>main.ts (root)</p>
  
  <pre><code>provider "azurerm" {
      alias               = "ActiveDirectory"
      subscription_id     = "${var.subscriptionNucleus}"
  }
  
  provider "azurerm" {
      alias               = "Infrastructure"
      subscription_id     = "${var.subscriptionInfrastructure}"
  }
  module "activedirectory" {
      providers                       = { azurerm = "azurerm.ActiveDirectory" 
  }
      source                          = "./modules/activedirectory"
      subscription_id_infrastructure  = "${var.subscriptionInfrastructure}"
  }
  module "infrastructure" {
      providers                   = { azurerm = "azurerm.Infrastructure"}
      source                      = "./modules/infrastructure"
      location                    = "${var.location}"
      application_id              = 
   "${module.activedirectory.service_principal_application_id}"
      subscription_id             = "${var.subscriptionInfrastructure}"
      prefix                      = "${var.prefix}"
  }
  </code></pre>
  
  <p>main.ts (./modules/infrastructure)</p>
  
  <pre><code>data "azurerm_azuread_service_principal" "serviceprincipal" {
      application_id = "${var.application_id}"
  }
  
  provider "azurerm" {
      alias           = "InfrastructureSP"
      subscription_id = "${var.subscription_id}"
      client_id       = "${var.application_id}"
      client_secret   = "secret"
      tenant_id       = 
  "${data.azurerm_client_config.clientconfig.tenant_id}"  
  }
  </code></pre>