JavaScriptを有効にしてください

Bicep で VM のカスタム スクリプト拡張機能を使い隊

 ·   4 分で読めます  ·   Kento

Azure Bicep を使用して Virtual Machine のカスタムスクリプト拡張機能をデプロイしたので備忘録です
結論を先に書くと Azure Verified Module を使った方法ではうまくいきませんでした

ざっくり手順

  1. Azure Verified Module を使った Bicep ファイルの準備
  2. カスタムスクリプト用 PowerShell スクリプトの準備
  3. デプロイの実行、トラブルシューティング

1. Azure Verified Module を使った Bicep ファイルの準備

まず、Bicepファイルを作成します
今回も Azure Verified Module を使用して作成しました

Azure Verified Modules を使って Bicep をデプロイし隊 – クラウドを勉強し隊

---
title: Bicep ファイルで作成するもの
---
graph LR
subgraph Vnet[vnet]
    subgraph Subnet[subnet]
        WS1("VM + カスタムスクリプト拡張機能")
    end
end
NSG1(NSG)

classDef subG fill:none,color:#0a0,stroke:#0a0
class Vnet,Subnet subG
classDef SCP fill:#e83,color:#fff,stroke:none
class WS1 SCP
classDef NSGG fill:#46d,color:#fff,stroke:#fff
class NSG1 NSGG

Subnet -.- NSG1

Azure Verified Module を使うと VM の子リソースとしての拡張機能は一緒に書くことができます
公式ドキュメントを見に行くと拡張機能は次のものが整備されています

  • extensionAadJoinConfig
  • extensionAntiMalwareConfig
  • extensionAzureDiskEncryptionConfig
  • extensionCustomScriptConfig
  • extensionCustomScriptProtectedSetting
  • extensionDependencyAgentConfig
  • extensionDomainJoinConfig
  • extensionDomainJoinPassword
  • extensionDSCConfig
  • extensionGuestConfigurationExtension
  • extensionGuestConfigurationExtensionProtectedSettings
  • extensionHostPoolRegistration
  • extensionMonitoringAgentConfig
  • extensionNetworkWatcherAgentConfig
  • extensionNvidiaGpuDriverWindows

bicep-registry-modules/avm/res/compute/virtual-machine at avm/res/compute/virtual-machine/0.2.3 · Azure/bicep-registry-modules · GitHub

すごいたくさん整備されています
その中でも今回は extensionCustomScriptConfig を使います

実際に作成した VM の Bicep を抜き出すと以下になります

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module iisServer 'br/public:avm/res/compute/virtual-machine:0.2.3' = {
  name: '${prefix}-winser2022-deploy'
  params: {
    adminUsername: adminUsername
    adminPassword: adminPassword
    availabilityZone: 0
    imageReference: {
      publisher: 'MicrosoftWindowsServer'
      offer: 'WindowsServer'
      sku: '2022-datacenter-azure-edition'
      version: 'latest'
    }
    name: '${prefix}-win2022'
    nicConfigurations: [
      {
        ipConfigurations: [
          {
            name: '${prefix}-ip-config'
            pipConfiguration: {
              publicIpNameSuffix: '-pip'
            }
            privateIpAddressVersion: 'IPv4'
            subnetResourceId: Vnet.outputs.subnetResourceIds[0]
          }
        ]
        nicSuffix: '-nic'
      }
    ]
    osDisk: {
      diskSizeGB: '128'
      managedDisk: {
        storageAccountType: 'Premium_LRS'
      }
    }
    osType: 'Windows'
    vmSize: 'Standard_D4s_v4'
    // ここからカスタムスクリプト拡張機能
    extensionCustomScriptConfig: {
      enabled: true
      fileData: [
        {
          uri: 'https://raw.githubusercontent.com/NakayamaKento/Azure_Bicep/main/Blog/vm_customscript/installiis.ps1'

        }
      ]
      settings: {
        commandToExecute: 'powershell.exe -ExecutionPolicy Unrestricted -File installiis.ps1'
      }
    }
  }
}

最後の方でカスタムスクリプト拡張機能を書いています
uri でカスタムスクリプト拡張機能で実行してほしい PowerShell スクリプトを指定し
settings の commandToExecute でスクリプトが呼び出されます

公式ドキュメントも参考にしました

fileUris: スクリプト ファイルが格納される場所です。 提供された場所を使用しない場合は、値を更新する必要があります。
commandToExecute:このコマンドによってスクリプトが呼び出されます。

テンプレートを使用して VM 拡張機能をデプロイする - Azure Resource Manager | Microsoft Learn

2. カスタムスクリプト用 PowerShell スクリプトの準備

カスタムスクリプトとして実行する PowerShell スクリプトを用意します
今回は Windows サーバーに IIS をインストールします

Azure_Bicep/Blog/vm_customscript/installiis.ps1 at main · NakayamaKento/Azure_Bicep · GitHub

3. デプロイの実行、トラブルシューティング

Bicep ファイルと PowerShell スクリプトが用意できたのでデプロイしてみます

が、成功せずにエラーが返ってきました、、、
エラー内容を確認すると CommandToExecute が見つからない という趣旨がかかれています
しかし, 実際には module のパラメータ-には含めています

{“code”:“VMExtensionProvisioningError”,“message”:“VM has reported a failure when processing extension ‘CustomScriptExtension’ (publisher ‘Microsoft.Compute’ and type ‘CustomScriptExtension’). Error message: ‘Invalid Configuration - CommandToExecute is not specified in the configuration; it must be specified in either the protected or public configuration section’. More information on troubleshooting is available at https://aka.ms/VMExtensionCSEWindowsTroubleshoot. “}

そこでカスタムスクリプト拡張機能だけ Azure Verified Module を使わずにデプロイしてみます
module のパラメーターからカスタムスクリプト拡張機能の部分をコメントアウトしています

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module iisServer 'br/public:avm/res/compute/virtual-machine:0.2.3' = {
  name: '${prefix}-winser2022-deploy'
  params: {
    adminUsername: adminUsername
    adminPassword: adminPassword
    availabilityZone: 0
    imageReference: {
      publisher: 'MicrosoftWindowsServer'
      offer: 'WindowsServer'
      sku: '2022-datacenter-azure-edition'
      version: 'latest'
    }
    name: '${prefix}-win2022'
    nicConfigurations: [
      {
        ipConfigurations: [
          {
            name: '${prefix}-ip-config'
            pipConfiguration: {
              publicIpNameSuffix: '-pip'
            }
            privateIpAddressVersion: 'IPv4'
            subnetResourceId: Vnet.outputs.subnetResourceIds[0]
          }
        ]
        nicSuffix: '-nic'
      }
    ]
    osDisk: {
      diskSizeGB: '128'
      managedDisk: {
        storageAccountType: 'Premium_LRS'
      }
    }
    osType: 'Windows'
    vmSize: 'Standard_D4s_v4'
    // ここからカスタムスクリプト拡張機能
    /*
    extensionCustomScriptConfig: {
      enabled: true
      fileData: [
        {
          uri: 'https://raw.githubusercontent.com/NakayamaKento/Azure_Bicep/main/Blog/vm_customscript/installiis.ps1'

        }
      ]
      settings: {
        commandToExecute: 'powershell.exe -ExecutionPolicy Unrestricted -File installiis.ps1'
      }
    }
    */
  }
}

// カスタムスクリプト拡張機能を外出し
resource iisInstall 'Microsoft.Compute/virtualMachines/extensions@2020-06-01'= {
  name: '${prefix}-win2022/installIIS'
  location: resourceGroup().location
  dependsOn: [
    iisServer
  ]
  properties:{
    publisher: 'Microsoft.Compute'
    type: 'CustomScriptExtension'
    typeHandlerVersion: '1.10'
    autoUpgradeMinorVersion: true
    enableAutomaticUpgrade: false
    settings:{
      fileUris:[
        'https://raw.githubusercontent.com/NakayamaKento/Azure_Bicep/main/Blog/vm_customscript/installiis.ps1'
      ]
      commandToExecute: 'powershell -ExecutionPolicy Unrestricted -File installiis.ps1'
    }
  }
}

このファイルのデプロイは成功しました
ということは Azure Verified Module 側に不具合がありそうです
いろいろ調査をした結果、Module 内の処理で commandToExecute がうまくパラメーターとして渡せていないことがわかりました

Azure Verified Module へ Issues をあげておきました
[AVM Module Issue]:extensionCustomScriptConfig issue in avm/res/compute/virtual-machine · Issue #1540 · Azure/bicep-registry-modules · GitHub

まとめ

Bicep を使って VM のカスタムスクリプト拡張機能をデプロイしました
残念ながら Azure Verified Module だけを使ってデプロイすることはできませんでしたが
やりたいことは出来たのでよかったです

参考

共有

Kento
著者
Kento
2020年に新卒で IT 企業に入社. インフラエンジニア(主にクラウド)として活動中