Use PowerShell to Create a Linux VM in Azure
In a couple of my previous blog articles, I've demonstrated how to create a storage account in Azure and how to create a reserved virtual IP address in Azure. Both of those items will be used in today's blog article so I recommend reading through those previous blog articles if you haven't already done so.
The goal in this blog article is to build a CentOS based OpenLogic 7.0 VM in Azure except using PowerShell instead using the Azure portal website (GUI):
First, the name of the image that Azure uses to build those VM's will need to be determined. This
can be accomplished using the
2Where-Object ImageName -like *centos* |
3Select-Object -Property ImageName
The last one in the previous set of results looks like the image that's used to create the CentOS based OpenLogic 7.0 VM's on Azure.
Just to be sure, let's take a look at the details of that particular image:
1Get-AzureVMImage -ImageName 5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-70-20150128
Yep. That's the image. You can also see what locations that particular image is available in as shown in the previous results.
You could also return a list of the locations where that image is available in a more readable format by selecting only the location property, splitting on the semicolon, and sorting by name:
1(Get-AzureVMImage -ImageName 5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-70-20150128).Location -split ';' |
Now to create a cloud service in Azure for our new VM:
1New-AzureService -ServiceName 'mikefrobbins-test' -Location 'South Central US'
I've used the process shown in this article on Azure to create SSH keys for my Linux VM. I didn't install CYGWIN as that article suggested. I followed the process here to install and configure OpenSSL and I used Win32 OpenSSL which can be downloaded from here. I also installed Putty which the article on Azure that I previously referenced suggested.
The public key certificate that was created as shown in that previously referenced Azure article needs to be uploaded to the cloud service that was previously created. It also needs to be added to the user's account on the Linux VM that we'll be creating:
1$certificate = 'C:\tmp\mikefrobbinsCert.cer'
2Add-AzureCertificate -CertToDeploy $certificate -ServiceName 'mikefrobbins-test'
3$sshkey = New-AzureSSHKey -PublicKey -Fingerprint (Get-PfxCertificate -FilePath $certificate).Thumbprint -Path '/home/mrtest/.ssh/authorized_keys'
If you haven't already associated a storage account with your Azure subscription, that will need to be accomplished prior to creating the VM, otherwise you'll receive an error message stating that a default storage account hasn't been configured or to specify a media location for the VM. This storage account must reside in the same data-center as the VM.
1Set-AzureSubscription -SubscriptionId 'abcdefgh-1234-1a23-a123-ab123456c78c' -CurrentStorageAccountName 'mikefrobbinsstorage'
Create an Azure VM config using the
New-AzureVMConfig cmdlet, pipe that to
Add-AzureProvisioningConfig to set some of the additional options, and then pipe that to
New-AzureVM to create the actual VM:
1New-AzureVMConfig -ImageName '5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-70-20150128' -InstanceSize 'Basic_A1' -Name 'mrtestvm01' |
2Add-AzureProvisioningConfig -Linux -LinuxUser 'mrtest' -NoSSHPassword -SSHPublicKeys $sshKey |
3New-AzureVM -ServiceName 'mikefrobbins-test' -ReservedIPName 'mikefrobbinsIP' -Location 'South Central US'
Be patient, the previous command will take a couple of minutes to complete.
I've opened the Putty SSH client and logged into the new VM that was created in the prevous step. I'll verify the version of CentOS that it's running:
If you're looking for a client to transfer files to/from your new Linux VM, I'd recommend WinSCP. It's easy to setup and use and it supports using certificates.