
Machine images as code with Packer
Problem
Containers and deployables in PaaS (Platform as a Service) environments are okay, but sometimes you need or want to create machine images, e.g. for IaaS (Infrastructure as a Service) environments or to set up the PaaS environments mentioned above. And in best case independent of the respective virtualization environment or cloud. Moreover, the advantage of machine images is that they only have to be provisioned at the time of creation and can afterwards be started quickly.
So back to self-made shell scripts and handwork? No.
Solution
There is a tool from HashiCorp, the originators of Terraform, Vagrant, Vault etc..
Packer offers the possibility to define the machine image at first and in the following to create the images for different virtualization solutions and clouds. You don't have to reinvent the wheel, because Packer supports well-known provisioning tools like Chef, Ansible and others.
Components from Packer:
- Templates are the configuration files (JSON) of Packer and describe one or more images.
- Builders produce the images for one platform at a time.
- e.g. Amazon EC2, Azure, Docker, Google Cloud, Vagrant, VirtualBox, VMWare, and many more.
- Provisioners install and configure the respective image.
- e.g. with Chef, Puppet, Ansible, Shell-Scripts, etc.
- Artifact is the result of a builder (the generated machine image).
- Post-Processors takes the created images and executes further tasks (e.g. push in Docker Registry).
All defined images are created with the following simple command:
packer build
The packer template creates an Amazon Machine Image (AMI) in AWS. In the build, the image is provisioned with Ansible.

Further Aspects
- https://www.packer.io/
- https://github.com/kaorimatz/packer-templates
- https://bitbucket.jambit.com/projects/POSTACK/repos/example_packer/browse
---
Author
Philipp Hemmer / Senior Software Architect / Business Division Automotive Bavaria
Download Toilet Paper #113: Machine images as code with Packer (pdf)