Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 36 ยท Getting Started

What Is Packer? Machine Images as Code

Learn what HashiCorp Packer does, why teams build golden machine images, how it differs from Terraform and Ansible, and how a build flows from template to artifact.

Packer is a command-line tool that builds machine images from a text description. A machine image is a frozen copy of a server's disk: an Amazon Machine Image (AMI), a Google Compute Engine image, an Azure managed image, a VMware template, a Docker image, a QCOW2 file for KVM, and so on. You describe the base system, the software you want installed and the settings you want applied. Packer starts a temporary machine, configures it, saves the result as an image and throws the temporary machine away.

The key idea is simple: do the slow, error-prone setup once, at build time, instead of every time a server starts. When you later launch a hundred servers from the image, all hundred are identical and boot in seconds.

This site cannot run Packer, because it needs a real hypervisor, Docker daemon or cloud account. The HCL and shell snippets are read-only. Where a program helps, we use clearly labelled Python stand-ins that model the same ideas (parsing, naming, ordering). Output from real Packer is never invented here: commands are described, and you run them on your own machine.

The problem Packer solves

Without images, a new server follows a long recipe: boot a stock operating system, update packages, install runtimes, copy configuration, start services. That recipe can fail halfway, takes minutes, and gives slightly different results each day because package repositories change. Servers that were set up by hand drift apart over time, and nobody can say exactly what is installed on which one.

With a golden image, the recipe runs once in a controlled build. The result is tested, versioned and reused. A server then is not "configured"; it is simply started from a known-good image. This style is called immutable infrastructure: you never log in to patch a running machine, you build a new image and replace the machine.

flowchart LR
  T["Template<br/>(HCL files)"] --> P["Packer"]
  P --> V["Temporary VM<br/>or container"]
  V --> S["Provision:<br/>install and configure"]
  S --> I["Image artifact<br/>AMI, VM template, Docker image"]
  I --> L["Launch many<br/>identical servers"]

From text template to a reusable image.

What Packer is made of

Packer has a small core and a plugin system:

  • Core: reads your template, validates it, plans the steps and drives the build.
  • Builders (called sources in HCL templates): create the temporary machine on a platform, such as amazon-ebs, docker, qemu, virtualbox-iso, azure-arm, googlecompute.
  • Provisioners: run inside the temporary machine to install and configure software (shell, file, ansible, powershell, ...).
  • Post-processors: work on the finished artifact, for example tagging a Docker image, compressing a file, or writing a manifest.
  • Data sources: look up values at build time, such as "the newest Ubuntu AMI".
  • Plugins: builders, provisioners, post-processors and data sources ship as separate plugin binaries that packer init downloads.

Packer in four steps

Describe Write a template in HCL.
Build Packer starts a temporary machine.
Provision Install software and settings.
Save The disk becomes an image.

Then the temporary machine is deleted.

1 / 2

Packer, Terraform, Ansible: who does what?

These tools are often mixed up because they come from the same ecosystem or solve neighbouring problems.

Tool Main job Output
Packer Build a machine image An image (AMI, template, Docker image)
Terraform / OpenTofu Create and manage infrastructure Running resources: networks, servers, databases
Ansible, Chef, Puppet Configure a machine A configured machine (running or temporary)
Docker build Build a container image A container image only

They work well together. Packer can call Ansible as a provisioner while baking an image. Terraform then launches instances from the AMI Packer produced. The image ID is the handoff between the two: Packer prints or records it, and Terraform looks it up (often with a data source) or receives it as a variable. Packer does not manage running infrastructure and has no state file that tracks live resources. Each packer build is a fresh run that ends with an artifact.

A first look at a template

You will meet every part in detail soon. For now, read this template as a sentence: "Using the Docker platform, start from Ubuntu, run two shell commands, and save the result."

HCL
packer {
  required_plugins {
    docker = {
      version = ">= 1.0.0"
      source  = "github.com/hashicorp/docker"
    }
  }
}

source "docker" "base" {
  image  = "ubuntu:24.04"
  commit = true
}

build {
  sources = ["source.docker.base"]

  provisioner "shell" {
    inline = [
      "apt-get update",
      "apt-get install -y curl",
    ]
  }
}

Running packer init . downloads the plugin, and packer build . produces the image. The next lessons install Packer and walk through exactly that.

A tiny model of a build

The program below is a Python stand-in, not Packer. It models the one fact that matters most: a build is an ordered list of steps that all run against one temporary machine, and the final result is whatever state that machine ended in. If a step fails, the build stops and no image is produced.

Python Stand-in: a build is an ordered list of steps

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that in build 2 the install nginx step never runs. The same is true in real Packer: the first failing provisioner aborts the build (you can choose whether the temporary machine is cleaned up or kept for debugging with -on-error).

Where images are used

  • Autoscaling groups launch new instances from an AMI that already contains your application.
  • Kubernetes node pools use hardened node images.
  • Developer machines and Vagrant boxes share the same base system as production.
  • CI runners are pre-baked with compilers and caches so jobs start fast.
  • Compliance: a security team publishes one approved base image that other teams extend.

When not to use Packer

Packer is not always the right tool. If you deploy only containers, a Dockerfile and a registry may be all you need. If a machine is short-lived and configuring it on boot takes seconds, baking an image adds a pipeline for little gain. Packer shines when boot time, consistency or auditing matter, or when you need the same definition for several platforms (for example an AWS AMI and a VMware template from one codebase).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a successful Packer build produce?

    Packer builds machine images such as AMIs, VM templates or Docker images. It does not manage running infrastructure.
  2. 2. What happens to the temporary machine after the image is saved?

    Packer creates a temporary machine only for the build and removes it afterwards. You can keep it on failure with -on-error=abort.
  3. 3. Which pairing describes how Packer and Terraform usually cooperate?

    The image ID produced by Packer is passed to Terraform, which creates instances from it.
  4. 4. What is 'immutable infrastructure'?

    Instead of changing a running machine you build a new image and replace the machine.

Summary

  • Packer builds machine images from text templates: it starts a temporary machine, provisions it, saves an image and cleans up.
  • Golden images make launches fast and identical, and support immutable infrastructure.
  • Core concepts: sources (builders), provisioners, post-processors, data sources and plugins.
  • Packer builds images, Terraform manages infrastructure, Ansible and similar tools configure machines; they combine well.
  • A failing step aborts the build, so no half-configured image is saved.

Next: Installing Packer