2016年5月17日火曜日

githubのAzure Resource Manager テンプレートをカスタマイズしてみた - Azure Virtual Machine with RDP connection 記事訂正

githubのAzure Resource Manager テンプレート https://github.com/Azure/azure-quickstart-templatesは、ご存じだと思います。

自分の勉強もかねて、テンプレートをカスタマイズしてみたので、参考までに公開します。
もっと洗練された方法があればコメントもらえるとありがたいです。

githubのAzure Resource Manager テンプレートをカスタマイズしてみた - Azure Virtual MachineにRDP接続用のエンドポイントを追加してみました。
元ネタは、
Azure Resource Manager Template Visualizerを開いたときに既定で表示される複数NICのテンプレートです。

githubのAzure Resource Manager テンプレートをカスタマイズしてみた - 仮想ネットワークgithubのAzure Resource Manager テンプレートをカスタマイズしてみた - ストレージアカウントが先にデプロイされている前提です。
※2016/05/17 確認したところ、本記事のJSONファイルでは、仮想ネットワークは都度作成(つまり上書き)されることがわかりましたので、訂正します。
 githubのAzure Resource Manager テンプレートをカスタマイズしてみた - Azure Virtual Machine 記事訂正 に記載した通り、仮想ネットワークのフルセット記述不要です。既存の仮想ネットワークに関連付ける方法は調べたうえで、別途ブログ記事として投稿しようと思います。
 同様にストレージアカウントも関連付けの設定がありそうか否かを確認しようと思います。


githubのAzure Resource Manager テンプレートをカスタマイズしてみた - Azure Virtual Machineで公開したJSONファイルとのdiffをbash on Ubuntu on Windowsで取りました(しつこい)!

diff armTemplate-simplewindows-modified.json armTemplate-simplewindows-modified-withRDP.json
39a40,46
>     },
>     "rdpFrontend": {
>       "type": "int",
>       "defaultValue": 50001,
>       "metadata": {
>         "description": "Public port number for RDP"
>       }
47a55,56
>     "vmName": "[parameters('vmName')]",
>     "vmSize": "Standard_D1",
57,58d65
<     "vmName": "[parameters('vmName')]",
<     "vmSize": "Standard_D1",
61a69,77
>     "lbName": "[concat(parameters('vmName'),'-LB')]",
>     "lbInboundNatRulesName": "[concat(parameters('vmName'),'-lbInboundNatRules01')]",
>     "lbFrontEndName": "[concat(parameters('vmName'),'-lbFrontEnd')]",
>     "lbBackendAddressPoolName": "[concat(parameters('vmName'),'-lbBackendPool1')]",
>     "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
>     "lbID": "[resourceId('Microsoft.Network/loadBalancers',variables('lbName'))]",
>     "lbFrontEndIPConfigID": "[concat(variables('lbID'),'/frontendIPConfigurations/',variables('lbFrontEndName'))]",
>     "lbPoolID": "[concat(variables('lbID'),'/backendAddressPools/',variables('lbBackendAddressPoolName'))]",
>     "lbNatID": "[concat(variables('lbID'),'/inboundNatRules/',variables('lbInboundNatRulesName'))]",
77c93
<       "name": "myPublicIP",
---
>       "name": "[variables('publicIPAddressName')]",
87a104,143
>       "name": "[variables('lbName')]",
>       "type": "Microsoft.Network/loadBalancers",
>       "location": "[resourceGroup().location]",
>       "dependsOn": [
>         "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
>       ],
>       "properties": {
>         "frontendIPConfigurations": [
>           {
>             "name": "[variables('lbFrontEndName')]",
>             "properties": {
>               "publicIPAddress": {
>                 "id": "[variables('publicIPAddressID')]"
>               }
>             }
>           }
>         ],
>         "backendAddressPools": [
>           {
>             "name": "[variables('lbBackendAddressPoolName')]"
>           }
>         ],
>         "inboundNatRules": [
>           {
>             "name": "[variables('lbInboundNatRulesName')]",
>             "properties": {
>               "frontendIPConfiguration": {
>                 "id": "[variables('lbFrontEndIPConfigID')]"
>               },
>               "protocol": "tcp",
>               "frontendPort": "[parameters('rdpFrontend')]",
>               "backendPort": 3389,
>               "enableFloatingIP": false
>             }
>           }
>         ]
>       }
>     },
>     {
>       "apiVersion": "[variables('apiVersion')]",
113,114c169,170
<         "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
<         "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
---
>         "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
>         "[concat('Microsoft.Network/loadBalancers/',variables('lbName'))]"
122,124d177
<               "publicIPAddress": {
<                 "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
<               },
127c180,190
<               }
---
>               },
>               "loadBalancerBackendAddressPools": [
>                 {
>                   "id": "[variables('lbPoolID')]"
>                 }
>               ],
>               "loadBalancerInboundNatRules": [
>                 {
>                   "id": "[variables('lbNatID')]"
>                 }
>               ]
  • 外向けのRDPポート指定するためのparametersを追加。
  • 外向けRDPポートと3389をマッピングするための記述を追加。
  • 外向けのRDPポートのために、Microsoft.Network/loadBalancersを追加し、NICのdependsOnはMicrosoft.Network/virtualNetworks/をそのままとしつつ、Microsoft.Network/publicIPAddresses/のかわりにMicrosoft.Network/loadBalancers/を入れる。
    代わりにMicrosoft.Network/loadBalancers/のdependsOnはMicrosoft.Network/publicIPAddresses/を入れる。
  • resourcesのパブリックIPアドレスの名前をVM名由来に変更。
  • resourcesにあったidのconcatが可読性を損なっているので、valiablesへ移動。
といった感じでカスタマイズし直しました。 Azure Resource Manager Template Visualizerで表示するとこういう感じ。
ということで、JSONのソースファイルを貼っておきます。 ※Azure Resource Managerでデプロイ確認済みです。
{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmName": {
      "type": "string",
      "metadata": {
        "description": "name for the Virtual Machine."
      }
    },
    "adminUsername": {
      "type": "string",
      "metadata": {
        "description": "Username for the Virtual Machine."
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Password for the Virtual Machine."
      }
    },
    "dnsLabelPrefix": {
      "type": "string",
      "metadata": {
        "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
      }
    },
    "windowsOSVersion": {
      "type": "string",
      "defaultValue": "2012-R2-Datacenter",
      "allowedValues": [
        "2008-R2-SP1",
        "2012-Datacenter",
        "2012-R2-Datacenter"
      ],
      "metadata": {
        "description": "The Windows version for the VM. This will pick a fully patched image of this given Windows version. Allowed values: 2008-R2-SP1, 2012-Datacenter, 2012-R2-Datacenter."
      }
    },
    "rdpFrontend": {
      "type": "int",
      "defaultValue": 50001,
      "metadata": {
        "description": "Public port number for RDP"
      }
    }
  },
  "variables": {
    "storageAccountName": "armstorageact01",
    "sizeOfDiskInGB": "100",
    "dataDisk1VhdName": "[concat(variables('vmName'),'-data1')]",
    "imagePublisher": "MicrosoftWindowsServer",
    "imageOffer": "WindowsServer",
    "vmName": "[parameters('vmName')]",
    "vmSize": "Standard_D1",
    "OSDiskName": "[concat(variables('vmName'),'-os')]",
    "nicName": "[concat(variables('vmName'),'-nic1')]",
    "addressPrefix": "10.0.0.0/16",
    "subnetName": "Subnet1",
    "subnetPrefix": "10.0.0.0/24",
    "storageAccountType": "Standard_LRS",
    "publicIPAddressName": "[concat(variables('vmName'),'-PublicIP')]",
    "publicIPAddressType": "Dynamic",
    "vmStorageAccountContainerName": "vhds",
    "virtualNetworkName": "ArmVNet1",
    "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
    "lbName": "[concat(parameters('vmName'),'-LB')]",
    "lbInboundNatRulesName": "[concat(parameters('vmName'),'-lbInboundNatRules01')]",
    "lbFrontEndName": "[concat(parameters('vmName'),'-lbFrontEnd')]",
    "lbBackendAddressPoolName": "[concat(parameters('vmName'),'-lbBackendPool1')]",
    "publicIPAddressID": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]",
    "lbID": "[resourceId('Microsoft.Network/loadBalancers',variables('lbName'))]",
    "lbFrontEndIPConfigID": "[concat(variables('lbID'),'/frontendIPConfigurations/',variables('lbFrontEndName'))]",
    "lbPoolID": "[concat(variables('lbID'),'/backendAddressPools/',variables('lbBackendAddressPoolName'))]",
    "lbNatID": "[concat(variables('lbID'),'/inboundNatRules/',variables('lbInboundNatRulesName'))]",
    "apiVersion": "2015-06-15"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[variables('storageAccountName')]",
      "apiVersion": "[variables('apiVersion')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "accountType": "[variables('storageAccountType')]"
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('publicIPAddressName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsLabelPrefix')]"
        }
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "name": "[variables('lbName')]",
      "type": "Microsoft.Network/loadBalancers",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
      ],
      "properties": {
        "frontendIPConfigurations": [
          {
            "name": "[variables('lbFrontEndName')]",
            "properties": {
              "publicIPAddress": {
                "id": "[variables('publicIPAddressID')]"
              }
            }
          }
        ],
        "backendAddressPools": [
          {
            "name": "[variables('lbBackendAddressPoolName')]"
          }
        ],
        "inboundNatRules": [
          {
            "name": "[variables('lbInboundNatRulesName')]",
            "properties": {
              "frontendIPConfiguration": {
                "id": "[variables('lbFrontEndIPConfigID')]"
              },
              "protocol": "tcp",
              "frontendPort": "[parameters('rdpFrontend')]",
              "backendPort": 3389,
              "enableFloatingIP": false
            }
          }
        ]
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[variables('virtualNetworkName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('addressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[variables('subnetName')]",
            "properties": {
              "addressPrefix": "[variables('subnetPrefix')]"
            }
          }
        ]
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[variables('nicName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
        "[concat('Microsoft.Network/loadBalancers/',variables('lbName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[variables('subnetRef')]"
              },
              "loadBalancerBackendAddressPools": [
                {
                  "id": "[variables('lbPoolID')]"
                }
              ],
              "loadBalancerInboundNatRules": [
                {
                  "id": "[variables('lbNatID')]"
                }
              ]
            }
          }
        ]
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[variables('vmName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
        "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[variables('vmSize')]"
        },
        "osProfile": {
          "computerName": "[variables('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('imagePublisher')]",
            "offer": "[variables('imageOffer')]",
            "sku": "[parameters('windowsOSVersion')]",
            "version": "latest"
          },
          "osDisk": {
            "name": "osdisk",
            "vhd": {
              "uri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('OSDiskName'),'.vhd')]"
            },
            "caching": "ReadWrite",
            "createOption": "FromImage"
          },
          "dataDisks": [
            {
              "name": "datadisk1",
              "diskSizeGB": "[variables('sizeOfDiskInGB')]",
              "lun": 0,
              "vhd": {
                "uri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/',variables('dataDisk1VhdName'),'.vhd')]"
              },
              "createOption": "Empty"
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": "true",
            "storageUri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net')]"
          }
        }
      }
    }
  ]
}

0 件のコメント:

コメントを投稿