{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "workshopName": {
      "type": "string",
      "defaultValue": "workshop",
      "metadata": {
        "description": "Workshop name."
      }
    },
    "vmPrefix": {
      "type": "string",
      "defaultValue": "node",
      "metadata": {
        "description": "Prefix for VM names."
      }
    },
    "numberOfInstances": {
      "type": "int",
      "defaultValue": 3,
      "metadata": {
        "description": "Number of VMs to create."
      }
    },
    "adminUsername": {
      "type": "string",
      "defaultValue": "ubuntu",
      "metadata": {
        "description": "Admin username for VMs."
      }
    },
    "sshKeyData": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "SSH rsa public key file as a string."
      }
    },
    "imagePublisher": {
      "type": "string",
      "defaultValue": "Canonical",
      "metadata": {
        "description": "OS image publisher; default Canonical."
      }
    },
    "imageOffer": {
      "type": "string",
      "defaultValue": "UbuntuServer",
      "metadata": {
        "description": "The name of the image offer. The default is Ubuntu"
      }
    },
    "imageSKU": {
      "type": "string",
      "defaultValue": "18.04-LTS",
      "metadata": {
        "description": "Version of the image. The default is 18.04-LTS"
      }
    },
    "vmSize": {
      "type": "string",
      "defaultValue": "Standard_D1_v2",
      "metadata": {
        "description": "VM Size."
      }
    }
  },
  "variables": {
    "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
    "subnet1Ref": "[concat(variables('vnetID'),'/subnets/',variables('subnet1Name'))]",
    "vmName": "[parameters('vmPrefix')]",
    "sshKeyPath": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]",
    "publicIPAddressName": "PublicIP",
    "publicIPAddressType": "Dynamic",
    "virtualNetworkName": "MyVNET",
    "netSecurityGroup": "MyNSG",
    "addressPrefix": "10.0.0.0/16",
    "subnet1Name": "subnet-1",
    "subnet1Prefix": "10.0.0.0/24",
    "nicName": "myVMNic"
  },
  "resources": [
    {
      "apiVersion": "2017-11-01",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[concat(variables('publicIPAddressName'),copyIndex(1))]",
      "location": "[resourceGroup().location]",
      "copy": {
        "name": "publicIPLoop",
        "count": "[parameters('numberOfInstances')]"
      },
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]"
      },
      "tags": {
        "workshop": "[parameters('workshopName')]"
      }
    },
    {
      "apiVersion": "2017-11-01",
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[variables('virtualNetworkName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkSecurityGroups/', variables('netSecurityGroup'))]"
      ],
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('addressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[variables('subnet1Name')]",
            "properties": {
              "addressPrefix": "[variables('subnet1Prefix')]",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('netSecurityGroup'))]"
              }
            }
          }
        ]
      },
      "tags": {
        "workshop": "[parameters('workshopName')]"
      }
    },
    {
      "apiVersion": "2017-11-01",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[concat(variables('nicName'),copyIndex(1))]",
      "location": "[resourceGroup().location]",
      "copy": {
        "name": "nicLoop",
        "count": "[parameters('numberOfInstances')]"
      },
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'),copyIndex(1))]",
        "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('publicIPAddressName'), copyIndex(1)))]"
              },
              "subnet": {
                "id": "[variables('subnet1Ref')]"
              }
            }
          }
        ]
      },
      "tags": {
        "workshop": "[parameters('workshopName')]"
      }
    },
    {
      "apiVersion": "2017-12-01",
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[concat(variables('vmName'),copyIndex(1))]",
      "location": "[resourceGroup().location]",
      "copy": {
        "name": "vmLoop",
        "count": "[parameters('numberOfInstances')]"
      },
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'), copyIndex(1))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[concat(variables('vmName'),copyIndex(1))]",
          "adminUsername": "[parameters('adminUsername')]",
          "linuxConfiguration": {
            "disablePasswordAuthentication": true,
            "ssh": {
              "publicKeys": [
                {
                  "path": "[variables('sshKeyPath')]",
                  "keyData": "[parameters('sshKeyData')]"
                }
              ]
            }
          }
        },
        "storageProfile": {
          "osDisk": {
            "createOption": "FromImage"
          },
          "imageReference": {
            "publisher": "[parameters('imagePublisher')]",
            "offer": "[parameters('imageOffer')]",
            "sku": "[parameters('imageSKU')]",
            "version": "latest"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nicName'),copyIndex(1)))]"
            }
          ]
        }
      },
      "tags": {
        "workshop": "[parameters('workshopName')]"
      }
    },
    {
      "apiVersion": "2017-11-01",
      "type": "Microsoft.Network/networkSecurityGroups",
      "name": "[variables('netSecurityGroup')]",
      "location": "[resourceGroup().location]",
      "tags": {
        "workshop": "[parameters('workshopName')]"
      },
      "properties": {
        "securityRules": [
          {
            "name": "default-open-ports",
            "properties": {
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "*",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 1000,
              "direction": "Inbound"
            }
          }
        ]
      }
    }
  ],
  "outputs": {
    "resourceID": {
      "type": "string",
      "value": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('publicIPAddressName'),'1'))]"
    }
  }
}
