Bagikan melalui


Mulai Cepat: Membuat dan menyebarkan sumber daya Azure Functions menggunakan Bicep

Dalam artikel ini, Anda menggunakan Azure Functions dengan Bicep untuk membuat aplikasi fungsi dan sumber daya terkait di Azure. Aplikasi fungsi menyediakan konteks eksekusi bagi eksekusi kode fungsi Anda.

Menyelesaikan mulai cepat ini dikenakan biaya kecil berupa beberapa sen USD atau kurang di akun Azure Anda.

Bicep adalah bahasa pemrogram khusus domain (DSL) yang menggunakan sintaks deklaratif untuk menyebarkan sumber daya Azure. Bicep menyediakan sintaks ringkas, keamanan jenis yang andal, dan dukungan untuk penggunaan kembali kode. Bicep menawarkan pengalaman penulisan terbaik untuk solusi infrastructure-as-code di Azure.

Setelah membuat aplikasi fungsi, Anda dapat menyebarkan kode proyek Azure Functions ke aplikasi tersebut.

Prasyarat

Akun Azure

Sebelum memulai, Anda memerlukan akun Azure dengan langganan aktif. Buat akun gratis.

Tinjau file Bicep

File Bicep yang digunakan dalam mulai cepat berasal dari Templat Mulai Cepat Azure.

@description('The name of the function app that you wish to create.')
param appName string = 'fnapp${uniqueString(resourceGroup().id)}'

@description('Storage Account type')
@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
])
param storageAccountType string = 'Standard_LRS'

@description('Location for all resources.')
param location string = resourceGroup().location

@description('Location for Application Insights')
param appInsightsLocation string

@description('The language worker runtime to load in the function app.')
@allowed([
  'node'
  'dotnet'
  'java'
])
param runtime string = 'node'

var functionAppName = appName
var hostingPlanName = appName
var applicationInsightsName = appName
var storageAccountName = '${uniqueString(resourceGroup().id)}azfunctions'
var functionWorkerRuntime = runtime

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

resource hostingPlan 'Microsoft.Web/serverfarms@2021-03-01' = {
  name: hostingPlanName
  location: location
  sku: {
    name: 'Y1'
    tier: 'Dynamic'
  }
  properties: {}
}

resource functionApp 'Microsoft.Web/sites@2021-03-01' = {
  name: functionAppName
  location: location
  kind: 'functionapp'
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: hostingPlan.id
    siteConfig: {
      appSettings: [
        {
          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: 'WEBSITE_NODE_DEFAULT_VERSION'
          value: '~14'
        }
        {
          name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
          value: applicationInsights.properties.InstrumentationKey
        }
        {
          name: 'FUNCTIONS_WORKER_RUNTIME'
          value: functionWorkerRuntime
        }
      ]
      ftpsState: 'FtpsOnly'
      minTlsVersion: '1.2'
    }
    httpsOnly: true
  }
}

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

Empat sumber daya Azure berikut dibuat oleh file Bicep ini:

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.

Menerapkan file Bicep

  1. Simpan file Bicep sebagai main.bicep ke komputer lokal Anda.

  2. Sebarkan file Bicep menggunakan Azure CLI atau Azure PowerShell.

    az group create --name exampleRG --location eastus
    az deployment group create --resource-group exampleRG --template-file main.bicep --parameters appInsightsLocation=<app-location>
    

    Catatan

    Ganti <lokasi aplikasi> dengan wilayah untuk Application Insights, yang biasanya sama dengan grup sumber daya.

    Setelah penyebaran selesai, Anda akan melihat pesan yang menunjukkan penyebaran berhasil.

Memvalidasi penyebaran

Gunakan Azure CLI atau Azure PowerShell untuk memvalidasi penyebaran.

az resource list --resource-group exampleRG

Kunjungi halaman selamat datang aplikasi fungsi

  1. Gunakan output dari langkah validasi sebelumnya untuk mengambil nama unik yang dibuat untuk aplikasi fungsi Anda.

  2. Buka browser dan masukkan URL berikut: <https://<appName.azurewebsites.net>. Pastikan untuk mengganti <\appName> dengan nama unik yang dibuat untuk aplikasi fungsi Anda.

    Saat Anda mengunjungi URL, Anda akan melihat halaman seperti ini:

    Halaman selamat datang aplikasi fungsi

Membersihkan sumber daya

Jika Anda melanjutkan ke langkah berikutnya dan menambahkan pengikatan output antrean Azure Storage, tetap simpan semua sumber daya Anda di tempat karena Anda akan meneruskan yang telah Anda lakukan sebelumnya.

Jika tidak, jika Anda tidak lagi membutuhkan sumber daya, gunakan Azure CLI, PowerShell, atau portal Azure untuk menghapus grup sumber daya dan sumber dayanya.

az group delete --name exampleRG

Langkah berikutnya

Setelah membuat sumber daya aplikasi fungsi di Azure, Anda dapat menyebarkan kode ke aplikasi yang sudah ada dengan menggunakan salah satu alat berikut: