Deploy Azure VM using JSON template and PowerShell

Azure Resource Manager templates are JavaScript Object Notation (JSON) files that define the infrastructure and configuration for your resources. In this case a virtual machine.

Steps in brief

  1. Get the virtual machine template files from a known good deployment within Azure portal
  2. Edit the parameters.json file
  3. Run the deployment in PowerShell

Get the template files

There are several ways to get a template file for your deployments. Such as creating it from scratch, GitHub , or the Azure portal. Here, we are using Azure portal to download two files: template.json and parameters.json.

In the Azure portal go to the virtual machine you want to copy. Then click on the “Export Template” menu and then “Download”.

Download JSON template files

Edit the parameters.json file

The template files can be edited to suit your needs, however, the point of this exercise is to reuse an existing deployment. Looking in the parameters.json file, you’ll see most of the parameters & values are self-explanatory and can stay as is. However to avoid name collisions you’ll need to change only a few values.

These three below relate to the Name of the VM and should be unique:

  • networkInterfaceName
  • publicIpAddressName
  • virtualMachineName

You may notice the the value for the Administrator’s password or public key is “Null”. Azure does not export it in the template file so you must add it back in for the new deployment.

  • adminPublicKey
Edit the parameters file

Deploy using Powershell

Now we are ready to deploy a new virtual machine based on the template files.

  • Using variables in the command line is optional although quite useful for scripting later on.
  • If you don’t give the “DeploymentName” it’ll default to something like “template”. I recommend using a name that’ll make sense when you see it in the Azure portal.

Open up Powershell and do the following:

$resourceGroupName = "DemoRG"

$templateFilePath = "/path/to/template.json"

$parametersFilePath = "/path/to/parameters.json"

New-AzResourceGroupDeployment -DeploymentName MyTemplateDeployTests `
 -ResourceGroupName $resourceGroupName `
 -TemplateFile $templateFilePath `
 -TemplateParameterFile $parametersFilePath

After a few minutes you’ll see some details about the deployment like:

DeploymentName          : MyTemplateDeployTests
ResourceGroupName       : DemoRG
ProvisioningState       : Succeeded

.. and so on

Verify the deployment

If you want, here are steps to get the IP address and then SSH into the VM to look around. FYI, this is a Linux VM.

Get the VM public IP address then login using SSH. This will only work if the template is configured with Network Security Group that allows port 22 OR there is a Network Security Group applied to the selected “subnetName” OR similar.

Get the IP address

In the output of this command will be the VM’s public IP address. Note, $resourceGroupName is reused from previous steps. Replace “virtualMachineName” with the VM name you used in the parameters file.

Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Name <VM Name>  | select IpAddress

OR This

Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName | Where-Object {$_.name -like "*<VM Name>*" }

OR This

Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName | Where-Object {$_.name -like "*<VM Name>*" } | Select-Object { $_.IpAddress }

Login to the VM

Goto to another terminal window and SSH into the VM. In this case, I am using my private key. Remember the public key was added to the parameters.json file.

ssh -i .ssh/id_rsa admin@192.168.1.1
Success !!

References