Packer, also known as HashiCorp Packer, is an open-source tool that builds machine images automatically from a single source template, producing identical images for several platforms such as Amazon Machine Images, Azure managed images, Google Compute Engine images, VMware templates and Docker images. In infrastructure as code for the cloud, Packer covers the image layer: it turns the installation and hardening of an operating system into versioned, repeatable code instead of a manual procedure.
Packer does not deploy or manage running infrastructure. It produces artifacts, usually golden images, that provisioning tools such as Terraform, OpenTofu or AWS CloudFormation later launch. This division of labor makes Packer a central building block of immutable infrastructure, where servers are replaced with new images rather than modified in place.
How Packer builds a machine image
Packer builds an image by launching a temporary machine, configuring it and capturing its disk as a reusable artifact. A build in the cloud typically starts an instance from a base image, connects to it over SSH or WinRM, runs provisioning steps, stops the instance, creates a snapshot and registers the result as a new image, then deletes every temporary resource.
The template is written in HashiCorp Configuration Language (HCL2), which replaced the older JSON template format in version 1.5 and later. A template is organized into a small number of block types, and each type has a distinct responsibility in the build pipeline.
| Block | Purpose | Typical example |
|---|---|---|
| Source | Defines the builder and its platform settings | amazon-ebs, azure-arm, googlecompute, docker |
| Build | Combines sources with provisioners and post-processors | One build producing images in two regions |
| Provisioner | Installs and configures software inside the machine | shell, file, ansible, powershell |
| Post-processor | Acts on the finished artifact | manifest, docker-tag, compress, checksum |
| Variable and locals | Parameterize versions, regions and credentials | Base image ID, application version |
| Data source | Looks up external values at build time | Latest official base image, secret value |
Because one build block can reference several sources at once, Packer runs those builds in parallel and applies the same provisioning steps to each. This multi-platform parity is the original reason the tool exists: the same template yields an AWS image, an Azure image and a local virtual machine with matching contents.
Builders, provisioners and plugins
Packer delegates platform-specific work to plugins, which since version 1.7 are distributed and versioned separately from the core binary. A required_plugins block pins each plugin to a version range, and the packer init command downloads them, which keeps builds reproducible across workstations and CI runners.
Builders differ mainly in how they obtain a temporary machine and how they capture the result. The choice of builder determines build time, cost and the artifact format.
| Builder | How it works | Artifact produced |
|---|---|---|
| amazon-ebs | Launches an EC2 instance and snapshots its EBS volume | Amazon Machine Image (AMI) |
| azure-arm | Creates a temporary virtual machine in a resource group | Managed image or Compute Gallery version |
| googlecompute | Boots a Compute Engine instance from a source image | Google Compute Engine image |
| vsphere-iso | Installs an operating system from ISO media | VMware virtual machine template |
| qemu | Runs a local virtual machine through QEMU | qcow2 or raw disk file |
| docker | Starts a container and commits its file system | Container image |
Provisioners range from simple shell scripts and file uploads to full configuration management with Ansible, Chef, Puppet or Salt. Many teams reuse their existing Ansible playbook during the image build, so the same configuration logic serves both baked images and running servers.
Packer compared with related infrastructure tools
Packer is often confused with provisioning tools, but it operates at a different stage of the lifecycle. It creates the image once at build time, whereas the other tools either create cloud resources or configure machines while they run.
| Tool | Primary job | When it acts | Relation to Packer |
|---|---|---|---|
| Packer | Builds machine images | Build time, before deployment | Produces the artifact |
| Terraform | Creates and updates cloud resources | Deployment time | Launches instances from Packer images |
| Ansible | Configures systems over SSH | Build time or run time | Often used as a Packer provisioner |
| cloud-init | Applies instance settings at first boot | First boot | Handles per-instance values the image omits |
| Dockerfile | Defines a container image | Build time | An alternative for container-only workloads |
The practical pattern is bake versus fry: Packer bakes slow, stable layers such as packages, agents and security baselines into the image, while cloud-init or a configuration tool fries in instance-specific values such as hostnames, secrets and environment endpoints at boot. Baking more shortens boot time and reduces configuration drift; baking less keeps images more generic.
Using Packer in an image pipeline
Packer delivers the most value when it runs inside a continuous integration pipeline rather than on a laptop. A typical pipeline validates the template with packer validate, formats it with packer fmt, builds the image, runs automated tests against a test instance and only then publishes the image to consumers.
- Pin the base image through a data source filter and record its ID in the manifest, so every image traces back to a known parent.
- Tag images with metadata such as build date, Git commit and operating system version to support audits and rollbacks.
- Test the result with tools such as Goss or InSpec before promotion, because a successful build does not prove a working image.
- Rebuild on a schedule, commonly weekly or monthly, so security patches reach the fleet without in-place updates.
- Deregister old images and delete their snapshots under a retention policy to control storage cost.
Organizations that use HCP Packer, the managed registry from HashiCorp, can track image versions in channels such as development and production and let Terraform query the approved image. Teams without the registry often publish image IDs to a parameter store, which achieves a similar promotion workflow with more manual wiring.
Limitations and common mistakes
Packer is simple in concept, but image pipelines fail in predictable ways. Most problems come from leaking secrets into images, slow builds or images that drift away from their template.
| Symptom | Likely cause | Fix |
|---|---|---|
| Credentials found inside an image | Secrets written to disk during provisioning | Inject secrets at boot and clean temporary files |
| Builds take over an hour | Large package installs from scratch each time | Layer images from a hardened base image |
| Orphaned instances and key pairs | Build interrupted before cleanup | Use cleanup tags and a scheduled sweeper |
| Images differ between runs | Unpinned packages and plugin versions | Pin versions and the base image filter |
| Cloned instances share identities | Machine IDs and SSH host keys baked in | Generalize with Sysprep or remove machine IDs |
Since August 2023, newer Packer releases use the Business Source License, which restricts offering the tool as a competing commercial service but does not limit ordinary internal use. Unlike Terraform, which gained the OpenTofu fork, Packer has no widely adopted open-source fork, so license-sensitive organizations should review the terms before standardizing.
The simple explanation
Packer is like a cookie cutter for servers. Instead of setting up every new computer by hand, a team writes down the recipe once: which operating system, which programs and which security settings. Packer follows that recipe and produces a finished master copy of a server.
Any number of identical servers can then be started from that master copy in seconds. When something needs to change, the team updates the recipe and makes a new master copy instead of repairing each server one by one. The key idea is simple: build once, copy many times.
How it is used in practice
A platform team at an online retailer runs about 300 web servers on AWS across two regions. Previously, each new server ran a 20-minute configuration script at boot, and slight differences between servers caused hard-to-trace errors during traffic peaks.
The team writes a Packer template that starts from the latest Ubuntu LTS image, installs the web server, monitoring agent and security baseline through an Ansible playbook, and copies the resulting AMI to both regions. A CI pipeline rebuilds the image every Monday, runs automated tests on a test instance and records the new AMI ID for Terraform.
Boot time drops from about 20 minutes to under two minutes, which lets autoscaling react to load spikes. Because every server now starts from the same tested image, configuration drift disappears, and rolling back a faulty release means launching the previous image.