Packer Cheat Sheet
Quick reference for HashiCorp Packer templates, builders, provisioners, and CLI commands used to build immutable machine images.
2 PagesIntermediateFeb 5, 2026
Basic Commands
Core Packer CLI workflow.
bash
packer init . # Download required pluginspacker fmt . # Format HCL filespacker validate . # Validate template syntaxpacker build . # Build image(s) defined in templatepacker build -var 'region=us-east-1' . # Pass a variablepacker build -only='amazon-ebs.ubuntu' . # Build a single named source
HCL2 Template
Minimal image build definition using the amazon-ebs builder.
hcl
packer { required_plugins { amazon = { version = ">= 1.2.0" source = "github.com/hashicorp/amazon" } }}source "amazon-ebs" "ubuntu" { ami_name = "my-app-{{timestamp}}" instance_type = "t3.micro" region = "us-east-1" source_ami_filter { filters = { name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*" } owners = ["099720109477"] most_recent = true } ssh_username = "ubuntu"}build { sources = ["source.amazon-ebs.ubuntu"] provisioner "shell" { inline = ["sudo apt-get update", "sudo apt-get install -y nginx"] }}
Provisioners
Mechanisms Packer uses to configure the image during build.
- shell- Runs inline commands or a local script on the instance being built
- file- Uploads a local file or directory to the build instance
- ansible- Runs an Ansible playbook against the temporary build instance
- powershell- Runs PowerShell commands, typically for Windows image builds
- post-processor "vagrant"- Packages the finished build output into a Vagrant box
- post-processor "manifest"- Writes build artifact metadata (IDs, timestamps) to a JSON file
Common Builders (Sources)
Plugins that define which platform an image is built for.
- amazon-ebs- Builds an AMI backed by an EBS snapshot on AWS
- googlecompute- Builds a custom image on Google Compute Engine
- azure-arm- Builds a managed image or VM image on Azure
- docker- Builds and commits a Docker image using a running container
- qemu- Builds a QEMU/KVM disk image, useful for local or on-prem virtualization
- virtualbox-iso- Builds a VirtualBox VM image starting from an ISO installer
Pro Tip
Use `packer build -only` and multiple named `source` blocks to build several platform images (AWS, Azure, Docker) from one shared template and one set of provisioners, keeping configuration DRY across clouds.
Was this cheat sheet helpful?
Explore Topics
#Packer#PackerCheatSheet#DevOps#Intermediate#BasicCommands#HCL2Template#Provisioners#CommonBuildersSources#CommandLine#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance