Getting Started with Packer

Sylia CHIBOUB
4 min readJun 29, 2020
Photos via Pexels

Introduction

Based on Packer’s documentation, Packer is defined as an open source tool for creating identical machine images for multiple platforms from a single source configuration.

A machine image is a single static unit that contains a pre-configured operating system and installed software which is used to quickly create new running machines.

There are many advantages of using packer :

  • Super fast infrastructure deployment. Packer images allow you to launch completely provisioned and configured machines in seconds,
  • Multi-provider portability. Because Packer creates identical images for multiple platforms, you can use those images to run your machines on any Cloud Provider plateform.
  • Improved stability. Packer installs and configures all the software for a machine at the time the image is built.
  • Greater testability. After a machine image is built, that machine image can be quickly launched and smoke tested to verify that things appear to be working. If they are, you can be confident that any other machines launched from that image will function properly.

The following Figure describes how packer build your machine imagine

Building a Machine Image with Packer

Install Packer

For this tutorial, i am using Ubuntu 18.04.

First, create ~/packer which is the directory where packer will be installed

wget https://releases.hashicorp.com/packer/1.5.5/packer_1.5.5_linux_amd64.zip
apt-get install unzip
unzip packer_1.5.5_linux_amd64.zip
rm -r packer_1.5.5_linux_amd64.zip

Next, you need to export the directory’s path to ~/.profile

echo 'export PATH="$PATH:~"' >> .profile

Then source ~/.profile

source ~/.profile

You can verify your installation by running the following command

packer

If you get an error that packer could not be found, then your PATH environment variable was not setup properly. Otherwise, Packer is installed and you’re ready to go !

Packer Terminology

The Template

The configuration file used to define what image we want built and how is called a template. The format of a template is JSON.

Templates are given to commands such as packer build, which will take the template and actually run the builds within it, producing any resulting machine images.

A template has a set of keys configuring various components of Packer. The main keys within a template are Builders and Provisioners

Builders

Builders are responsible for creating machines and generating images from them for various platforms. There are separate builders for each Cloud Provider, each with its own configuration. Flexible Engine uses the OpenStack Builder. To learn more about an individual builder check out Packer’s offcial Documentation. To Find out more about using Packer with Flexible Engine check out the Flexible Engine Documentation

Provisioners

Provisioners are used to install and configure the machine image after booting. they prepare the system for use through for example installing the necessary packages. Check out Template Provisioners to learn more about working with provisioners.

Build a Packer Image

As i said before, for this tutorial, i am using Flexible Engine Cloud Provider that uses the OpenStack Builder. You can use any Provider you want.

Define your Variables

First, we need to define our variables. Let’s create a file variable.shand fill it with the following contents:

OS_USERNAME and OS_PASSWORD represent your username and password to access your Cloud Provider Console.

OS_REGION represent the region where your resources are going to be deployed. Find more at Flexible Engine Documentation. You can find your authorized regions at your Cloud Provider’s Console.

OS_AUTH_URLrepresent the Identity and Access Management URL, it can be found at Flexible Engine Documentation.

OS_TENANT_NAME represents your project name.

OS_DOMAIN_NAME is your ID.

Both the OS_TENANT_NAME and OS_DOMAIN_NAMEcan be found at your Cloud Provider’s Console.

SSH_USERNAMEspecifies the SSH login username of the private image to be created.

INSTANCE_NAME specifies the temporary packer’s instance name.

SOURCE_ID defines the ID of the public image on your Cloud Provider’s Console.

FLAVOR defines the capacity of your machine in term of CPU and memory. More information can be found on Flexible Engine Documentation.

NETWORK_ID represents the ID of your network where your temporary packer instance gonna belong. It can be found on your Cloud Provider’s Console.

FLOATING_ID is the ID of your created EIP that you gonna bind to your packer temporary instance in order to enable it to access internet and download packages. The creation on your EIP can be done on your Cloud Provider’s Console.

Build your Template

After defining your variable.sh Let’s create the packer’s template template.json

Our Template is made of a Builder and a Provisioner. For the builder, i am using the OpenStack Builder that is compatible with Flexible Engine platform.

The builder uses the variables that i have defined in variable.sh to build the image.

The provisioner as i have described before is used to configure the image. There are many types of provisioners. Here i am using only three : Inline, shell and source/destination provisioners.

Shell Provisioner allow you to execute commands defined on your shell script on the temporary packer machine to configure your packer image.

Inline Provisioner allow you to execute commands defined in the inline block on the temporary packer machine to configure your packer image.

Source/Destination Provisionner allow you to move your local files to the temporary packer machine.

Run Packer

First you need to source your variable.sh script to setup the env variable, next build your image by running the packer build template.json command.

References

Packer Documentation

--

--

Sylia CHIBOUB

Supporting Open Source and Cloud Native as a DevOps Engineer