Azure + Packer

Yes, it works. How cool is that?
The hardest part is to set up your environment and even that is easy. Kudos to MS.

What you’ll need: a linux machine, golang, packer, Azure plugin for packer, nodejs, Azure cli tools and an Azure subscription.

I’ll start with a clean Ubuntu 14.04 machine.
Let’s start by installing node, npm, git and mercurial (which you’ll need to install the Azure packer plugin).

sudo apt-get install node npm git mercurial  

In Ubuntu node binary is called nodejs because there’s another package called node. One quick fix is to symlink nodejs to node.

sudo ln -s /usr/bin/nodejs /usr/bin/node  

Then we need to install Azure CLI tools. For this we use npm:

sudo npm -g install azure-cli  

And packer. But in order to use Packer and the Azure plugin for Packer you first have to have a recent version of Go (>=1.4).

wget https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz  

Unpack it somewhere. I use ~/opt/go
Now we set it up

export PATH=$HOME/opt/go/bin:$PATH  
export GOROOT=$HOME/opt/go  
export GOPATH=$HOME/go  

You might want to add these to your shell settings (like .bashrc).
Test by running go version. You should see something like:

fx@azure-playground:~$ go version  
go version go1.5.1 linux/amd64  

Download packer from packer.io and unpack it:

wget https://dl.bintray.com/mitchellh/packer/packer_0.8.6_linux_amd64.zip  
unzip packer_0.8.6_linux_amd64.zip -d packer  

Now, install the Azure plugin for Packer

cd packer  
# the built plugin will be placed in current directory
export GOBIN=$PWD  
go get github.com/MSOpenTech/packer-azure/packer/plugin/...  

Now, set up Azure CLI if you haven’t done so already.
This is as easy as

azure account download  

This will give you a link to download your settings file. This will have to be done using a real browser. Once you have it, set it up:

azure account import ~/azuresubscription.publishsettings  

In order to use packer, you need a storage account:

azure storage account create packer -l "North Europe" -d "My Packer images" --type GRS  

Now is the moment of truth, if everything went well up to now then you should be able to create a packer image.
Let’s create a very minimal packer json file:

{
  "variables": {
    "sn": "Windows Azure MSDN - Visual Studio Ultimate",
    "ps": "/home/fx/azuresubscription.publishsettings",
    "sa": "packer"
  },
  "builders": [
    {
      "type": "azure",
      "publish_settings_path": "{{user `ps`}}",
      "subscription_name": "{{user `sn`}}",
      "storage_account": "{{user `sa`}}",
      "storage_account_container": "images",
      "os_type": "Linux",
      "os_image_label": "Ubuntu Server 14.04 LTS",
      "location": "North Europe",
      "instance_size": "Small",
      "user_image_label": "Ubuntu1404LTS"
    }
  ],
  "provisioners": [
    {
      "execute_command": "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'",
      "inline": [
        "sudo apt-get update",
        "sudo apt-get -y upgrade"
      ],
      "inline_shebang": "/bin/sh -x",
      "type": "shell"
    }
  ]
}

You can see more examples on github.
Save it as ubuntu.json and check for errors:

./packer validate ubuntu.json

If no errors were reported, go ahead and build it:

./packer build ubuntu.json

Once the build is done you’ll see something like

...
==> Builds finished. The artifacts of successful builds are:
--> azure: {imageLabel: 'Ubuntu1404LTS',imageName: 'Ubuntu1404LTS_2015-09-27_13-55',mediaLocation: 'https://packer.blob.core.windows.net/images/Ubuntu1404LTS_2015-09-27_13-55-os-2015-09-27.vhd'}

Confirm by running:

azure vm image show Ubuntu1404LTS_2015-09-27_13-55  

Congrats, you have your first Packer image on Azure