Packer — Automated Machine Image Building for Any Platform
Packer is HashiCorp's tool for creating identical machine images across multiple platforms from a single source. Build AMIs, Docker images, GCE images, and VMware templates in one pipeline — the standard for immutable infrastructure.
What it is
Packer is HashiCorp's open-source tool for creating identical machine images across multiple platforms from a single source configuration. It supports Amazon AMIs, Docker images, Google Compute Engine images, VMware templates, and many more builders through a plugin system.
Infrastructure engineers, DevOps teams, and platform teams use Packer to enforce consistency between development, staging, and production environments. By baking all dependencies into a machine image, you eliminate configuration drift that occurs when provisioning servers at boot time.
How it saves time or tokens
Without Packer, teams either provision servers at boot time (slow, error-prone) or manually create and snapshot images (inconsistent, undocumented). Packer automates the entire process: launch a temporary instance, run provisioners (shell scripts, Ansible, Chef), capture the image, and destroy the instance. The result is a versioned, reproducible artifact.
A single packer build command replaces a multi-step manual process that typically takes 30-60 minutes of an engineer's time per image variant.
How to use
- Install Packer:
brew install packer
- Write an HCL template and build:
packer init aws.pkr.hcl
packer validate aws.pkr.hcl
packer build aws.pkr.hcl
- The output is a machine image ID (AMI, Docker tag, etc.) ready for deployment.
Example
A Packer HCL template for building an AWS AMI:
packer {
required_plugins {
amazon = {
source = "github.com/hashicorp/amazon"
version = "~> 1.3"
}
}
}
source "amazon-ebs" "ubuntu" {
ami_name = "my-app-{{timestamp}}"
instance_type = "t3.micro"
region = "us-east-1"
source_ami = "ami-0c7217cdde317cfec"
ssh_username = "ubuntu"
}
build {
sources = ["source.amazon-ebs.ubuntu"]
provisioner "shell" {
inline = [
"sudo apt-get update -y",
"sudo apt-get install -y nginx"
]
}
}
This template launches a t3.micro instance, installs nginx, and captures the result as a timestamped AMI.
Related on TokRepo
- DevOps Tools -- Infrastructure automation tools for building and deploying
- Automation Tools -- CI/CD pipelines and build automation workflows
Common pitfalls
- Forgetting
packer initbefore the first build. Packer 1.7+ requires explicit plugin installation via init, and builds fail without it. - Not cleaning up old AMIs. Each build creates a new image; set up a lifecycle policy or script to deregister images older than N days.
- Provisioner ordering matters. Shell provisioners run sequentially; if an early step fails, subsequent steps may succeed silently with a broken base. Use
set -ein shell scripts.
Frequently Asked Questions
Packer supports AWS (AMI), Google Cloud (GCE), Azure (managed images), Docker, VMware, VirtualBox, Proxmox, and many more through its plugin system. You can build images for multiple platforms simultaneously from the same template.
Packer creates machine images. Terraform provisions infrastructure using those images. A typical workflow is: Packer builds an AMI, then Terraform references that AMI ID to launch EC2 instances. Both are HashiCorp tools and complement each other.
Yes. Packer has an Ansible provisioner that runs playbooks inside the temporary build instance. This lets teams reuse existing Ansible roles for image building without rewriting them as shell scripts.
Yes. You can define multiple sources in a single build block and Packer builds them in parallel. This is useful for creating identical images across AWS, GCP, and Docker simultaneously.
Packer supports both HCL (HashiCorp Configuration Language) and JSON templates. HCL is the recommended format since Packer 1.7, offering better readability, variables, and expressions. JSON templates are considered legacy but still supported.
Citations (3)
- Packer GitHub— Packer creates identical machine images across multiple platforms
- Packer Documentation— HCL template format and plugin system for Packer
- HashiCorp Learn— Immutable infrastructure pattern for reducing configuration drift
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.