Rossini
Rossini

Reputation: 6049

How I create a VM from a custom Image using Azure Resource Manager

I've been using the Azure Classic portal for a while and I'm able to create VMs from my custom images w/o a problem. Now I'm trying to use the new portal, when I go to create a new VM I don't see the option to use my images. How do I create a VM from one of my images?

Upvotes: 1

Views: 1918

Answers (3)

VIJAY RAAVI
VIJAY RAAVI

Reputation: 406

Copy your Custom Images from Azure Classic portal to new Portal ( Here everything is stored under a Resource Group) Storage Account.

Now you will be able to Create new VM using your Custom images through Azure CLI or PowerShell or ARM Template.

Upvotes: 0

Aatif Akhter
Aatif Akhter

Reputation: 2206

Here is how to do it... Assuming your Resource group is created and my infra config are follows-

custom template uri path- https://myvmstore.blob.core.windows.net/vhds/CustomVHD.vhd  
RG Name - myVMsRG 
VNet Name - myVNET 
VmName = mytestvm  
userImageStorageAccountName = myvmstore 
adminUsername = adminuser  
adminPassword = PassWord123#  
osDiskVhdUri = https://myvmstore.blob.core.windows.net/vhds/CustomVHD.vhd 
dnsLabelPrefix = mytestvm 
osType = Windows  
vmSize = Standard_D2  
newOrExistingVnet = existing 
newOrExistingVnetName = myVNET  
newOrExistingSubnetName = mySubnet

First you need json template. I have configured for myself in this format. You can copy the same code and save with name deployvm.json in your D drive. Note: If your VNET name is different from myVNET please hard code the same in the template at the same position.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "customVmName": {
      "type": "string",
      "metadata": {
        "description": "This is the name of the your VM"
      }
    },
    "userImageStorageAccountName": {
      "type": "string",
      "metadata": {
        "description": "This is the name of the your storage account"
      }
    },
    "osDiskVhdUri": {
      "type": "string",
      "metadata": {
        "description": "Uri of the your user image"
      }
    },
    "dnsLabelPrefix": {
      "type": "string",
      "metadata": {
        "description": "DNS Label for the Public IP. Must be lowercase. It should match with the following regular expression: ^[a-z][a-z0-9-]{1,61}[a-z0-9]$ or it will raise an error."
      }
    },
    "adminUserName": {
      "type": "string",
      "metadata": {
        "description": "User Name for the Virtual Machine"
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Password for the Virtual Machine"
      }
    },
    "osType": {
      "type": "string",
      "allowedValues": [
        "Windows",
        "Linux"
      ],
      "metadata": {
        "description": "This is the OS that your VM will be running"
      }
    },
    "vmSize": {
      "type": "string",
      "metadata": {
        "description": "This is the size of your VM"
      }
    },
    "newOrExistingVnet": {
      "allowedValues": [ "new", "existing" ],
      "type": "string",
      "metadata": {
        "description":  "Select if this template needs a new VNet or will reference an existing VNet"
      }
    },
    "newOrExistingVnetName": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "New or Existing VNet Name"
      }
    },
    "newOrExistingSubnetName": {
      "type": "string",
      "defaultValue": "Subnet1",
      "metadata": {
        "description": "Subnet Name"
      }
    }
  },
  "variables": {
    "publicIPAddressName": "[parameters('customVmName')]",
    "vmName": "[parameters('customVmName')]",
    "nicName": "[parameters('customVmName')]",
    "publicIPAddressType": "Dynamic",
    "apiVersion": "2015-06-15",
    "vnetID": "[resourceId('myVNET', 'Microsoft.Network/virtualNetworks', parameters('newOrExistingVnetName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/', parameters('newOrExistingSubnetName'))]",
},
  "resources": [
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('publicIPAddressName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsLabelPrefix')]"
        }
      }
    },
    {
      "apiVersion": "2016-03-30",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[variables('nicName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
              },
              "subnet": {
                "id": "[variables('subnetRef')]"
              }
            }
          }
        ]
      }
    },
    {
      "apiVersion": "[variables('apiVersion')]",
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[variables('vmName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
      ],
      "properties": {
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computerName": "[variables('vmName')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "osDisk": {
            "name": "[concat(variables('vmName'),'-osDisk')]",
            "osType": "[parameters('osType')]",
            "caching": "ReadWrite",
            "createOption": "FromImage",
            "image": {
              "uri": "[parameters('osDiskVhdUri')]"
            },
            "vhd": {
              "uri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', parameters('userImageStorageAccountName')), variables('apiVersion')).primaryEndpoints.blob, 'vhds/',variables('vmName'), uniquestring(resourceGroup().id), 'osDisk.vhd')]"
            }
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',variables('nicName'))]"
            }
          ]
        },
        "diagnosticsProfile": {
          "bootDiagnostics": {
            "enabled": "true",
            "storageUri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', parameters('userImageStorageAccountName')), variables('apiVersion')).primaryEndpoints.blob)]"
          }
        }
      }
    }
  ]
}

Now use below powershell code to create your VM-

$paramList = 
     @{
        "Params1" = @{ customVmName = "mytestvm" ; userImageStorageAccountName = "myvmstore" ; adminUsername = "adminuser" ; adminPassword = "PassWord123#" ; osDiskVhdUri = "https://myvmstore.blob.core.windows.net/vhds/CustomVHD.vhd" ; dnsLabelPrefix = "mytestvm" ; osType ="Windows" ; vmSize = "Standard_D2" ; newOrExistingVnet = "existing" ;  newOrExistingVnetName = "myVPN" ; newOrExistingSubnetName = "mySubnet"}
       }

        foreach ($keys in $paramList.Keys)
        {   
            $paramvalues = $paramList.$keys
            New-AzureRmResourceGroupDeployment -ResourceGroupName "myVMsRG" -TemplateFile "D:\deployvm.json" -TemplateParameterObject $paramValues   
        }

Reference- https://github.com/Azure/azure-quickstart-templates/tree/master/101-vm-from-user-image

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72151

You could use Powershell for that or an ARM Template. There is really too much code to paste here, but even if the links I post will change, its easily searchable.

Upvotes: 0

Related Questions