Latihan - Melakukan refaktor templat Anda untuk menggunakan modul

Selesai

Dalam latihan ini, Anda akan memperbarui templat Bicep yang sebelumnya Anda buat sehingga menggunakan modul untuk sumber daya Azure App Service. Modul membantu menjaga niat templat utama lebih jelas. Anda dapat menggunakan kembali modul App Service di templat lain jika Anda memilih untuk melakukannya.

Selama proses tersebut, Anda akan:

  • Menambahkan modul baru dan memindahkan sumber daya App Service ke dalamnya.
  • Mereferensikan modul dari templat Bicep utama.
  • Menambahkan output untuk nama host aplikasi App Service, dan mengeluarkannya dari penyebaran modul dan templat.
  • Menguji penyebaran untuk memastikan templat valid.

Menambahkan file modul baru

  1. Di Visual Studio Code, buat folder baru bernama modul di folder yang sama tempat Anda membuat file main.bicep Anda. Di folder modul, buat file yang disebut appService.bicep. Simpan file.

  2. Tambahkan konten berikut ke dalam file appService.bicep:

    param location string
    param appServiceAppName string
    
    @allowed([
      'nonprod'
      'prod'
    ])
    param environmentType string
    
    var appServicePlanName = 'toy-product-launch-plan'
    var appServicePlanSkuName = (environmentType == 'prod') ? 'P2v3' : 'F1'
    
    resource appServicePlan 'Microsoft.Web/serverFarms@2022-03-01' = {
      name: appServicePlanName
      location: location
      sku: {
        name: appServicePlanSkuName
      }
    }
    
    resource appServiceApp 'Microsoft.Web/sites@2022-03-01' = {
      name: appServiceAppName
      location: location
      properties: {
        serverFarmId: appServicePlan.id
        httpsOnly: true
      }
    }
    

    Perhatikan bahwa Anda telah menyalin parameter dan variabel dari templat main.bicep Anda, karena templat appService.bicep perlu mandiri.

  3. Simpan perubahan pada file. Perhatikan bahwa Visual Studio Code tidak menunjukkan adanya garis berlekuk merah kepada Anda untuk menunjukkan peringatan tentang variabel yang hilang, parameter yang hilang, atau sumber daya yang tidak valid.

Menambahkan referensi ke modul dari templat induk

Sekarang setelah Anda memiliki modul lengkap untuk menyebarkan sumber daya App Service, Anda dapat merujuk ke modul dalam templat induk. Karena modul menyebarkan sumber daya App Service, Anda dapat menghapus sumber daya dan variabel terkait dari templat induk.

  1. Dalam file main.bicep, hapus sumber daya App Service dan definisi variabel appServicePlanName dan appServicePlanSkuName. Jangan hapus parameter App Service, karena Anda masih membutuhkannya. Selain itu, jangan hapus parameter, variabel, atau sumber daya akun penyimpanan.

  2. Di bagian bawah file main.bicep, tambahkan kode Bicep berikut:

    module appService 'modules/appService.bicep' = {
      name: 'appService'
      params: {
        location: location
        appServiceAppName: appServiceAppName
        environmentType: environmentType
      }
    }
    

    Perhatikan bahwa Anda menentukan parameter untuk modul Anda dengan merujuk parameter dalam templat induk.

  3. Simpan perubahan pada file.

Menambahkan nama host sebagai output

  1. Tambahkan kode Bicep berikut di bagian bawah file appService.bicep:

    output appServiceAppHostName string = appServiceApp.properties.defaultHostName
    

    Kode ini menyatakan bahwa output untuk modul ini, yang akan diberi nama appServiceAppHostName, akan berjenis string. Output akan mengambil nilainya dari properti defaultHostName aplikasi App Service.

  2. Simpan perubahan pada file.

    Output ini dideklarasikan dalam file Bicep yang akan kita gunakan sebagai modul, sehingga hanya akan tersedia untuk templat induk. Anda juga perlu mengembalikan output kepada orang yang menyebarkan templat.

  3. Buka file main.bicep dan tambahkan kode berikut di bagian bawah file:

    output appServiceAppHostName string = appService.outputs.appServiceAppHostName
    

    Perhatikan bahwa output ini dinyatakan dengan cara yang sama dengan output dalam modul. Tapi kali ini, Anda mereferensikan output modul, bukan properti sumber daya.

  4. Simpan perubahan pada file.

Memverifikasi file Bicep Anda

Setelah Anda menyelesaikan semua perubahan sebelumnya, file main.bicep Anda akan terlihat seperti contoh ini:

param location string = 'eastus'
param storageAccountName string = 'toylaunch${uniqueString(resourceGroup().id)}'
param appServiceAppName string = 'toylaunch${uniqueString(resourceGroup().id)}'

@allowed([
  'nonprod'
  'prod'
])
param environmentType string

var storageAccountSkuName = (environmentType == 'prod') ? 'Standard_GRS' : 'Standard_LRS'

resource storageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: storageAccountSkuName
  }
  kind: 'StorageV2'
  properties: {
    accessTier: 'Hot'
  }
}

module appService 'modules/appService.bicep' = {
  name: 'appService'
  params: {
    location: location
    appServiceAppName: appServiceAppName
    environmentType: environmentType
  }
}

output appServiceAppHostName string = appService.outputs.appServiceAppHostName

File appService.bicep Anda akan terlihat seperti contoh ini:

param location string
param appServiceAppName string

@allowed([
  'nonprod'
  'prod'
])
param environmentType string

var appServicePlanName = 'toy-product-launch-plan'
var appServicePlanSkuName = (environmentType == 'prod') ? 'P2v3' : 'F1'

resource appServicePlan 'Microsoft.Web/serverFarms@2022-03-01' = {
  name: appServicePlanName
  location: location
  sku: {
    name: appServicePlanSkuName
  }
}

resource appServiceApp 'Microsoft.Web/sites@2022-03-01' = {
  name: appServiceAppName
  location: location
  properties: {
    serverFarmId: appServicePlan.id
    httpsOnly: true
  }
}

output appServiceAppHostName string = appServiceApp.properties.defaultHostName

Jika salah satu file tidak cocok, salin contoh atau sesuaikan templat Anda agar sesuai dengan contohnya.

Menyebarkan templat Bicep yang diperbarui

Jalankan perintah Azure CLI berikut di terminal.

az deployment group create \
  --template-file main.bicep \
  --parameters environmentType=nonprod

Jalankan perintah Azure PowerShell berikut di terminal.

New-AzResourceGroupDeployment `
  -TemplateFile main.bicep `
  -environmentType nonprod

Memeriksa penyebaran Anda

  1. Di browser Anda, kembali ke portal Microsoft Azure. Buka grup sumber daya Anda, dan Anda akan melihat bahwa sekarang ada dua penyebaran yang berhasil.

  2. Pilih tautan 2 Berhasil. Perhatikan bahwa Anda memiliki penyebaran yang disebut utama dalam daftar, dan penyebaran baru yang disebut appService.

    Screenshot of the Azure portal interface for the deployments, with the two deployments listed and succeeded statuses.

  3. Pilih penyebaran yang disebut utama, lalu pilih Detail penyebaran untuk memperluas daftar sumber daya yang disebarkan.

    Perhatikan bahwa penyebaran modul kita muncul dalam daftar.

    Screenshot of the Azure portal interface for the specific deployment, with one resource listed.

  4. Pilih tab Output. Perhatikan bahwa ada output yang disebut appServiceAppHostName dengan nama host aplikasi App Service Anda. Salin nama host ke clipboard Anda.

    Screenshot of the Azure portal interface for the specific deployment's outputs.

  5. Buka tab browser baru dan tempelkan nama host yang Anda salin. Anda akan melihat halaman beranda App Service default.

    Screenshot of the default App Service welcome page.

Selamat! Anda telah berhasil menyebarkan fondasi untuk aplikasi yang hebat.