With the scalable infrastructure that the cloud provides, you can have a dynamic horizontal scaling model where VMs scale with traffic. Therefore, you can have the best possible utilization of your infrastructure – the best bang for your buck! The problem with the traditional approach is that even if we use Ansible to apply the configuration to new machines, it is slower to get ready. Therefore, the scaling is not optimal, especially for bursty traffic.
Immutable infrastructure helps you manage these problems by taking the same approach we took for containers – baking configuration directly into the OS image using modern DevOps tools and practices. Immutable infrastructure helps you deploy the tested configuration to production by replacing the existing VM without doing any updates in place. It is faster to start and easy to roll back. You can also version infrastructure changes with this approach.
HashiCorp has an excellent suite of DevOps products related to infrastructure and configuration management. HashiCorp provides Packer to help you create immutable infrastructure by baking configurations directly in your VM image, rather than the slow process of creating a VM with a generic OS image and then customizing it later. It works on a similar principle as Docker uses to bake container images; that is, you define a template (configuration file) that specifies the source image, the desired configuration, and any provisioning steps needed to set up the software on the image. Packer then builds the image by creating a temporary instance with the base image, applying the defined configuration, and capturing the machine image for reuse.
Packer provides some of the following key features:
- Multi-platform support: Packer works on the plugin architecture and, therefore, can be used to create VM images for a lot of different cloud and on-premises platforms, such as VMware, Oracle VirtualBox, Amazon EC2, Azure’s ARM, Google Cloud Compute, and container images for Docker or other container runtimes.
- Automation: Packer automates image creation and eliminates manual effort to build images. It also helps you with your multi-cloud strategy, as you can use a single configuration to build images for various platforms.
- Fosters GitOps: Packer configurations are machine-readable and written in HCL or JSON, so they can easily sit with your code. This, therefore, fosters GitOps.
- Integration with other tools: Packer integrates well with other HashiCorp tools, such as Terraform and Vagrant.
Packer uses a staging VM to customize the image. The following is the process that Packer follows while building the custom image:
- You start with Packer configuration HCL files to define the base image you want to start from and where to build the image. You also define the provisioner for building the custom image, such as Ansible, and specify what playbooks to use.
- When you run a Packer build, Packer uses the details in the configuration files to create a build VM from the base image, run the provisioner to customize it, turn off the build VM, take a snapshot, and save that as a disk image. It finally saves the image in an image repository.
- You can then build the VM from the custom image using Terraform or other tools.
The following figure explains the process in detail:

Figure 10.1 – Packer build process
The result is that your application is quick to start up and scales very well. For any changes within your configuration, create a new disk image with Packer and Ansible and then use Terraform to apply the changes to your resources. Terraform will then spin down the old VMs and spin up new ones with the new configuration. If you can relate it to the container deployment workflow, you can make real sense of it. It’s akin to using the container workflow within the VM world! But is immutable infrastructure for everyone? Let’s understand where it fits best.
Leave a Reply