Baking machine images with HashiCorp Packer.

2017-03-15

This post covers how to programatically build Virtualbox and AWS images with Packer


Why packer?

Throughout my career I’ve built countless golden images hacking and whacking disparate and sometimes inconsistent solutions. Few years ago I come across Packer, another great tool developed by HashiCorp, the same folks behind Vagrant, Terraform,Vault and more. For me, Packer had few winning points that made it stand out, it was easy to learn, it was multi provider (AWS,Virtualbox,VMWare) and has satisfied three basic principles, consistency, repeatability and reusability.

Requirements.

In order to complete this walk-through you need to have Packer, Virtualbox and Vagrant installed, the installation of such tools is not in the scope of this tutorial. Please find the installation guides from the below links:

https://www.virtualbox.org

https://www.vagrantup.com

https://www.packer.io/

Setup and Demo.

To ease the demo, please clone the following git repo containing a catalog of Packer templates.

git clone https://github.com/p0bailey/packer-templates

cd packer-templates/

Choose you favourite Linux distributions among the following:

centos-6.8 - centos-7.3 - debian-8 - ubuntu-14.04 - ubuntu-16.04

E.g. use ubuntu-16.04.

cd ubuntu-16.04

To build an ubuntu-16.04 Vagrant box type:

make virtualbox

To test the newly created box type:

make vagrant_up

To cleanup the previous environment type:

make vagrant_clean

Provisioning.

These templates are provisioned with Ansible 2.2 installed via Pip. Whether you’d like to add additional configurations of packages please refer to the below snippet.

E.g. ubuntu-16.04/scripts/provision.sh

#!/bin/sh -eux
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get -y install python-pip python-dev build-essential libffi-dev libssl-dev git
sudo pip install --upgrade pip
sudo pip install ansible
sudo pip install --upgrade setuptools

### Add your code below.

I hope you enjoyed this tutorial, I’ll try my best to write another one about baking AWS EC2 instances with Packer and Terraform.

Bye,

Phillip