JavaScriptを有効にしてください

ARM テンプレートとBicep 03 | Template Spec を試し隊

 ·   5 分で読めます  ·   Kento

今回は ARM テンプレートや Bicep と一緒に使うテンプレート スペックを試してみます

Template Spec とは

ARM テンプレートを Github などの Git ツールを使わずに、Azure の機能として管理をするサービスです
ARM テンプレートや Bicep を Azure へアップロードして管理をするということです
RBAC を使った権限管理やタグ付けなど Azure ネイティブの機能を使うことができます

graph LR
    1[テンプレート ファイル] --> 2((公開))
    2 --> 3[バージョン]
    4((デプロイ)) --> 5[新規リソース]
  subgraph 新規リソース グループ
    5
  end
  subgraph リソース グループ
    3[バージョン]
    subgraph テンプレート スペック
      3
    end
  end
  4-- 参照 --> 3[バージョン]

Template Spec がないと?

ARM テンプレートや Bicep を社内で共有するときが困ります

  • 共用のストレージに保存すると、Azure の利用者ではない人にもファイルを操作される可能性がある
  • 都度メールで送付などはめんどくさい
  • Github などに置くのもベターですが、SAS の管理などの手間があるそうです(公開情報にあったけどイマイチ大変度合いがわからない

RBAC で権限が渡せるなら、Azure の利用スコープに色々結びつくので便利な観点は多そうです

ARM Template (JSON) で使ってみる

通常の ARM テンプレートのファイルを用意します
Template Spec を保存するリソースグループ (templateSpecRG) は既に作成済みとします
今回はストレージ アカウントを対象にしています

 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
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS",
        "Premium_LRS"
      ]
    }
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-06-01",
      "name": "[concat('store', uniquestring(resourceGroup().id))]",
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      }
    }
  ]
}

Azure_ARM_template/Blog/arm_bicep_03/storage.json at main · NakayamaKento/Azure_ARM_template

次に以下のコマンドを実行して ARM テンプレートを基に Template Spec を作成します

1
2
3
4
5
6
az ts create \
  --name storageSpec \
  --version "1.0a" \
  --resource-group templateSpecRG \
  --location "westus2" \
  --template-file "./storage.json"

上記の方法は

  • ストレージ アカウントの ARM テンプレートを用意
  • Template Spec は Azure CLI で作成

としましたが、

  • ストレージ アカウントの ARM テンプレートを含む Template Spec の ARM テンプレートを用意

することもできます
その場合は以下のようになります

 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
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "templateSpecName": {
      "type": "string",
      "defaultValue": "CreateStorageAccount"
    },
    "templateSpecVersionName": {
      "type": "string",
      "defaultValue": "0.1"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Resources/templateSpecs",
      "apiVersion": "2021-05-01",
      "name": "[parameters('templateSpecName')]",
      "location": "[parameters('location')]",
      "properties": {
        "description": "A basic templateSpec - creates a storage account.",
        "displayName": "Storage account (Standard_LRS)"
      }
    },
    {
      "type": "Microsoft.Resources/templateSpecs/versions",
      "apiVersion": "2021-05-01",
      "name": "[format('{0}/{1}', parameters('templateSpecName'), parameters('templateSpecVersionName'))]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Resources/templateSpecs', parameters('templateSpecName'))]"
      ],
      "properties": {
        "mainTemplate": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
            "storageAccountType": {
              "type": "string",
              "defaultValue": "Standard_LRS",
              "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_ZRS",
                "Premium_LRS"
              ]
            }
          },
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2019-06-01",
              "name": "[concat('store', uniquestring(resourceGroup().id))]",
              "location": "[resourceGroup().location]",
              "kind": "StorageV2",
              "sku": {
                "name": "[parameters('storageAccountType')]"
              }
            }
          ]
        }
      }
    }
  ]
}

Azure_ARM_template/Blog/arm_bicep_03/template_spec.json at main · NakayamaKento/Azure_ARM_template

デプロイが完了すると、このようなリソースが表示されます

portal01
デプロイ スタック:

コマンドで確認したいときは以下です

1
az ts list

Bicep で使ってみる

通常の Bicep のファイルを用意します
Template Spec を保存するリソースグループ (templateSpecRG) は既に作成済みとします
今回はストレージ アカウントを対象にしています

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_ZRS'
  'Premium_LRS'
])
param storageAccountType string = 'Standard_LRS'

resource stg 'Microsoft.Storage/storageAccounts@2021-04-01' = {
  name:  'store${uniqueString(resourceGroup().id)}'
  location: resourceGroup().location
  sku: {
    name: storageAccountType
  }
  kind:'StorageV2'
}

Azure_Bicep/Blog/arm_bicep_03/storage.bicep at main · NakayamaKento/Azure_Bicep

次に以下のコマンドを実行して Bicep ファイルを基に Template Spec を作成します

1
2
3
4
5
6
az ts create \
  --name storageSpec_bicep \
  --version "1.0a" \
  --resource-group templateSpecRG \
  --location "westus2" \
  --template-file "./storage.bicep"

無事に作成が完了しました

portal03
デプロイ スタック:
portal05
デプロイ スタック:

ARM テンプレート (JSON) と同じように Template Spec そのものの 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
param templateSpecName string = 'CreateStorageAccount'
param templateSpecVersionName string = '0.1'
param location string = resourceGroup().location

resource createTemplateSpec 'Microsoft.Resources/templateSpecs@2021-05-01' = {
  name: templateSpecName
  location: location
  properties: {
    description: 'A basic templateSpec - creates a storage account.'
    displayName: 'Storage account (Standard_LRS)'
  }
}

resource createTemplateSpecVersion 'Microsoft.Resources/templateSpecs/versions@2021-05-01' = {
  parent: createTemplateSpec
  name: templateSpecVersionName
  location: location
  properties: {
    mainTemplate: {
      '$schema': 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#'
      'contentVersion': '1.0.0.0'
      'parameters': {
        'storageAccountType': {
          'type': 'string'
          'defaultValue': 'Standard_LRS'
          'allowedValues': [
            'Standard_LRS'
            'Standard_GRS'
            'Standard_ZRS'
            'Premium_LRS'
          ]
        }
      }
      'resources': [
        {
          'type': 'Microsoft.Storage/storageAccounts'
          'apiVersion': '2019-06-01'
          'name': 'store$uniquestring(resourceGroup().id)'
          'location': resourceGroup().location
          'kind': 'StorageV2'
          'sku': {
            'name': '[parameters(\'storageAccountType\')]'
          }
        }
      ]
    }
  }
}

Azure_Bicep/Blog/arm_bicep_03/template_spec.bicep at main · NakayamaKento/Azure_Bicep

Template Spec を使ったデプロイ

CLI の方法

まず ARM テンプレートを基に作成した Template Spec からデプロイします
Template Spec のリソース ID を引数にしてデプロイします

1
2
3
az deployment group create \
  --resource-group demoRG \
  --template-spec /subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/templateSpecsRG/providers/Microsoft.Resources/templateSpecs/storageSpec/versions/1.0a

ARM テンプレートを基にしたストレージ アカウントが作成されました

portal02
デプロイ スタックから作成したストレージ アカウント:

Azure Portal の方法

Bicep ファイルを基にした Template Spec を Azure Portal からデプロイしてみます

画面上部の [展開] を選択

portal03
デプロイ スタック:

画面の通り実行してみる
※Storage Accout Type のところは Bicep ファイルで作成した通りの選択肢になっている

portal04
デプロイ スタックを使ってデプロイ01:

問題なくデプロイが完了した

portal06
デプロイ スタックを使ってデプロイ02:

まとめ

Template spec を使用すると RBAC を使って ARM テンプレートや Bicep ファイルを基にした テンプレートを共有することができる
CLI でも GUI でも利用できるので、CLI が苦手なユーザーにも使ってもらいやすそう

今度はリンクされたテンプレートでも試してみたいと思います
リンクされたテンプレートを使用してテンプレート スペックを作成する - Azure Resource Manager | Microsoft Learn

参考

共有

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