Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount a shared volume onto an Azure Container App instance using terraform

Documentation seems to be sorely lacking when it comes to Container Apps, in particular - with Terraform. I've so far managed to successfully create my container app as well as storage and that all works fine. but trying to mount the shared volume onto the container apps it is a struggle...below is a snippet of the code

storage and share creation

resource "azurerm_storage_account" "storage" {
  name                     = "storage1"
  resource_group_name      = azurerm_resource_group.rg.name
  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_storage_share" "share" {
  name                 = "storage1_share"
  quota                = "200"
  storage_account_name = azurerm_storage_account.storage.name
  depends_on           = [azurerm_storage_account.storage]
}

Container App snippet that is problematic...

  template {
    container {
      name   = var.name_prefix
      image  = "${data.azurerm_container_registry.acr.login_server}/data_svc:latest"
      cpu    = 0.25
      memory = "0.5Gi"

      volume_mounts {
        name = "${azurerm_storage_account.storage.name}-volume"
        path = "/${azurerm_storage_share.share.name}"
      }
    }

    volume {
      name         = "${azurerm_storage_account.storage.name}-volume"
      storage_type = "AzureFile"
      storage_name = azurerm_storage_account.storage.name
    }
  }

When i runn the apply, i get the following

│ Container App Name: "data-svc-app"): performing CreateOrUpdate: containerapps.ContainerAppsClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error:
│ Code="ManagedEnvironmentStorageNotFound" Message="Storage storage1 not found under managed environment"

Im thinking either i am missing a step or some values are not correct - the problem is due to lack of documentation, it's a trial-and-error process. Does anyone know the proper configuration to get this working in terraform? Thinking i need to add a configuration maybe for the Container App Environment...again though...documentation or examples are sparce on how.

like image 355
mike01010 Avatar asked Sep 21 '25 04:09

mike01010


1 Answers

Once you have resource for storage account you should create storage resource for container apps

esource "azapi_resource" "storage" {
  schema_validation_enabled = false
  type                      = "Microsoft.App/managedEnvironments/storages@2022-10-01"
  name                      = "shared-storage"
  parent_id                 = local.containerapp_environment_id
  body = jsonencode({
    properties = {
      azureFile = {
        accountKey  = azurerm_storage_account. storage.primary_access_key
        accountName = azurerm_storage_account. storage.name
        shareName   = azurerm_storage_share.share.name
        AccessMode  = "ReadWrite"
      }
    }
  })
}

And then

  template {
    container {
      name   = var.name_prefix
      image  = "${data.azurerm_container_registry.acr.login_server}/data_svc:latest"
      cpu    = 0.25
      memory = "0.5Gi"

      volume_mounts {
        name =  azurerm_storage_share.share.name

        path = "/${azurerm_storage_share.share.name}"
      }
    }

    volume {
      name         =  azurerm_storage_share.share.name
      storage_type = "AzureFile"
      storage_name = azapi_resource.storage.name
    }
  }

And if you don't use azapi provider you should use azurerm_container_app_environment_storage

resource "azurerm_container_app_environment_storage" "example" {
  name                         = "mycontainerappstorage"
  container_app_environment_id = azurerm_container_app_environment.example.id
  account_name                 = azurerm_storage_account.example.name
  share_name                   = azurerm_storage_share.example.name
  access_key                   = azurerm_storage_account.example.primary_access_key
  access_mode                  = "ReadOnly"
}
like image 113
Krzysztof Madej Avatar answered Sep 22 '25 19:09

Krzysztof Madej