Mengotomatiskan penyebaran sumber daya untuk aplikasi fungsi Anda di Azure Functions

Anda dapat menggunakan file Bicep atau templat Azure Resource Manager untuk menyebarkan aplikasi fungsi. Artikel ini menguraikan sumber daya dan parameter yang diperlukan untuk melakukannya. Anda mungkin perlu menyebarkan sumber daya lainnya, bergantung pada pemicu dan pengikatan di aplikasi fungsi Anda. Untuk informasi selengkapnya mengenai pembuatan file Bicep, lihat Memahami struktur dan sintaks file Bicep. Untuk informasi selengkapnya tentang membuat templat, lihat Menulis templat Azure Resource Manager.

Untuk contoh file Bicep dan templat ARM, lihat:

Sumber daya yang diperlukan

Penyebaran Azure Functions biasanya terdiri dari sumber daya ini:

Sumber daya Persyaratan Referensi sintaks dan properti
Aplikasi fungsi Diperlukan Microsoft.Web/sites
Akun penyimpanan Diperlukan Microsoft.Storage/storageAccounts
Komponen Application Insights Opsional Microsoft.Insights/components
Paket hosting Opsional1 Microsoft.Web/serverfarms

1 Paket hosting hanya diperlukan ketika Anda memilih untuk menjalankan aplikasi fungsi Anda pada paket Premium atau pada paket App Service.

Tip

Meskipun tidak diperlukan, sangat disarankan agar Anda mengonfigurasi Application Insights untuk aplikasi Anda.

Akun penyimpanan

Akun penyimpanan diperlukan untuk aplikasi fungsi. Anda memerlukan akun tujuan umum yang mendukung blob, tabel, antrean, dan file. Untuk informasi selengkapnya, lihat Persyaratan akun penyimpanan Azure Functions.

Penting

Akun penyimpanan digunakan untuk menyimpan data aplikasi penting, terkadang menyertakan kode aplikasi itu sendiri. Anda harus membatasi akses dari aplikasi dan pengguna lain ke akun penyimpanan.

resource storageAccountName 'Microsoft.Storage/storageAccounts@2022-05-01' = {
  name: storageAccountName
  location: location
  kind: 'StorageV2'
  sku: {
    name: storageAccountType
  }
  properties: {
    supportsHttpsTrafficOnly: true
    defaultToOAuthAuthentication: true
  }
}

Anda juga harus menentukan AzureWebJobsStorage koneksi dalam konfigurasi situs. Ini dapat diatur dalam appSettings koleksi di siteConfig objek :

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  ...
  properties: {
    ...
    siteConfig: {
      ...
      appSettings: [
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        ...
      ]
    }
  }
}

Dalam beberapa opsi paket hosting, aplikasi fungsi juga harus memiliki berbagi konten Azure Files, dan mereka akan memerlukan pengaturan aplikasi tambahan yang merujuk akun penyimpanan ini. Ini dibahas nanti dalam artikel ini sebagai bagian dari opsi paket hosting yang berlaku untuk ini.

Log penyimpanan

Karena akun penyimpanan digunakan untuk data aplikasi fungsi penting, Anda mungkin ingin memantau modifikasi konten tersebut. Untuk melakukan ini, Anda perlu mengonfigurasi log sumber daya Azure Monitor untuk Azure Storage. Dalam contoh berikut, ruang kerja Log Analytics bernama myLogAnalytics digunakan sebagai tujuan untuk log ini. Ruang kerja yang sama ini dapat digunakan untuk sumber daya Application Insights yang ditentukan nanti.

resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2021-09-01' existing = {
  name:'default'
  parent:storageAccountName
}

resource storageDataPlaneLogs 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
  name: '${storageAccountName}-logs'
  scope: blobService
  properties: {
    workspaceId: myLogAnalytics.id
    logs: [
      {
        category: 'StorageWrite'
        enabled: true
      }
    ]
    metrics: [
      {
        category: 'Transaction'
        enabled: true
      }
    ]
  }
}

Lihat Memantau Azure Storage untuk petunjuk tentang cara bekerja dengan log ini.

Application Insights

Application Insights disarankan untuk memantau aplikasi fungsi Anda. Sumber daya Application Insights didefinisikan dengan jenis Microsoft.Insights/components dan jenis web:

resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: applicationInsightsName
  location: appInsightsLocation
  kind: 'web'
  properties: {
    Application_Type: 'web'
    Request_Source: 'IbizaWebAppExtensionCreate'
  }
}

Selain itu, kunci instrumentasi perlu diberikan ke aplikasi fungsi menggunakan pengaturan aplikasi APPINSIGHTS_INSTRUMENTATIONKEY. Properti ini ditentukan dalam koleksi appSettings dalam objek siteConfig:

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  ...
  properties: {
    ...
    siteConfig: {
      ...
      appSettings: [
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: appInsights.properties.InstrumentationKey
        }
        ...
      ]
    }
  }
}

Paket hosting

Definisi paket hosting bervariasi dan dapat berupa salah satu paket berikut ini:

Aplikasi Fungsi

Sumber daya aplikasi fungsi didefinisikan dengan menggunakan sumber daya jenis Microsoft.Web/sites dan jenis functionapp:

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  identity:{
    type:'SystemAssigned'
  }
  properties: {
    serverFarmId: hostingPlan.id
    clientAffinityEnabled: false
    siteConfig: {
      alwaysOn: true
    }
    httpsOnly: true
  }
  dependsOn: [
    storageAccount
  ]
}

Penting

Jika Anda secara eksplisit mendefinisikan paket hosting, item tambahan akan diperlukan dalam array dependsOn: "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"

Aplikasi fungsi harus menyertakan pengaturan aplikasi berikut:

Nama pengaturan Deskripsi Contoh nilai
AzureWebJobsStorage String koneksi ke akun penyimpanan yang digunakan runtime Functions untuk antrean internal Lihat Akun Storage
FUNCTIONS_EXTENSION_VERSION Versi runtime Azure Functions ~4
FUNCTIONS_WORKER_RUNTIME Tumpukan bahasa yang akan digunakan untuk fungsi dalam aplikasi ini dotnet, node, java, python, atau powershell
WEBSITE_NODE_DEFAULT_VERSION Hanya diperlukan jika menggunakan tumpukan bahasa node pada Windows, menentukan versi yang akan digunakan ~14

Properti ini ditentukan dalam koleksi appSettings di properti siteConfig:

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  ...
  properties: {
    ...
    siteConfig: {
      ...
      appSettings: [
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${listKeys(storageAccountName, '2021-09-01').keys[0].value}'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
        {
          name: 'WEBSITE_NODE_DEFAULT_VERSION'
          value: '~14'
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        ...
      ]
    }
  }
}

Sebarkan pada paket Konsumsi

Paket Konsumsi secara otomatis mengalokasikan daya komputasi saat kode Anda berjalan, memperluas skala sesuai yang diperlukan untuk menangani beban, lalu menurunkan skala saat kode tidak berjalan. Anda tidak perlu membayar VM menganggur, dan Anda tidak perlu memesan kapasitas terlebih dahulu. Untuk mempelajari selengkapnya, lihat Skala dan hosting Azure Functions.

Untuk sampel file Bicep/templat Azure Resource Manager, lihat Aplikasi fungsi pada paket Konsumsi.

Membuat paket Konsumsi

Paket Konsumsi tidak perlu didefinisikan. Ketika tidak ditentukan, secara otomatis paket akan dibuat atau dipilih berdasarkan per wilayah saat Anda membuat sumber daya aplikasi fungsi tersebut.

Paket Pemakaian adalah jenis spesial sumber daya serverfarm. Anda bisa menentukannya dengan menggunakan nilai Dynamic untuk properti computeMode dan sku, sebagai berikut:

Windows

resource hostingPlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    name: 'Y1'
    tier: 'Dynamic'
    size: 'Y1'
    family: 'Y'
    capacity: 0
  }
  properties: {
    computeMode: 'Dynamic'
  }
}

Linux

Untuk menjalankan aplikasi di Linux, Anda juga harus mengatur "reserved": true untuk sumber daya serverfarms:

resource hostingPlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    name: 'Y1'
    tier: 'Dynamic'
    size: 'Y1'
    family: 'Y'
    capacity: 0
  }
  properties: {
    computeMode: 'Dynamic'
    reserved: true
  }
}

Membuat aplikasi fungsi

Ketika menentukan paket Pemakaian secara eksplisit, Anda harus mengatur properti serverFarmId di aplikasi sehingga mengarah ke ID sumber daya dari paket tersebut. Pastikan bahwa aplikasi fungsi memiliki pengaturan dependsOn yang juga mereferensikan paket tersebut.

Pengaturan yang diperlukan oleh aplikasi fungsi yang berjalan dalam paket Konsumsi berbeda antara Windows dan Linux.

Windows

Pada Windows, paket Pemakaian memerlukan dua pengaturan lainnya dalam konfigurasi situs: WEBSITE_CONTENTAZUREFILECONNECTIONSTRING dan WEBSITE_CONTENTSHARE. Properti ini mengonfigurasi akun penyimpanan tempat kode dan konfigurasi aplikasi fungsi disimpan.

Untuk sampel file Bicep/templat Azure Resource Manager, lihat Aplikasi Fungsi Azure yang Dihosting di Paket Konsumsi Windows.

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  properties: {
    serverFarmId: hostingPlan.id
    siteConfig: {
      appSettings: [
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsights.properties.InstrumentationKey
        }
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTSHARE'
          value: toLower(functionAppName)
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
        {
          name: 'WEBSITE_NODE_DEFAULT_VERSION'
          value: '~14'
        }
      ]
    }
  }
}

Penting

Jangan atur pengaturan WEBSITE_CONTENTSHARE di slot penyebaran baru. Pengaturan ini dibuat untuk Anda ketika aplikasi dibuat di slot penyebaran.

Linux

Aplikasi fungsi akan mengatur "kind": "functionapp,linux", dan mengatur properti "reserved": true. Aplikasi Linux juga harus menyertakan properti linuxFxVersion di bawah siteConfig. Jika Anda hanya menyebarkan kode, nilai untuk properti ini ditentukan oleh tumpukan runtime yang diinginkan dalam format runtime|runtimeVersion. Misalnya: python|3.7, node|14, dan dotnet|3.1.

Untuk paket Konsumsi Linux, diperlukan juga untuk menambahkan dua pengaturan lain dalam konfigurasi situs: WEBSITE_CONTENTAZUREFILECONNECTIONSTRING dan WEBSITE_CONTENTSHARE.

Untuk mengetahui sampel templat Azure Resource Manager, lihat Aplikasi Fungsi Azure yang Dihosting di Paket Konsumsi Linux.

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp,linux'
  properties: {
    reserved: true
    serverFarmId: hostingPlan.id
    siteConfig: {
      linuxFxVersion: 'node|14'
      appSettings: [
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsights.properties.InstrumentationKey
        }
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
      ]
    }
  }
}

Sebarkan pada paket Premium

Paket Premium menawarkan penskalaan yang sama dengan paket Konsumsi tetapi mencakup sumber daya khusus dan kemampuan ekstra. Untuk mempelajari selengkapnya, lihat Paket Azure Functions Premium.

Membuat paket Premium

Paket Premium adalah jenis khusus sumber daya serverfarm. Anda dapat menentukannya dengan menggunakan EP1, EP2, atau EP3 untuk nilai properti Name dalam sku seperti yang ditunjukkan dalam sampel berikut:

Windows

resource hostingPlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    name: 'EP1'
    tier: 'ElasticPremium'
    family: 'EP'
  }
  kind: 'elastic'
  properties: {
    maximumElasticWorkerCount: 20
  }
}

Linux

Untuk menjalankan aplikasi di Linux, Anda juga harus mengatur properti "reserved": true untuk sumber daya serverfarms:

resource hostingPlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    name: 'EP1'
    tier: 'ElasticPremium'
    family: 'EP'
  }
  kind: 'elastic'
  properties: {
    maximumElasticWorkerCount: 20
    reserved: true
  }
}

Buat aplikasi fungsi

Untuk aplikasi fungsi pada paket Premium, Anda perlu mengatur properti serverFarmId di aplikasi agar mengarah ke ID sumber daya paket tersebut. Anda harus memastikan bahwa aplikasi fungsi memiliki pengaturan dependsOn untuk paket juga.

Paket Premium memerlukan pengaturan lain dalam konfigurasi situs: WEBSITE_CONTENTAZUREFILECONNECTIONSTRING dan WEBSITE_CONTENTSHARE. Properti ini mengonfigurasi akun penyimpanan tempat kode aplikasi fungsi dan konfigurasi disimpan, yang digunakan untuk skala dinamis.

Untuk sampel file Bicep/templat Azure Resource Manager, lihat Aplikasi Fungsi Azure yang Di-hosting di Paket Premium.

Pengaturan yang diperlukan oleh aplikasi fungsi yang berjalan dalam paket Premium berbeda antara Windows dan Linux.

Windows

resource functionAppName_resource 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  properties: {
    serverFarmId: hostingPlanName.id
    siteConfig: {
      appSettings: [
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsightsName.properties.InstrumentationKey
        }
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTSHARE'
          value: toLower(functionAppName)
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
        {
          name: 'WEBSITE_NODE_DEFAULT_VERSION'
          value: '~14'
        }
      ]
    }
  }
}

Penting

Tidak perlu mengubah pengaturan WEBSITE_CONTENTSHARE karena dibuat untuk Anda saat situs pertama kali dibuat.

Linux

Aplikasi fungsi akan mengatur "kind": "functionapp,linux", dan mengatur properti "reserved": true. Aplikasi Linux juga harus menyertakan properti linuxFxVersion di bawah siteConfig. Jika Anda hanya menyebarkan kode, nilai untuk properti ini ditentukan oleh tumpukan runtime yang diinginkan dalam format runtime|runtimeVersion. Misalnya: python|3.7, node|14, dan dotnet|3.1.

resource functionApp 'Microsoft.Web/sites@2021-02-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp,linux'
  properties: {
    reserved: true
    serverFarmId: hostingPlan.id
    siteConfig: {
      linuxFxVersion: 'node|14'
      appSettings: [
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsightsName.properties.InstrumentationKey
        }
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'WEBSITE_CONTENTSHARE'
          value: toLower(functionAppName)
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
      ]
    }
  }
}

Sebarkan pada paket App Service

Dalam paket App Service, aplikasi fungsi Anda berjalan pada VM khusus pada SKU Dasar, Standar, dan Premium, mirip dengan aplikasi web. Untuk detail tentang cara kerja paket App Service, lihat gambaran mendalam paket Azure App Service.

Untuk sampel file Bicep/templat Azure Resource Manager, lihat Aplikasi fungsi pada paket Azure App Service.

Membuat paket Khusus

Dalam Functions, paket Khusus hanyalah paket App Service reguler, yang ditentukan oleh sumber daya serverfarm. Anda dapat menentukan SKU sebagai berikut:

Windows

resource hostingPlanName 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    tier: 'Standard'
    name: 'S1'
    size: 'S1'
    family: 'S'
    capacity: 1
  }
}

Linux

Untuk menjalankan aplikasi di Linux, Anda juga harus mengatur properti "reserved": true untuk sumber daya serverfarms:

resource hostingPlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    tier: 'Standard'
    name: 'S1'
    size: 'S1'
    family: 'S'
    capacity: 1
  }
  properties: {
    reserved: true
  }
}

Membuat aplikasi fungsi

Untuk aplikasi fungsi pada paket Khusus, Anda harus mengatur properti serverFarmId pada aplikasi agar menunjuk ke ID sumber daya paket tersebut. Pastikan bahwa aplikasi fungsi memiliki pengaturan dependsOn yang juga mereferensikan paket tersebut.

Pada paket App Service, Anda harus mengaktifkan pengaturan "alwaysOn": true di konfigurasi situs agar aplikasi fungsi Anda berjalan dengan benar. Pada paket App Service, runtime fungsi menganggur setelah beberapa menit tidak aktif, jadi hanya pemicu HTTP yang akan "membangunkan" fungsi Anda.

Pengaturan WEBSITE_CONTENTAZUREFILECONNECTIONSTRING dan WEBSITE_CONTENTSHARE tidak didukung pada paket Khusus.

Untuk sampel file Bicep/templat Azure Resource Manager, lihat Aplikasi Fungsi Azure yang Di-hosting di Paket Khusus.

Pengaturan yang diperlukan oleh aplikasi fungsi yang berjalan dalam paket Khusus berbeda antara Windows dan Linux.

Windows

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  properties: {
    serverFarmId: hostingPlan.id
    siteConfig: {
      alwaysOn: true
      appSettings: [
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsightsName.properties.InstrumentationKey
        }
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
        {
          name: 'WEBSITE_NODE_DEFAULT_VERSION'
          value: '~14'
        }
      ]
    }
  }
}

Linux

Aplikasi fungsi akan mengatur "kind": "functionapp,linux", dan mengatur properti "reserved": true. Aplikasi Linux juga harus menyertakan properti linuxFxVersion di bawah siteConfig. Jika Anda hanya menyebarkan kode, nilai untuk properti ini ditentukan oleh tumpukan runtime yang diinginkan dalam format runtime|runtimeVersion. Contoh properti linuxFxVersion meliputi: python|3.7, node|14, dan dotnet|3.1.

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp,linux'
  properties: {
    reserved: true
    serverFarmId: hostingPlan.id
    siteConfig: {
      alwaysOn: true
      linuxFxVersion: 'node|14'
      appSettings: [
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsightsName.properties.InstrumentationKey
        }
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};EndpointSuffix=${environment().suffixes.storage};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
      ]
    }
  }
}

Gambar Kontainer Kustom

Jika Anda menyebarkan citra kontainer kustom, Anda harus menentukannya dengan linuxFxVersion dan menyertakan konfigurasi yang memungkinkan citra Anda ditarik, seperti di Aplikasi Web untuk Kontainer. Selain itu, atur WEBSITES_ENABLE_APP_SERVICE_STORAGE ke false, karena konten aplikasi Anda disediakan dalam kontainer itu sendiri:

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  properties: {
    serverFarmId: hostingPlan.id
    siteConfig: {
      appSettings: [
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: 'node'
        }
        {
          name: 'WEBSITE_NODE_DEFAULT_VERSION'
          value: '~14'
        }
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'DOCKER_REGISTRY_SERVER_URL'
          value: dockerRegistryUrl
        }
        {
          name: 'DOCKER_REGISTRY_SERVER_USERNAME'
          value: dockerRegistryUsername
        }
        {
          name: 'DOCKER_REGISTRY_SERVER_PASSWORD'
          value: dockerRegistryPassword
        }
        {
          name: 'WEBSITES_ENABLE_APP_SERVICE_STORAGE'
          value: 'false'
        }
      ]
      linuxFxVersion: 'DOCKER|myacr.azurecr.io/myimage:mytag'
    }
  }
  dependsOn: [
    storageAccount
  ]
}

Sebarkan ke Azure Arc

Azure Functions dapat disebarkan ke Kubernetes dengan dukungan Azure Arc. Proses ini sebagian besar mengikuti penyebaran ke paket App Service, dengan beberapa perbedaan yang perlu diperhatikan.

Untuk membuat aplikasi dan merencanakan resource, Anda harus sudah membuat lingkungan App Service Kubernetes untuk klaster Kubernetes dengan dukungan Azure Arc. Contoh-contoh ini mengasumsikan bahwa Anda memiliki ID sumber daya dari lokasi kustom dan lingkungan Kubernetes App Service yang menjadi tujuan penyebaran Anda. Untuk sebagian besar file Bicep/templat ARM, Anda dapat memasukkan nilai-nilai ini sebagai parameter.

param kubeEnvironmentId string
param customLocationId string

Situs dan paket harus mereferensikan lokasi kustom melalui bidang extendedLocation. Blok ini terletak di luar properties, peer ke kind dan location:

resource hostingPlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  ...
  {
    extendedLocation: {
      name: customLocationId
    }
  }
}

Sumber daya paket harus menggunakan SKU Kubernetes (K1), dan bidang kind harus linux,kubernetes. Dalam properties, reserved harus true, dan kubeEnvironmentProfile.id harus diatur ke ID sumber daya lingkungan Kubernetes App Service. Sampel paket mungkin terlihat seperti:

resource hostingPlan 'Microsoft.Web/serverfarms@2022-03-01' = {
  name: hostingPlanName
  location: location
  kind: 'linux,kubernetes'
  sku: {
    name: 'K1'
    tier: 'Kubernetes'
  }
  extendedLocation: {
    name: customLocationId
  }
  properties: {
    kubeEnvironmentProfile: {
      id: kubeEnvironmentId
    }
    reserved: true
  }
}

Sumber daya aplikasi fungsi harus mengatur bidang kind ke functionapp,linux,kubernetes atau functionapp,linux,kubernetes,container bergantung pada apakah Anda ingin menyebarkannya melalui kode atau kontainer. Contoh aplikasi fungsi .NET 6.0 mungkin terlihat seperti:

resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
  name: functionAppName
  kind: 'kubernetes,functionapp,linux,container'
  location: location
  extendedLocation: {
    name: customLocationId
  }
  properties: {
    serverFarmId: hostingPlanName
    siteConfig: {
      linuxFxVersion: 'DOCKER|mcr.microsoft.com/azure-functions/dotnet:3.0-appservice-quickstart'
      appSettings: [
        {
          name: 'FUNCTIONS_EXTENSION_VERSION'
          value: '~4'
        }
        {
          name: 'AzureWebJobsStorage'
          value: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${storageAccount.listKeys().keys[0].value}'
        }
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsightsName.properties.InstrumentationKey
        }
      ]
      alwaysOn: true
    }
  }
  dependsOn: [
    storageAccount
    hostingPlan
  ]
}

Menyesuaikan penyebaran

Aplikasi fungsi memiliki banyak sumber daya anak yang dapat Anda gunakan dalam penyebaran, termasuk pengaturan aplikasi dan opsi kontrol sumber. Anda juga dapat memilih untuk menghapus sumber daya anak sourcecontrols, dan menggunakan opsi penyebaran yang berbeda.

Pertimbangan untuk penyebaran kustom:

  • Untuk berhasil menyebarkan aplikasi Anda dengan menggunakan Azure Resource Manager, penting untuk memahami cara sumber daya disebarkan di Azure. Dalam contoh berikut, konfigurasi tingkat atas diterapkan dengan menggunakan siteConfig. Penting untuk mengatur konfigurasi ini di tingkat atas, karena konfigurasi-konfigurasi ini menyampaikan informasi ke runtime Functions dan mesin penyebaran. Informasi tingkat atas diperlukan sebelum sumber daya sourcecontrols/web anak diterapkan. Meskipun memungkinkan untuk mengonfigurasi pengaturan ini di sumber daya config/appSettings tingkat turunan, terkadang aplikasi fungsi harus disebarkan sebelumconfig/appSettings diterapkan. Misalnya, saat Anda menggunakan fungsi dengan Logic Apps, fungsi Anda adalah dependensi dari sumber daya lain.

    resource functionApp 'Microsoft.Web/sites@2022-03-01' = {
      name: functionAppName
      location: location
      kind: 'functionapp'
      properties: {
        serverFarmId: hostingPlan.id
        siteConfig: {
          alwaysOn: true
          appSettings: [
            {
              name: 'FUNCTIONS_EXTENSION_VERSION'
              value: '~4'
            }
            {
              name: 'Project'
              value: 'src'
            }
          ]
        }
      }
      dependsOn: [
        storageAccount
      ]
    }
    
    resource config 'Microsoft.Web/sites/config@2022-03-01' = {
      parent: functionApp
      name: 'appsettings'
      properties: {
        AzureWebJobsStorage: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${storageAccount.listKeys().keys[0].value}'
        AzureWebJobsDashboard: 'DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${storageAccount.listKeys().keys[0].value}'
        FUNCTIONS_EXTENSION_VERSION: '~4'
        FUNCTIONS_WORKER_RUNTIME: 'dotnet'
        Project: 'src'
      }
      dependsOn: [
        sourcecontrol
        storageAccount
      ]
    }
    
    resource sourcecontrol 'Microsoft.Web/sites/sourcecontrols@2022-03-01' = {
      parent: functionApp
      name: 'web'
      properties: {
        repoUrl: repoUrl
        branch: branch
        isManualIntegration: true
      }
    }
    
  • File Bicep dan templat ARM sebelumnya menggunakan nilai pengaturan aplikasi Proyek , yang mengatur direktori dasar tempat mesin penyebaran Functions (Kudu) mencari kode yang dapat disebarkan. Dalam repositori kami, fungsi kami berada dalam subfolder folder src. Jadi, dalam contoh sebelumnya, kami menetapkan nilai pengaturan aplikasi ke src. Jika fungsi Anda berada di akar repositori Anda, atau jika Anda tidak menyebarkan dari kontrol sumber, Anda dapat menghapus nilai pengaturan aplikasi ini.

  • Saat memperbarui pengaturan aplikasi menggunakan Bicep atau ARM, pastikan Anda menyertakan semua pengaturan yang ada. Anda harus melakukan ini karena panggilan REST API yang mendasar menggantikan pengaturan aplikasi yang ada saat API pembaruan dipanggil.

Menerapkan templat Anda

Anda bisa menggunakan salah satu cara berikut untuk menyebarkan file Bicep dan templat Anda:

Tombol sebarkan ke Azure

Catatan

Saat ini, metode ini tidak mendukung penyebaran file Bicep jarak jauh.

Ganti <url-encoded-path-to-azuredeploy-json> dengan versi URL yang dikodekan dari jalur mentah file azuredeploy.json Anda di GitHub.

Berikut adalah contoh yang menggunakan markdown:

[![Deploy to Azure](https://azuredeploy.net/deploybutton.png)](https://portal.azure.com/#create/Microsoft.Template/uri/<url-encoded-path-to-azuredeploy-json>)

Berikut adalah contoh yang menggunakan HTML:

<a href="https://portal.azure.com/#create/Microsoft.Template/uri/<url-encoded-path-to-azuredeploy-json>" target="_blank"><img src="https://azuredeploy.net/deploybutton.png"></a>

Menyebarkan menggunakan PowerShell

Perintah PowerShell berikut membuat grup sumber daya dan menyebarkan file Bicep/templat ARM yang membuat aplikasi fungsi dengan sumber daya yang diperlukan. Untuk berjalan secara lokal, Anda harus menginstal Azure PowerShell. Jalankan Connect-AzAccount untuk masuk.

# Register Resource Providers if they're not already registered
Register-AzResourceProvider -ProviderNamespace "microsoft.web"
Register-AzResourceProvider -ProviderNamespace "microsoft.storage"

# Create a resource group for the function app
New-AzResourceGroup -Name "MyResourceGroup" -Location 'West Europe'

# Deploy the template
New-AzResourceGroupDeployment -ResourceGroupName "MyResourceGroup" -TemplateFile main.bicep  -Verbose

Untuk menguji penyebaran ini, Anda dapat menggunakan templat seperti ini yang membuat aplikasi fungsi pada Windows dalam paket Konsumsi.

Langkah berikutnya

Pelajari selengkapnya tentang cara mengembangkan dan mengonfigurasi Azure Functions.