👌

Azure Resource Managerテンプレートをデプロイ

に公開

https://learn.microsoft.com/en-us/training/modules/create-azure-resource-manager-template-vs-code/3-exercise-create-and-deploy-template?pivots=powershell

ARMテンプレートを用意

azuredeploy.jsonを作成し、以下の内容を用意する

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "functions": [],
  "variables": {},
  "resources": [],
  "outputs": {}
}

Azureにログイン

Azure PowerShell Toolsをインストール

以下のコマンドを打つ

Connect-AzAccount

ログイン完了

テンプレートをデプロイ

以下のコマンドで、East USリージョンにてResource Groupを作成

New-AzResourceGroup -Name rg-arm -Location eastus

リソースグループが作成されたことを確認

デフォルトのリソースグループを設定

Set-AzDefault -ResourceGroupName rg-arm

テンプレートをデプロイ
※azuredeploy.jsonの中身は空っぽうのため、何のリソースも作らないです

$templateFile="azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="blanktemplate-"+"$today"
New-AzResourceGroupDeployment -Name $deploymentName -TemplateFile $templateFile

デプロイのステータスをチェック

ARMテンプレートをアップデート

以下の内容でARMテンプレートをアップデート

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "functions": [],
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-01-01",
      "name": "sa0111chenyia01",
      "tags": {
        "displayName": "sa0111chenyia01"
      },
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "Standard_LRS"
      }
    }
  ],
  "outputs": {}
}

ARMテンプレートをデプロイ

以下のコマンドを実行する

$templateFile="azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addstorage-"+"$today"
New-AzResourceGroupDeployment -Name $deploymentName -TemplateFile $templateFile

デプロイの結果をチェック

Parameterでの練習

https://learn.microsoft.com/en-us/training/modules/create-azure-resource-manager-template-vs-code/5-exercise-parameters-output?pivots=powershell

parametersの内容をARMテンプレートに追加

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
+ "parameters": {
+   "storageName": {
+       "type": "string",
+       "minLength": 3,
+       "maxLength": 24,
+       "metadata": {
+        "description": "The name of the Azure storage resource"
+       }
+   }
+},
  "functions": [],
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-01-01",
+     "name": "[parameters('storageName')]",
      "tags": {
+       "displayName": "[parameters('storageName')]"
      },
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "Standard_LRS"
      }
    }
  ],
  "outputs": {}
}

デプロイコマンドを実行

$templateFile="azuredeploy.json"
$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addnameparameter-"+"$today"
New-AzResourceGroupDeployment -Name $deploymentName -TemplateFile $templateFile -storageName sa0111chenyia02

Parameterでの練習2

もう一個のParamenterを追加し練習する

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
        "type": "string",
        "minLength": 3,
        "maxLength": 24,
        "metadata": {
        "description": "The name of the Azure storage resource"
        }
    },
+   "storageSKU": {
+       "type": "string",
+       "defaultValue": "Standard_LRS",
+       "allowedValues": [
+           "Standard_LRS",
+           "Standard_GRS",
+           "Standard_RAGRS",
+           "Standard_ZRS",
+           "Premium_LRS",
+           "Premium_ZRS",
+           "Standard_GZRS",
+           "Standard_RAGZRS"
+       ]
+   }
},
  "functions": [],
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-01-01",
      "name": "[parameters('storageName')]",
      "tags": {
        "displayName": "[parameters('storageName')]"
      },
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
+       "name": "[parameters('storageSKU')]"
      }
    }
  ],
  "outputs": {}
}

コマンドを打ってデプロイ

$today=Get-Date -Format "MM-dd-yyyy"
$deploymentName="addSkuParameter-"+"$today"
New-AzResourceGroupDeployment -Name $deploymentName -TemplateFile $templateFile -storageName sa0111chenyia03 -storageSKU Standard_GRS

オプション以外のStringをコマンドに入れて、デプロイを試みる

New-AzResourceGroupDeployment -Name $deploymentName -TemplateFile $templateFile -storageName sa0111chenyia04 -storageSKU Basic

エラーメッセージが表示で、デプロイ失敗

Outputオプションを編集

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "storageName": {
        "type": "string",
        "minLength": 3,
        "maxLength": 24,
        "metadata": {
        "description": "The name of the Azure storage resource"
        }
    },
    "storageSKU": {
        "type": "string",
        "defaultValue": "Standard_LRS",
        "allowedValues": [
            "Standard_LRS",
            "Standard_GRS",
            "Standard_RAGRS",
            "Standard_ZRS",
            "Premium_LRS",
            "Premium_ZRS",
            "Standard_GZRS",
            "Standard_RAGZRS"
        ]
    }
},
  "functions": [],
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2025-01-01",
      "name": "[parameters('storageName')]",
      "tags": {
        "displayName": "[parameters('storageName')]"
      },
      "location": "[resourceGroup().location]",
      "kind": "StorageV2",
      "sku": {
        "name": "[parameters('storageSKU')]"
      }
    }
  ],
+ "outputs": {
+   "storageEndpoint": {
+       "type": "object",
+       "value": "[reference(parameters('storageName')).primaryEndpoints]"
+   }
+ }
}

コマンドを実行する

New-AzResourceGroupDeployment -Name $deploymentName -TemplateFile $templateFile -storageName sa0111chenyia04 -storageSKU Standard_GRS

Outputを確認

Discussion