Packer

Also known as: HashiCorp Packer

On this page 7

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.

BlockPurposeTypical example
SourceDefines the builder and its platform settingsamazon-ebs, azure-arm, googlecompute, docker
BuildCombines sources with provisioners and post-processorsOne build producing images in two regions
ProvisionerInstalls and configures software inside the machineshell, file, ansible, powershell
Post-processorActs on the finished artifactmanifest, docker-tag, compress, checksum
Variable and localsParameterize versions, regions and credentialsBase image ID, application version
Data sourceLooks up external values at build timeLatest official base image, secret value
Main building blocks of a Packer template

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.

BuilderHow it worksArtifact produced
amazon-ebsLaunches an EC2 instance and snapshots its EBS volumeAmazon Machine Image (AMI)
azure-armCreates a temporary virtual machine in a resource groupManaged image or Compute Gallery version
googlecomputeBoots a Compute Engine instance from a source imageGoogle Compute Engine image
vsphere-isoInstalls an operating system from ISO mediaVMware virtual machine template
qemuRuns a local virtual machine through QEMUqcow2 or raw disk file
dockerStarts a container and commits its file systemContainer image
Common builder types and their artifacts

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 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.

ToolPrimary jobWhen it actsRelation to Packer
PackerBuilds machine imagesBuild time, before deploymentProduces the artifact
TerraformCreates and updates cloud resourcesDeployment timeLaunches instances from Packer images
AnsibleConfigures systems over SSHBuild time or run timeOften used as a Packer provisioner
cloud-initApplies instance settings at first bootFirst bootHandles per-instance values the image omits
DockerfileDefines a container imageBuild timeAn alternative for container-only workloads
Where Packer fits among neighboring tools

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.

SymptomLikely causeFix
Credentials found inside an imageSecrets written to disk during provisioningInject secrets at boot and clean temporary files
Builds take over an hourLarge package installs from scratch each timeLayer images from a hardened base image
Orphaned instances and key pairsBuild interrupted before cleanupUse cleanup tags and a scheduled sweeper
Images differ between runsUnpinned packages and plugin versionsPin versions and the base image filter
Cloned instances share identitiesMachine IDs and SSH host keys baked inGeneralize with Sysprep or remove machine IDs
Typical Packer problems and their fixes

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.