HashiCorp Packer

HashiCorp Packer Basics

Mutable vs Immutable Infrastructure

In this guide, we’ll dive into the fundamental differences between mutable and immutable infrastructure and demonstrate how HashiCorp Packer enables a truly immutable workflow.

Table of Contents

  1. What Is Mutable Infrastructure?
  2. What Is Immutable Infrastructure?
  3. Mutable vs Immutable: A Comparison
  4. Introducing HashiCorp Packer
  5. Building Immutable Images with Packer
  6. References

What Is Mutable Infrastructure?

Mutable infrastructure evolves over time. You maintain a long-lived server, log in to apply patches, and deploy application updates directly on that machine.

Workflow:

  1. Update application code.
  2. Deploy changes to an existing server.
  3. SSH into the server to install packages or tweak configurations.

Warning

Over time, manual patching and ad-hoc changes can lead to configuration drift, causing inconsistencies between environments.

What Is Immutable Infrastructure?

With immutable infrastructure, servers are never modified after they’re deployed. Instead, you build a new server image for every change and replace existing instances.

Workflow:

  1. Update application code.
  2. Build a fresh, fully configured machine image (OS tweaks, dependencies, application artifacts).
  3. Deploy the new image to replace old instances.

This approach guarantees that every server instance is identical, reproducible, and replaceable.

Note

Immutable infrastructure simplifies rollbacks: redeploy a previous image version and your environment reverts instantly.

Mutable vs Immutable: A Comparison

FeatureMutable InfrastructureImmutable Infrastructure
Deployment ModelLive server updates via SSHReplace servers with new images
Configuration DriftHigh risk of drift over timeZero drift—image contains all configuration
RollbacksManual rollback stepsRedeploy previous image
ReproducibilityDifficult to reproduce exact state100% reproducible builds
Tooling ExamplesAnsible, Chef, manual scriptsHashiCorp Packer, Terraform, Docker

Introducing HashiCorp Packer

HashiCorp Packer automates the creation of immutable machine images for multiple platforms:

  • AWS EC2 AMIs
  • Docker containers
  • Google Compute Engine images
  • Azure Managed Images

Packer uses a template to define:

  • Builders: Base image source (e.g., Ubuntu AMI).
  • Provisioners: Steps to install and configure software (Shell, Ansible, Chef).
  • Post-processors: Output formats and artifact distribution.

For more details, see the Packer documentation.

Building Immutable Images with Packer

  1. Create a template file (template.json):

    {
      "builders": [{
        "type": "amazon-ebs",
        "region": "us-west-2",
        "source_ami": "ami-0abcdef12345",
        "instance_type": "t2.micro",
        "ssh_username": "ubuntu",
        "ami_name": "my-app-{{timestamp}}"
      }],
      "provisioners": [{
        "type": "shell",
        "script": "scripts/install.sh"
      }]
    }
    
  2. Run the build command:

    packer build template.json
    
  3. Deploy the resulting AMI via your choice of orchestration:

References

Watch Video

Watch video content

Previous
Mutable Infrastructure