チュートリアル:リンクされたテンプレートを使用してテンプレート スペックを作成する

メイン テンプレートとリンクされたテンプレートを使用してテンプレート スペックを作成する方法について説明します。 テンプレート スペックは、ARM テンプレートを組織内の他のユーザーと共有するために使用します。 この記事では、デプロイ リソースrelativePath プロパティを使用して、メイン テンプレートとそのリンクされたテンプレートをパッケージ化するテンプレート スペックを作成する方法について説明します。

前提条件

アクティブなサブスクリプションが含まれる Azure アカウント。 無料でアカウントを作成できます

Note

Azure PowerShell でテンプレート スペックを使用するには、バージョン 5.0.0 以降をインストールする必要があります。 Azure CLI でこれを使用するには、バージョン 2.14.2 以降を使用します。

リンク済みテンプレートの作成

メイン テンプレートとリンクされたテンプレートを作成します。

テンプレートをリンクするには、メイン テンプレートにデプロイ リソースを追加します。 templateLink プロパティで、親テンプレートのパスに基づいて、リンクされたテンプレートの相対パスを指定します。

リンクされたテンプレートは linkedTemplate.json と呼ばれ、メイン テンプレートが格納されているパス内の artifacts というサブフォルダーに格納されます。 relativePath には、次のいずれかの値を使用できます。

  • ./artifacts/linkedTemplate.json
  • /artifacts/linkedTemplate.json
  • artifacts/linkedTemplate.json

relativePath プロパティは、常に relativePath を宣言しているテンプレート ファイルを基準とした相対パスであるため、linkedTemplate.json から呼び出される別の linkedTemplate2.json が存在し、linkedTemplate2.json が同じ artifacts サブフォルダーに格納されている場合は、linkedTemplate.json で指定される relativePath は単に linkedTemplate2.json となります。

  1. 次の JSON を使用してメイン テンプレートを作成します。 メイン テンプレートを azuredeploy.json としてローカル コンピューターに保存します。 このチュートリアルでは、c:\Templates\linkedTS\azuredeploy.json パスに保存したものと仮定していますが、任意のパスを使用できます。

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": {
          "type": "string",
          "defaultValue": "westus2",
          "metadata":{
            "description": "Specify the location for the resources."
          }
        },
        "storageAccountType": {
          "type": "string",
          "defaultValue": "Standard_LRS",
          "metadata":{
            "description": "Specify the storage account type."
          }
        }
      },
      "variables": {
        "appServicePlanName": "[format('plan{0}', uniquestring(resourceGroup().id))]"
      },
      "resources": [
        {
          "type": "Microsoft.Web/serverfarms",
          "apiVersion": "2022-09-01",
          "name": "[variables('appServicePlanName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "B1",
            "tier": "Basic",
            "size": "B1",
            "family": "B",
            "capacity": 1
          },
          "kind": "linux",
          "properties": {
            "perSiteScaling": false,
            "reserved": true,
            "targetWorkerCount": 0,
            "targetWorkerSizeId": 0
          }
        },
        {
          "type": "Microsoft.Resources/deployments",
          "apiVersion": "2022-09-01",
          "name": "createStorage",
          "properties": {
            "mode": "Incremental",
            "templateLink": {
              "relativePath": "artifacts/linkedTemplate.json"
            },
            "parameters": {
              "storageAccountType": {
                "value": "[parameters('storageAccountType')]"
              }
            }
          }
        }
      ]
    }
    

    Note

    Microsoft.Resources/deployments の apiVersion は 2020-06-01 以降である必要があります。

  2. メイン テンプレートが保存されているフォルダーに artifacts という名前のディレクトリを作成します。

  3. 次の JSON を使用して、リンクされたテンプレートを作成します。

    {
      "$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"
          ],
          "metadata": {
            "description": "Storage Account type"
          }
        },
        "location": {
          "type": "string",
          "defaultValue": "[resourceGroup().location]",
          "metadata": {
            "description": "Location for all resources."
          }
        }
      },
      "variables": {
        "storageAccountName": "[format('store{0}', uniquestring(resourceGroup().id))]"
      },
      "resources": [
        {
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2022-09-01",
          "name": "[variables('storageAccountName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "[parameters('storageAccountType')]"
          },
          "kind": "StorageV2",
          "properties": {}
        }
      ],
      "outputs": {
        "storageAccountName": {
          "type": "string",
          "value": "[variables('storageAccountName')]"
        }
      }
    }
    
  4. テンプレートを linkedTemplate.json として artifacts フォルダーに保存します。

テンプレート スペックの作成

テンプレート スペックはリソース グループに格納されます。 リソース グループを作成した後、次のスクリプトでテンプレート スペックを作成します。 テンプレート スペック名は webSpec です。

New-AzResourceGroup `
  -Name templateSpecRG `
  -Location westus2

New-AzTemplateSpec `
  -Name webSpec `
  -Version "1.0.0.0" `
  -ResourceGroupName templateSpecRG `
  -Location westus2 `
  -TemplateFile "c:\Templates\linkedTS\azuredeploy.json"

完了したら、Azure portal から、または次のコマンドレットを使用して、テンプレート スペックを表示できます。

Get-AzTemplateSpec -ResourceGroupName templatespecRG -Name webSpec

テンプレート スペックのデプロイ

テンプレート スペックをデプロイできるようになりました。テンプレート スペックのデプロイは、それが含まれているテンプレートのデプロイと同じですが、テンプレート スペックのリソース ID を渡す点が異なります。同じデプロイ コマンドを使用し、必要に応じて、テンプレート スペックのパラメーター値を渡します。

New-AzResourceGroup `
  -Name webRG `
  -Location westus2

$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name webSpec -Version "1.0.0.0").Versions.Id

New-AzResourceGroupDeployment `
  -TemplateSpecId $id `
  -ResourceGroupName webRG

次のステップ

テンプレート スペックをリンクされたテンプレートとしてデプロイする方法の詳細については、「チュートリアル:テンプレート スペックをリンクされたテンプレートとしてデプロイする」を参照してください。