JavaScriptを有効にしてください

Azure Verified Modules を使って Bicep をデプロイし隊

 ·   5 分で読めます  ·   Kento

今回は Azure Verified Modules を使用して Bicep テンプレートをデプロイしてみます。
Azure Verified Modulesは、公式に検証された再利用可能な Bicep モジュールのコレクションであり、開発者がより迅速かつ安全にインフラストラクチャを構築できるように設計されています。

Azure Verified Modules の利点

Azure Verified Modulesは、以下のような多くの利点を提供します。

  • 信頼性: 公式に検証されたモジュールは、品質と互換性が保証されています。
  • 再利用性: 標準化されたモジュールを使用することで、コードの再利用が容易になります。
  • セキュリティ: セキュリティのベストプラクティスに基づいて構築されたモジュールは、安全なデプロイメントを促進します。

Azure Verified Modules

Bicep デプロイメントの実践

Azure Verified Modules を使用した Bicep デプロイメントのプロセスは、以下のステップに分けられます。

  1. モジュールの選定: 必要なインフラストラクチャコンポーネントに対応する Verified Module を選択します。
  2. パラメータの設定: モジュールに必要なパラメータを定義し、カスタマイズします。
  3. デプロイメントの実行: Bicep ファイルを Azure にデプロイし、リソースの作成を開始します。

1. モジュールの選定

Azure Verified Modules にアクセスし、利用可能なモジュールを確認します
Bicep ファイルは画像内の赤枠から探すことができます

avm01
Azure Verified Modules:

今回はシンプルに以下の構成にしたいと思います

  • Vnet (subnet 1つ)
  • NSG
  • Public IP address
  • NIC
  • Windows Server 2022 の VM
---
title: Vnet と Windows Server 2022
---
graph TB
subgraph Vnet[vnet]
    subgraph Subnet[subnet]
        WS1("Windows Server 2022")
    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

それぞれのリソースのモジュールを Bicep Resource Modules | Azure Verified Modules 内で検索すると見つかりました

avm03
Azure Verified Modules: Vnet

avm05
Azure Verified Modules: NSG

avm06
Azure Verified Modules: Public IP Address

avm04
Azure Verified Modules: NIC

avm02
Azure Verified Modules: VM

2. パラメータの設定

まずは Vnet を作成していきます
bicep-registry-modules/avm/res/network/virtual-network at main · Azure/bicep-registry-modules · GitHub を見てみると色んなサンプルが用意されています

  • 最小の必須パラメータを使ったサンプル
  • 多くのパラメータを利用したサンプル
  • Vnet ピアリングを構成しているサンプル
  • Well-Architected Framework のベストプラクティスに従ったサンプル
avm07
Azure Verified Modules: Vnet の詳細

このサンプルを参照にしつつ、Visual Studio Code の Bicep 拡張機能にも助けてもらいながら Bicep ファイルを作成してみます
モジュールを使うときのお作法に気を付けつつ、以下の内容を記述します

1
module virtualNetwork 'br/public:avm/res/network/virtual-network:0.1.5'

Visual Studio Code の拡張機能によってどんどん補完されていきます

vscode01
Visual Studio Code の Bicep 拡張機能: Vnet の記述01

vscode02
Visual Studio Code の Bicep 拡張機能: Vnet の記述02

vscode03
Visual Studio Code の Bicep 拡張機能: Vnet の記述03

必須のパラメータだけを参照するとこのようになります

1
2
3
4
5
6
7
module virtualNetwork 'br/public:avm/res/network/virtual-network:0.1.5' = {
  name: 
  params: {
    addressPrefixes: 
    name: 
  }
}

vscode04
Visual Studio Code の Bicep 拡張機能: Vnet の記述04

vscode05
Visual Studio Code の Bicep 拡張機能: Vnet の記述05

しかし、このままだとサブネットが存在していないのでサブネットを追加しておきます
これも拡張機能が補完してくれます

vscode06
Visual Studio Code の Bicep 拡張機能: Vnet の記述06

公式のサンプルを見ながら、パラメータも追加して このようにしてみました

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
param prefix string = 'my'
param addressPrefix string = '10.0.0.0/16'

module virtualNetwork 'br/public:avm/res/network/virtual-network:0.1.5' = {
  name: '${prefix}-vnet-deploy'
  params: {
    addressPrefixes: [
      addressPrefix
    ]
    name: '${prefix}-vnet'
    subnets: [
      {
        addressPrefix: cidrSubnet(addressPrefix, 24, 0)
        name: '${prefix}-subnet'
      }
    ]
  }
}

同様に以下のリソースも書いていきます
また、サブネットが NSG に関連付けれるように修正も加えます

  • NSG
  • Public IP address
  • NIC
  • Windows Server 2022 の VM
 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
76
77
78
79
80
81
82
83
84
85
86
param prefix string = 'my'
param addressPrefix string = '10.0.0.0/16'
param adminUsername string = 'AzureAdmin'

@secure()
param adminPassword string

module virtualNetwork 'br/public:avm/res/network/virtual-network:0.1.5' = {
  name: '${prefix}-vnet-deploy'
  params: {
    addressPrefixes: [
      addressPrefix
    ]
    name: '${prefix}-vnet'
    subnets: [
      {
        addressPrefix: cidrSubnet(addressPrefix, 24, 0)
        name: '${prefix}-subnet'
        networkSecurityGroupResourceId: nsg.outputs.resourceId
      }
    ]
  }
}

module nsg 'br/public:avm/res/network/network-security-group:0.1.3' = {
  name: '${prefix}-nsg-deploy'
  params: {
    name: '${prefix}-nsg'
    securityRules: [
      {
        name : 'Allow-RDP'
        properties: {
          access: 'Allow'
          description: 'Allow RDP'
          destinationAddressPrefix: '*'
          destinationPortRange: '3389'
          direction: 'Inbound'
          priority: 1000
          protocol: 'Tcp'
          sourceAddressPrefix: '*'
          sourcePortRange: '*'
        }
      }
    ]
  }
}


module windowsServer2022 'br/public:avm/res/compute/virtual-machine:0.2.3' = {
  name: '${prefix}-vm-deploy'
  params: {
    adminUsername: adminUsername
    adminPassword: adminPassword
    availabilityZone: 0 // 0 は可用性ゾーンなし
    imageReference: {
      offer: 'WindowsServer'
      publisher: 'MicrosoftWindowsServer'
      sku: '2022-Datacenter'
      version: 'latest'
    }
    name: '${prefix}-win2022-vm'
    nicConfigurations: [
      {
        ipConfigurations: [
          {
            name: '${prefix}-ip-config'
            pipConfiguration: {
              publicIpNameSuffix: '-pip'
            }
            privateIpAddressVersion: 'IPv4'
            subnetResourceId: virtualNetwork.outputs.subnetResourceIds[0]
          }
        ]
        nicSuffix: '-nic'
      }
    ]
    osDisk: {
      diskSizeGB: '128'
      managedDisk: {
        storageAccountType: 'Premium_LRS'
      }
    }
    osType: 'Windows'
    vmSize: 'Standard_D2s_v3'
  }
}

3. デプロイメントの実行

PowerShell を使ってデプロイしてみます

1
New-AzResourceGroupDeployment -ResourceGroupName bicep-avm -TemplateFile main.bicep

デプロイに失敗してしまいました

portal01
デプロイの様子: 失敗

PowerShell でもエラーが表示されています

 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
PS C:\Users\XXXXX\Azure_Bicep\Blog\bicep_avm> New-AzResourceGroupDeployment -ResourceGroupName bicep-avm -TemplateFile main.bicep

cmdlet New-AzResourceGroupDeployment at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
adminPassword: ************
New-AzResourceGroupDeployment: 0:54:03 - The deployment 'main' failed with error(s). Showing 1 out of 1 error(s).
Status 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. (Code: DeploymentFailed)
 - The property 'securityProfile.encryptionAtHost' is not valid because the 'Microsoft.Compute/EncryptionAtHost' feature is not enabled for this subscription. (Code:InvalidParameter)  


CorrelationId: 8b3bc073-397e-4958-bdb5-f07ea0335583

DeploymentName          : main
ResourceGroupName       : bicep-avm
ProvisioningState       : Failed
Timestamp               : 2024/04/01 15:54:00
Mode                    : Incremental
TemplateLink            : 
Parameters              : 
                          Name             Type                       Value
                          ===============  =========================  ==========
                          prefix           String                     "my"
                          addressPrefix    String                     "10.0.0.0/16"
                          adminUsername    String                     "AzureAdmin"
                          adminPassword    SecureString               null

Outputs                 : 
DeploymentDebugLogLevel : 

どうやらモジュールでは ホストの暗号化が既定で有効になっているようです
しかし自分の環境ではこの機能が有効になっていないため、エラーが発生しました
公開情報を元に機能を有効化しておきます

1
Register-AzProviderFeature -FeatureName "EncryptionAtHost" -ProviderNamespace "Microsoft.Compute"

しばらくすると有効化が完了しました

1
2
3
4
5
PS C:\Users\XXXXX\Azure_Bicep\Blog\bicep_avm> Get-AzProviderFeature -FeatureName "EncryptionAtHost" -ProviderNamespace "Microsoft.Compute"

FeatureName      ProviderName      RegistrationState
-----------      ------------      -----------------
EncryptionAtHost Microsoft.Compute Registered

ホストでの暗号化を使用してエンドツーエンドの暗号化を有効にする - Azure portal - マネージド ディスク - Azure Virtual Machines | Microsoft Learn

改めてデプロイを実行してみたら、無事に成功しました

portal02
デプロイの様子: 成功
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
PS C:\Users\XXXXXX\azure\Azure_Bicep\Blog\bicep_avm> New-AzResourceGroupDeployment -ResourceGroupName bicep-avm -TemplateFile main.bicep

cmdlet New-AzResourceGroupDeployment at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
adminPassword: ************

DeploymentName          : main
ResourceGroupName       : bicep-avm
ProvisioningState       : Succeeded
Timestamp               : 2024/04/01 16:28:06
Mode                    : Incremental
TemplateLink            : 
Parameters              : 
                          Name             Type                       Value
                          ===============  =========================  ==========
                          prefix           String                     "my"
                          addressPrefix    String                     "10.0.0.0/16"
                          adminUsername    String                     "AzureAdmin"
                          adminPassword    SecureString               null

Outputs                 : 
DeploymentDebugLogLevel : 

まとめ

Azure Verified Modulesを使用することで、Bicepを介したインフラストラクチャのデプロイメントは、より効率的かつ安全に行うことができます。
特に今回は有効化していないホストの暗号化の機能に気づくことができました。
※ブログのためのネタじゃなくてガチです。

参考

共有

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