Mount Azure File Share to Azure App Service or Azure Functions Linux Container Apps

Raymond Raymond event 2023-12-26 visibility 315
more_vert

Code description

This code snippet shows you how to mount Azure Storage file share as a local drive in a Linux container app hosted in Azure App Service or Azure Functions.

Outline of the code

The code snippet does the following:

  • Create a storage account

  • Create file service

  • Create a file share

  • Create an App Service resource with Linux container

  • Mount the file share to the app

For .NET 8 containers

If you use .NET 8 images that runs without root permissions, please mount the file share to path under /home. Otherwise, you may encounter errors like the following when deploying to Azure:

"message":"The resource write operation failed to complete successfully, because it reached terminal provisioning state 'Failed'.","details":[{"code":"DeploymentFailed","target":"/subscriptions/***/resourceGroups/***/providers/Microsoft.Resources/deployments/kontext-web","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.","details":[{"code":"InternalServerError","message":"There was an unexpected InternalServerError. Please try again later. x-ms-correlation-request-id: ***"}

Code snippet

param location string = 'australiaeast'
param storageAccountName string = 'mystorageaccount'

resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  kind: 'StorageV2'
  location: location
  name: storageAccountName
  properties: {
    accessTier: 'Cool'
    allowBlobPublicAccess: true
    encryption: {
      keySource: 'Microsoft.Storage'
      services: {
        blob: {
          enabled: true
          keyType: 'Account'
        }
        file: {
          enabled: true
          keyType: 'Account'
        }
      }
    }
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
  }
  sku: {
    name: 'Standard_LRS'
  }
}

// File share for sharing data across services.
resource storageAccountFileService 'Microsoft.Storage/storageAccounts/fileServices@2023-01-01' = {
  parent: storageAccount
  name: 'default'
}

resource storageAccountFileShareMain 'Microsoft.Storage/storageAccounts/fileServices/shares@2023-01-01' = {
  parent: storageAccountFileService
  name: 'fs-main'
  properties: {
    shareQuota: 5 // Set the quota for the file share in GiB. Adjust as needed.
  }
}


resource appServicePlan 'Microsoft.Web/serverfarms@2023-01-01' = {
  name: 'myAsp'
  location: location
  sku: {
    name: 'B1'
    tier: 'Basic'
    size: 'B1'
    family: 'B1'
    capacity: 1
  }
  properties: {
    reserved: true // This is for Linux
  }
}

resource myApp 'Microsoft.Web/sites@2023-01-01' = {
  name: 'myApp'
  location: location
  // For function app, change kind to functionapp,linux
  kind: 'linux'
  properties: {
    serverFarmId: appServicePlan.id
    siteConfig: {
      linuxFxVersion: 'Docker|***:latest'
      appSettings: [
      ]
      connectionStrings: [
      ]
      azureStorageAccounts: {
        myFileShare: {
          type: 'AzureFiles'
          accountName: storageAccount.name
          shareName: storageAccountFileShareMain.name
          accessKey: storageAccount.listKeys().keys[0].value
          mountPath: '/home/fs-main'
        }
      }
    }
    httpsOnly: true
  }
}
More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts