0

I try to deploy a backend and frontend as container apps in azure with terraform.
The frontend needs to have the URL of the backend as environment variable.

Is this possible in one go?

Like for example with docker-compose where the internal hostname of a container is predetermined or a service in kubernetes.

The only thing I can think of right now is to apply the plan, wait for the container apps to get created and receive a hostname and then afterwards change my .tf file and apply again. But there has to be a better solution.

Marty
  • 974
  • 9
  • 25

3 Answers3

6

As mentioned by De_The_Mi you can reference the url of the latest revision deployed using:

azurerm_container_app.<your-backend-object-name>.latest_revision_fqdn

However that changes with every deployment, if you want the "Application Url" as it's called in the Portal you can use the following as mentioned in the Github issue (https://github.com/hashicorp/terraform-provider-azurerm/issues/20696)

azurerm_container_app.<your-backend-object-name>.ingress[0].fqdn
Fran Hoey
  • 61
  • 2
1

In terraform you can set the property depends_on = [] to each resource to define dependencies that are not clear from code side. But in your case, you should be able to reference the fqdn of the backend using the variable of the backend-container-app: latest_revision_fqdn

The reference should be something like:

azurerm_container_app.<your-backend-object-name>.latest_revision_fqdn

By using this variable in the frontend you don't need to define any dependencies on your own because terraform will know them.

De_The_Mi
  • 43
  • 4
1

If you created your container app using azapi provider like below

resource "azapi_resource" "containerapp" {
  type      = "Microsoft.App/containerapps@2022-03-01"
  name      = var.aca_name
  parent_id = azurerm_resource_group.rg.id
  location  = azurerm_resource_group.rg.location
 
 
  identity {
    type         = "SystemAssigned, UserAssigned"
    identity_ids = [azurerm_user_assigned_identity.containerapp.id]
  }
  body = jsonencode({
 
    properties = {
      managedEnvironmentId = azapi_resource.containerapp_environment.id
      configuration = {
        ingress = {
          external : true,
          targetPort : 80
        },
        "registries" : [
          {
            "server" : data.azurerm_container_registry.acr.login_server,
            "identity" : azurerm_user_assigned_identity.containerapp.id
          }
        ]
      }
      template = {
        containers = [
          {
            image = "${data.azurerm_container_registry.acr.login_server}/aspcoresample:76ef8d9511d310649729a28563fdf6d133338e30",
            name  = "firstcontainerappacracr"
            resources = {
              cpu    = 0.25
              memory = "0.5Gi"
            },
            "probes" : [
              {
                "type" : "Liveness",
                "httpGet" : {
                  "path" : "/",
                  "port" : 80,
                  "scheme" : "HTTP"
                },
                "periodSeconds" : 10
              },
              {
                "type" : "Readiness",
                "httpGet" : {
                  "path" : "/",
                  "port" : 80,
                  "scheme" : "HTTP"
                },
                "periodSeconds" : 10
              },
              {
                "type" : "Startup",
                "httpGet" : {
                  "path" : "/",
                  "port" : 80,
                  "scheme" : "HTTP"
                },
                "periodSeconds" : 10
              }
            ]
          }
        ]
        scale = {
          minReplicas = 0,
          maxReplicas = 2
        }
      }
    }
 
  })
  ignore_missing_property = true
  depends_on = [
    azapi_resource.containerapp_environment
  ]
}

then you have to add response_export_values = ["properties.configuration.ingress.fqdn"] at the root level of the resource.

So next you can fetch fqdn

locals {
  containerapp_fqdn = jsondecode(azapi_resource.containerapp.output).properties.configuration.ingress.fqdn
}
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107