Use Vagrant with Microsoft Azure

For several days, I’ve been playing with Vagrant, the popular tool that lets you create any environment based on virtual machines quickly and replicable. It’s an Open Source project that is compatible with Windows, Mac and Linux and that it really facilitates the life of the developer, worrying only about what really matters: the application. On the official website of Vagrant can verify that there are different providers (that will create your virtual machines): Virtual Box, VMware, Docker and AWS. Of course, there is also one for Microsoft Azure and in this post I’ll show how to install and create a virtual machine on Azure using Vagrant.

If you still don’t use it, install Vagrant from the downloads section on the official site. For this post I have used Mac OS X as operating system. Once installed, open a terminal and check that works by launching the command vagrant.

Vagrant command

The next thing is to install the provider for Azure. You don’t need anything more than run the following command from the terminal in order to begin the installation:

vagrant plugin install vagrant-azure

From here, you can already begin to design your project. Create a folder called azure_proyect and access to it so that will be your proyect’s location.

mkdir azure_proyect
cd azure_proyect

Once there you can begin to configure your box. There is a dummy box that will give you the skeleton of the Vagrantfile file on Github, which describes the type of machine that you are going to configure. Launch the following command to request the box:

vagrant box add azure https://github.com/msopentech/vagrant-azure/raw/master/dummy.box

Finally, to get the required file to configure your project launch the vagrant init command.

vagrant init

If you access to the folder where you defined your project for Vagrant (Terminal > open .) you will see that it is already available the Vagrantfile file that you need to configure your credentials for Microsoft Azure, how will be your virtual machine, which image are going to use, etc.

Vagrant file

Modify the file to replace the contents with the following:

Vagrant.configure('2') do |config|
 config.vm.box = 'azure'
 config.vm.provider :azure do |azure, override|
 # Mandatory Settings
 azure.mgmt_certificate = 'YOUR_CERTIFICATE.pfx'
 azure.mgmt_endpoint = 'https://management.core.windows.net'
 azure.subscription_id = 'YOUR_SUBSCRIPTION_ID'
 azure.vm_image = 'b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-15_04-amd64-server-20150224.5-beta1-en-us-30GB'
 azure.vm_name = 'ubuntugise' # max 15 characters. contains letters, number and hyphens. can start with letters and can end with letters and numbers
 # vm_password is optional when specifying the private_key_file with Linux VMs
 # When building a Windows VM and using WinRM this setting is used to authenticate via WinRM (PowerShell Remoting)
 azure.vm_password = 'YOUR_PASSWORD' # min 8 characters. should contain a lower case letter, an uppercase letter, a number and a special character
 # Optional Settings
 #azure.storage_acct_name = 'NAME OF YOUR STORAGE ACCOUNT' # optional. A new one will be generated if not provided.
 #azure.vm_user = 'PROVIDE A USERNAME' # defaults to 'vagrant' if not provided
 #azure.cloud_service_name = 'PROVIDE A NAME FOR YOUR CLOUD SERVICE' # same as vm_name. leave blank to auto-generate
 #azure.deployment_name = 'PROVIDE A NAME FOR YOUR DEPLOYMENT' # defaults to cloud_service_name
 azure.vm_location = 'West Europe' # e.g., West US
 # Optional *Nix Settings
 #azure.ssh_port = 'A VALID PUBLIC PORT' # defaults to 22
 #azure.private_key_file = 'Path to your ssh private key file (~/.ssh/id_rsa) to use for passwordless auth. If the id_rsa file is password protected, you will be prompted for the password.'
 # Optional Windows Settings
 #azure.winrm_transport = [ 'http', 'https' ] # this will open up winrm ports on both http (5985) and http (5986) ports
 #azure.winrm_https_port = 'A VALID PUBLIC PORT' # customize the winrm https port, instead of 5986
 #azure.winrm_http_port = 'A VALID PUBLIC PORT' # customize the winrm http port, insted of 5985
 #azure.tcp_endpoints = '3389:53389' # opens the Remote Desktop internal port that listens on public port 53389. Without this, you cannot RDP to a Windows VM.
 end
end

I’ve left all optional settings commented, so you can see at a glance all what can be configured. Now we will focus on those that are mandatory for you in this first test with Vagrant and Azure. According to the above script, we need several mandatory parameters:

  • azure.mgmt_certificate: it’s the certificate to connect to your subscription on Microsoft Azure. It can be in PEM, PFX or raw string format. To make it easy, download the file of publication of your Microsoft Azure account from here.
    <?xml version="1.0" encoding="utf-8"?>
    <PublishData>
      <PublishProfile SchemaVersion="2.0" PublishMethod="AzureServiceManagementAPI">
        <Subscription ServiceManagementUrl="https://management.core.windows.net" Id="THIS IS YOUR SUBSCRIPTION ID" Name="THIS IS YOUR SUBSCRIPTION NAME" ManagementCertificate="THIS IS YOUR CERTIFICATE" />
      </PublishProfile>
    </PublishData>

    The easiest way is to use ruby to generate the pfx using this file. To do this make sure that you have to install the azure library using the following command:

    gem install azure -v 0.7.0

    With pfxer you can create a pfx file using your publish settings with the following:

    pfxer transform --in PATH_PUBLISH_SETTINGS
  • azure.mgmt_endpoint:The value that is needed is https://management.core.windows.net, which is the endpoint of the administration APIs, so it is not necessary to change it.
  • azure.subscription_id: It is of the id of your subscription, you can find it in previous file.
  • azure.vm_image: The name of the image within the repository of Microsoft Azure you are going to use. Using Azure CLI you can retrieve thename of the images that you can use:
    azure vm image list

    Don’t panic, I know that the list is endless. You can filter them by using something like the following:

    azure vm image list | grep &amp;quot;Ubuntu-15_04&amp;quot;

    For this article I am going to use the one that has the name b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu-15_04-amd64-server-20150224.5-beta1-en-us-30GB.

  • azure.vm_name: name of your virtual machine.
  • azure.vm_password: the administrator password.
  • azure.vm_location: the region where you want to deploy your machine.

Once you have set all the parameters, you can simply launch the vagrant up command to create your new virtual machine through Vagrant:

vagrant up

vagrant up provider azure

Once the process is finished, you can communicate with your new virtual machine through vagrant ssh.
vagrant ssh

To complete the cycle, when you have finished working with it, you can remove it via the vagrant destroy command.

vagrant destroy azure

Cheers!