2023/03/03 • 3 min read

Infrastructure As Code – Packer

This is not an in-depth work on Packer but rather a brief presentation on the what, how, and why Packer may be a good solution to be used on the underlying structure builds of an infrastructure.

So, let’s start by describing what Packer is.

What is Packer?

Basically, Packer is an open-source tool created by Hashicorp. This tool lands in the category of IaC Tools. It helps us build a standard image to be used cross-platform and has an infrastructure underlying building block to be later applied as a basis for your machine type, it helps you maintain a single or unique source configuration, also called a “golden image”.

Other tools can also be used as an alternative, Docker and Vagrant are examples, but why choose Packer itself? Did I say already that it’s free? Well some other key features come into play: Packer is lightweight, it can run on any common operating system, and it’s fast! Yes it’s fast! It can create your machine images at multiple endpoints simultaneously allowing you to create a single pipeline to deploy it across your structure and can use a few types of format files to let you describe the needed configuration, like JSON.

On a small side note:
Although Packer can also be used to deploy machines across supported platforms, this is not its purpose, and neither does it replace the role of a configuration management tool, like Puppet, Chef, or even Ansible (a subject for a future article), but it can use these managers to actually install software and even configure even more images to a level of readiness to be directly used as a golden image or to be later deployed with specific configurations setup at the time of deployment for each of their use cases using the configuration management tools.
After your image is created, you will use your other IaC tools if you are truly following a CI/CD approach, deploying it on your infrastructure using any of the provisioning tools you wish to use. In my case, I’m biased toward Terraform, but you can use your choice of tools from the available ones.

How do we start working with it?

You can start by downloading the tool from the Hashicorp website and installing it. Choose your flavor for installation and follow the steps. The setup is quite simple, and the steps are well documented.
Once you have installed it, confirm whether everything was done successfully by checking the return of Packer help or just checking the version you use with “packer –version”. In either case, it should return some output confirming that you have installed it.

As you can see below, easy fast, and simple and you are ready to start building your infrastructure images.

How to create your own images?

To be able to start using Packer you will need to understand the key “building” blocks to make the magic happen: Plugins.

Builders are used to create artifacts on major cloud services.

Provisioners allow us to modify the VMs using either shell or other configuration management tools.

Post-Processors allow us to manage the artifacts once they have been created.

However, with the growing adaptation of this tool, as you can imagine the sheer number of plugins has increased. Now over 60 plus supported plugins, a word of caution is mandatory. They follow the same tier as all other Hashicorp tools, using a tier value to help you identify the correct approach in case of the maintained support. Once you have located your specific plugin, you need to check the tier information.

Tiers example

Once you have the building blocks, a couple more things need to be added to the flow: the use of Variables and what type of format to use.
Packer can use JSON format or Hashicorp Configuration Language (HCL for short). Packer is currently using HCL2 version, but you can still find HCL and Json examples, and it still works depending on your version of Packer.

Code examples and notions

Below you can have a look at some code examples that better illustrate the use of Packer. Keep in mind that you will always need the Authentication and Permissions on the correct credentials to be used to perform any actions. These permissions may be by methods of: an Access Token given, a JSON Service Account Key created and stored, or others.

Consider that this example only shows a type of structure adopted for this case and that it shouldn’t be taken literally as a specific work setup

Structure example

Here you can see a template file comprising several blocks containing variables, builders, provisioners, and post-processors blocks

Template file example

And now, a variable file structure

Variable file example

Overall assessment of the tool usage

So why did we choose Packer as one of the tools on our IaC tools bag?

Well to sum it up, the ease of learning how to use this tool, the ability to use it cross-platforms and integration with our CI/CD pipelines and Git repositories were some of the most relevant features that we took in to account. Also, last but not the list, the fact that it’s open source.

So for that, this tool got our own seal of approval.

Go back to the blog posts list