Part 1: Creating Azure Virtual Machines Using PowerShell

Before I delve into the PowerShell script to create Azure Virtual Machines I want to talk about the relationship between virtual machines and cloud services. This can be a source of confusion and I think it is not so obvious when you look at the Azure portal.

When you create a web role, worker role or VM it is created inside a cloud service. A cloud service is a container that contains roles and a role contains instances. For example you can have a web role that contains 3 instances, in reality this will mean you have 3 windows servers running IIS inside this web role. Being a web role you have some access to the underlying OS but you don’t see the 3 VMs running as you do with compute instances but they are still there, you just see them as web role instances.

When you create a VM it will also reside inside a cloud service as a role but you don’t see a role called virtual machines inside your cloud service, you just see the instances. When you create a new VM you have the option to create a new cloud service or adding this VM to an existing cloud service. The default behaviour is to create a new cloud service and you end up with a 1 to 1 mapping between VMs and cloud services. This works great if you don’t have a need for your VMs to communicate with each other on the private network and it makes mapping endpoints easier since there is only one instance in the cloud service using the ports.

If you look at a cloud service you will see it has a DNS name and the instances inside the cloud service all use this DNS name. The instances have private IP addresses so they can communicate which each other over the private network in the cloud service. If you for instance have 3 web roles the load balancer will redirect port 80 traffic to these 3 web role instances and all 3 will be handling the HTTP requests, great for scaling out your web app. If you have multiple VMs in the cloud service you will see the load balancer setup endpoints mapping external ports to the internal ports on your VM instances.

If you for instance want to remote desktop into these VMs you have the use the port that the load balancer assigned for that VM, by default it will automatically choose a port for RDP but you can specify one if you want. To change the ports you click on the endpoints tab for that VM under the Virtual Machines section in the portal. Remember you can’t just choose any port, you have to make sure the port is not in use already by another instance in that cloud service.

When you create a cloud service you choose the region to host this cloud service, if you then add more VMs to this cloud service you can’t change the location for the VM, it is inherited from the cloud service.

Next time I’ll show the PowerShell script to create a VM.

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *