Home > Blockchain >  Pass one resource's variable to another
Pass one resource's variable to another

Time:09-17

I am creating an Azure App Service resource and an App Registration resource (and app service and others that are not relevant to this question as they work fine) via Terraform.

resource "azurerm_app_service" "app" {
  name                = var.app_service_name
  location            = var.resource_group_location
  resource_group_name = azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.plan-app.id

  app_settings = {
    "AzureAd:ClientId" = azuread_application.appregistration.application_id
  }

  site_config {
    ftps_state = var.app_service_ftps_state
  }
}

resource "azuread_application" "appregistration" {
  display_name                   = azurerm_app_service.app.name
  owners                         = [data.azuread_client_config.current.object_id]
  sign_in_audience               = "AzureADMyOrg"
  fallback_public_client_enabled = true

  web {
    homepage_url  = var.appreg_web_homepage_url
    logout_url    = var.appreg_web_logout_url
    redirect_uris = [var.appreg_web_homepage_url, var.appreg_web_redirect_uri]

    implicit_grant {
      access_token_issuance_enabled = true
      id_token_issuance_enabled     = true
    }
  }
}

output "appreg_application_id" {
  value = azuread_application.appregistration.application_id
}

I need to add the App Registration client / application id to the app_settings block in the app service resource.

The error I get with the above configuration is:

{"@level":"error","@message":"Error: Cycle: azuread_application.appregistration, azurerm_app_service.app","@module":"terraform.ui","@timestamp":"2021-09-15T10:54:31.753401Z","diagnostic":{"severity":"error","summary":"Cycle: azuread_application.appregistration, azurerm_app_service.app","detail":""},"type":"diagnostic"}

Note that the output variable displays the application id correctly.

CodePudding user response:

You have a cycle error because you have both resources referencing each other. Terraform builds a directed acyclical graph to work out which order to create (or destroy) resources in with the information from one resource or data source flowing into another normally determining this order.

In your case your azuread_application.appregistration resource is referencing the azurerm_app_service.app.name parameter while the azurerm_app_service.app resource needs the azuread_application.appregistration.application_id attribute.

I don't know a ton about Azure but to me that seems like the azurerm_app_service resource needs to be created ahead of the azuread_application resource and so I'd expect the link to be in that direction.

Because you are already setting the azurerm_app_service.app.name parameter to var.app_service_name then you can just directly pass var.app_service_name to azuread_application.appregistration.display_name to achieve the same result but to break the cycle error.

resource "azurerm_app_service" "app" {
  name                = var.app_service_name
  location            = var.resource_group_location
  resource_group_name = azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.plan-app.id

  app_settings = {
    "AzureAd:ClientId" = azuread_application.appregistration.application_id
  }

  site_config {
    ftps_state = var.app_service_ftps_state
  }
}

resource "azuread_application" "appregistration" {
  display_name                   = var.app_service_name
  owners                         = [data.azuread_client_config.current.object_id]
  sign_in_audience               = "AzureADMyOrg"
  fallback_public_client_enabled = true

  web {
    homepage_url  = var.appreg_web_homepage_url
    logout_url    = var.appreg_web_logout_url
    redirect_uris = [var.appreg_web_homepage_url, var.appreg_web_redirect_uri]

    implicit_grant {
      access_token_issuance_enabled = true
      id_token_issuance_enabled     = true
    }
  }
}

output "appreg_application_id" {
  value = azuread_application.appregistration.application_id
}
  • Related