AzコマンドとAzure ARM TemplateでResource Groupを作成する

Azure CLI

Resource Groupの第二弾。
az group コマンドとARM templateで、それぞれResource Groupを作成してみる。詳細は適宜MSの公式ドキュメントのLinkを添付しておいたので、そちらも参照してみるとより理解が深まる。

大丈夫、そもそもResource Group自体どんだけ作っても無料。

az groupコマンドを使用したresource groupの作成・削除

“az group”とすると、ヘルプがでてわかりやすい。

$> az group
the following arguments are required: _subcommand

TRY THIS:
az group create --location westus --resource-group MyResourceGroup
Create a new resource group in the West US region.

az group delete --resource-group MyResourceGroup
Delete a resource group.

az group list --query "[?location=='westus']"
List all resource groups located in the West US region.


az group
Read more about the command in reference docs $>

resource groupの作成

$> az group create --location japanwest --resource-group freeOnlyWestJ
{
  "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/freeOnlyWestJ",
  "location": "japanwest",
  "managedBy": null,
  "name": "freeOnlyWestJ",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}
$> 

resource groupの削除

$> az group delete --resource-group freeOnlyWestJ
Are you sure you want to perform this operation? (y/n): y
$> 

ARM templateを使用したresource groupの作成(サブスク指定あり)

サブスクリプションを指定する場合、locationも必須パラメータのようだ。

$> az deployment sub create --name PayAsYouGo-0520 --template-file RG.json --parameters RGParams.json
the following arguments are required: --location/-l

TRY THIS:
az deployment sub create --location WestUS --parameters '{ \"policyName\": { \"value\": \"policy2\" } }' --template-file azuredeploy.json
Start a deployment at subscription scope. (autogenerated)

az deployment sub create --location WestUS --template-file azuredeploy.json
Start a deployment at subscription scope. (autogenerated)


az deployment sub
Read more about the command in reference docs $>

location指定するとOK。
locationはサブスクリプションのlocationでないとエラーになる。
結果レコードが長いが、重要なポイントは、japaneastの”PayAsYouGo-0520″というサブスクリプションに”freeOnlyWestJ”というResource Groupが作成され、リソースグループのロケーションが”japanwest”であるという事を確認できること。

$> az deployment sub create --name PayAsYouGo-0520 --location japaneast --template-file RG.json --parameters RGParams.json
{                                                                     
  "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/providers/Microsoft.Resources/deployments/PayAsYouGo-0520",
  "location": "japaneast",
  "name": "PayAsYouGo-0520",   --->サブスクリプション
  "properties": {
    "correlationId": "650da37e-52c5-4f3d-9e17-cd0d3bfe9931",
    "debugSetting": null,
    "dependencies": [],
    "duration": "PT1.1004384S",
    "error": null,
    "mode": "Incremental",
    "onErrorDeployment": null,
    "outputResources": [
      {
        "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/freeOnlyWestJ"
      }
    ],
    "outputs": {},
    "parameters": {
      "rgLocation": {
        "type": "String",
        "value": "japanwest"
      },
      "rgName": {
        "type": "String",
        "value": "freeOnlyWestJ"
      },
      "tags": {
        "type": "Object",
        "value": {
          "owner": "masatoshuw"
        }
      }
    },
    "parametersLink": null,
    "providers": [
      {
        "id": null,
        "namespace": "Microsoft.Resources",
        "registrationPolicy": null,
        "registrationState": null,
        "resourceTypes": [
          {
            "aliases": null,
            "apiProfiles": null,
            "apiVersions": null,
            "capabilities": null,
            "defaultApiVersion": null,
            "locationMappings": null,
            "locations": [
              "japanwest" --->リージョン
            ],
            "properties": null,
            "resourceType": "resourceGroups", --->リソースグループ
            "zoneMappings": null
          }
        ]
      }
    ],
    "provisioningState": "Succeeded",
    "templateHash": "1956942179878927632",
    "templateLink": null,
    "timestamp": "2021-08-01T03:37:25.877890+00:00",
    "validatedResources": null
  },
  "tags": null,
  "type": "Microsoft.Resources/deployments"
}
$> 

作成出来たリソースの確認。

$> az group list
[
  {
    "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/NetworkWatcherRG",
    "location": "japaneast",
    "managedBy": null,
    "name": "NetworkWatcherRG",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": "Microsoft.Resources/resourceGroups"
  },
  {
    "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/freeOnlyEastJ",
    "location": "japaneast",
    "managedBy": null,
    "name": "freeOnlyEastJ",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": {},
    "type": "Microsoft.Resources/resourceGroups"
  },
  {
    "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/DefaultResourceGroup-EJP",
    "location": "japaneast",
    "managedBy": null,
    "name": "DefaultResourceGroup-EJP",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": "Microsoft.Resources/resourceGroups"
  },
  { ---> ここから
    "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/freeOnlyWestJ",
    "location": "japanwest",
    "managedBy": null,
    "name": "freeOnlyWestJ",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": {
      "owner": "masatoshuw"
    },
    "type": "Microsoft.Resources/resourceGroups"
  }, --->ここまで
  {
    "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/cloud-shell-storage-southeastasia",
    "location": "southeastasia",
    "managedBy": null,
    "name": "cloud-shell-storage-southeastasia",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": "Microsoft.Resources/resourceGroups"
  }
]
$> 

後始末。

$> az group delete --resource-group freeOnlyWestJ
Are you sure you want to perform this operation? (y/n): y
$>

ARM templateを使用したresource groupの作成(サブスク指定なし)

サブスクリプションを指定しない場合はデフォルトサブスクリプションで実行されるようだ。サブスクリプションのデフォルトはlocationによって異なるようで、locationパラメータはサブスクリプションを指定しない場合でも必須のようだ。
(個人契約であれば通常1つのサブスクしかないからこれでいいかも)

$> az deployment sub create --template-file RG.json --parameters RGParams.json
the following arguments are required: --location/-l

TRY THIS:
az deployment sub create --location WestUS --parameters '{ \"policyName\": { \"value\": \"policy2\" } }' --template-file azuredeploy.json
Start a deployment at subscription scope. (autogenerated)

az deployment sub create --location WestUS --template-file azuredeploy.json
Start a deployment at subscription scope. (autogenerated)


az deployment sub
Read more about the command in reference docs $>

ロケーションを指定して再実行。

$> az deployment sub create --location japaneast --template-file RG.json --parameters RGParams.json
{                                                                     
  "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/providers/Microsoft.Resources/deployments/RG",
  "location": "japaneast",
  "name": "RG",
  "properties": {
    "correlationId": "bc13cf0f-a43e-4727-9e99-067937f88bc2",
    "debugSetting": null,
    "dependencies": [],
    "duration": "PT2.5925327S",
    "error": null,
    "mode": "Incremental",
    "onErrorDeployment": null,
    "outputResources": [
      {
        "id": "/subscriptions/871e8b6f-0727-42ce-840e-02bf7d76541a/resourceGroups/freeOnlyWestJ"
      }
    ],
    "outputs": {},
    "parameters": {
      "rgLocation": {
        "type": "String",
        "value": "japanwest"
      },
      "rgName": {
        "type": "String",
        "value": "freeOnlyWestJ"
      },
      "tags": {
        "type": "Object",
        "value": {
          "owner": "masatoshuw"
        }
      }
    },
    "parametersLink": null,
    "providers": [
      {
        "id": null,
        "namespace": "Microsoft.Resources",
        "registrationPolicy": null,
        "registrationState": null,
        "resourceTypes": [
          {
            "aliases": null,
            "apiProfiles": null,
            "apiVersions": null,
            "capabilities": null,
            "defaultApiVersion": null,
            "locationMappings": null,
            "locations": [
              "japanwest"
            ],
            "properties": null,
            "resourceType": "resourceGroups",
            "zoneMappings": null
          }
        ]
      }
    ],
    "provisioningState": "Succeeded",
    "templateHash": "1956942179878927632",
    "templateLink": null,
    "timestamp": "2021-08-01T03:47:52.985387+00:00",
    "validatedResources": null
  },
  "tags": null,
  "type": "Microsoft.Resources/deployments"
}
$>

今回使ったARM templateとparameterファイル

{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.1",
    "parameters": {
        "rgName": {
            "type": "string"
        },
        "rgLocation": {
            "type": "string"
        },
        "tags": {
            "type": "object",
            "defaultValue": {}
        }
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[parameters('rgLocation')]",
            "name": "[parameters('rgName')]",
            "properties": {},
            "tags": "[parameters('tags')]"
        }
    ],
    "outputs": {}
}
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "rgName": {
            "value": "freeOnlyWestJ"
        },
        "rgLocation": {
            "value": "japanwest"
        },
        "tags": {
            "value": {
                "owner": "masatoshuw"
            }
        }
    }
}

コメント

タイトルとURLをコピーしました