Packer Tutorial
Learn Packer step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
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.
- Installing Packer on Windows, macOS and Linux Install HashiCorp Packer with a package manager or a zip download, verify the checksum, check the version, set up editor support and prepare a clean working folder.
- Your First Packer Build with the Docker Builder Write a Packer template for the Docker builder, run init, fmt, validate and build, tag the result with a post-processor, and understand what happens at each step.
- Packer CLI Commands and Build Flags A practical tour of the Packer command line: init, fmt, validate, build, console, inspect, plugins, hcl2_upgrade, build flags like -only and -on-error, logging and exit codes.
Concepts and Templates
- Packer Template Anatomy: Blocks, Arguments, Expressions Understand every top-level block in a Packer HCL2 template, how arguments and nested blocks differ, how references work and how Packer builds a plan from your files.
- Packer Source and Build Blocks in Depth Learn how Packer source and build blocks fit together: multiple sources, build names, source-specific provisioners with only and except, build variables and execution order.
- Packer Plugins, Version Constraints and packer init How Packer plugins work: required_plugins, source addresses, version constraints, packer init and upgrade, installing from a mirror or local file, and plugin naming rules.
- Packer Build Lifecycle, Artifacts and the Manifest Follow a Packer build from start to cleanup: machine creation, communicator, provisioners, artifact creation, post-processor chains, the manifest file and what happens on errors.
- Packer Project Layout, Multiple Files and Legacy JSON Organize a Packer project in several files, learn which file extensions Packer loads, how variable files are picked up, and how to migrate legacy JSON templates to HCL2.
Builders and Provisioners
- Packer Builders Overview: Cloud, VM and Container Sources Compare Packer builders for clouds, hypervisors and containers, learn the common settings they share, the Docker and null builders in detail, and how to choose a builder.
- Packer amazon-ebs Builder: Build an AMI Step by Step Build AWS AMIs with Packer: credentials, the amazon-ebs source, looking up the newest base AMI, naming with timestamps, tags, encryption, multi-region copies and IAM permissions.
- Packer VM Builders: QEMU, VirtualBox and VMware ISO Builds Build virtual machine images from an installer ISO with Packer: iso_url and checksums, boot_command, HTTP-served autoinstall files, shutdown, output formats and acceleration.
- Packer Shell and File Provisioners: Scripts, Uploads, Cleanup Use Packer's shell, shell-local and file provisioners: inline commands, scripts, environment variables, execute_command, uploads and downloads, retries, ordering and image cleanup.
- Packer Ansible Provisioner and Other Configuration Tools Configure images with the Packer ansible and ansible-local provisioners, PowerShell and Windows restarts, breakpoints, InSpec and Chef or Puppet, and learn when to use scripts or tools.
Variables and HCL
- Packer Input Variables: Types, Defaults, Validation, Precedence Parameterize Packer templates with input variables: types, defaults, descriptions, validation rules, sensitive values, var files, PKR_VAR environment variables and precedence.
- Packer Locals and HCL Expressions: Conditionals, for, dynamic Compute values in Packer with locals, template strings, conditionals, for expressions, splat, merge and dynamic blocks, and learn when to use a local instead of a variable.
- Packer Built-in Functions: Strings, Dates, Files, Collections A tour of Packer's HCL functions: string and regex helpers, timestamp and formatdate, file and templatefile, collection and encoding functions, plus env, uuidv4 and secret lookups.
- Packer Data Sources and Handling Secrets Safely Look up the newest AMI with a Packer data source, read secrets from AWS Secrets Manager or Vault, mark values sensitive, and keep credentials out of logs and images.
Images for Cloud and VMs
- Building AMIs on AWS with amazon-ebs Build Amazon Machine Images with the Packer amazon-ebs builder: source AMI filters, instance types, tags, encryption, sharing and cleanup, with a runnable naming checker.
- Azure and Google Cloud Images with Packer Build Azure managed images and Compute Gallery versions and Google Compute Engine images with Packer: authentication, source images, naming rules and a runnable name-mapping stand-in.
- VirtualBox, VMware and QEMU Images Build local and on-premises VM images with Packer: ISO installs, boot commands, preseed and cloud-init, VirtualBox, VMware and QEMU builders, Vagrant boxes and Windows notes.
- The Docker Builder and Container Images Use Packer's docker builder to build container images with the same provisioners as VM images: commit and export modes, changes, tags, and when a Dockerfile is the better tool.
Post-processors and Pipelines
- Post-processors: What They Do and How to Chain Them Understand Packer post-processors: what an artifact is, the post-processor and post-processors blocks, sequences versus parallel steps, only/except and keep_input_artifact.
- The Manifest Post-processor and Image Metadata Use the Packer manifest post-processor to record AMI ids, build times and custom data in JSON, parse artifact ids per region, and pass image ids to Terraform or deployment steps.
- Checksum, Compress, shell-local and Artifice Use the checksum, compress, shell-local and artifice post-processors to publish file images safely, verify downloads, run custom steps and replace artifacts in a chain.
- Tagging and Pushing Container Images in a Pipeline Chain docker-tag, docker-push and manifest post-processors correctly, log in to registries safely, choose immutable tags and record image digests for deployment.
Testing and Security
- Validating Packer Templates: fmt, validate and inspect Catch template mistakes early with packer fmt, init, validate and inspect, use the console, read PACKER_LOG output, run -on-error, and add checks to every pull request.
- Testing the Images You Build Test Packer images before release: provisioner-time checks, InSpec and Goss specs, boot smoke tests, test instances, a test pyramid for images, and failing the build on bad results.
- Hardening and Securing Packer Images Secure the image build: keep secrets out of images and logs, use sensitive variables and short-lived credentials, harden the OS, remove build leftovers and protect the pipeline.
CI/CD Patterns
- Running Packer in GitHub Actions Build images in GitHub Actions: setup-packer, init, validate and build jobs, OIDC to AWS without stored keys, matrix builds per source, caching plugins and pull request versus main behavior.
- Image Versioning, Registries and Promotion Version golden images with semantic versions and commit SHAs, use channels such as dev, staging and prod, learn what the HCP Packer registry adds, and promote images safely with Terraform.
- Rebuild Schedules and Image Lifecycle Keep golden images fresh: rebuild on a schedule and on triggers, track image age, retire old AMIs and snapshots with retention rules, and plan cost, drift and rollout waves.
Projects and Practice
- Project: A Golden Web Server Image Plan and write a complete Packer project: a hardened, tested Nginx web server image with variables, provisioners, a cleanup step, a manifest and a pull request pipeline.
- Project: A Multi-cloud Image Matrix Design one Packer build that produces matching images on AWS, Azure and Google Cloud: shared provisioning, per-cloud sources, naming rules, a combined manifest and a parallel pipeline.
- Project: A Hardened Base Image Pipeline Build a hardened base image pipeline: layered images, a hardening script verified by tests, vulnerability and secret scans as gates, weekly rebuilds, channels and an audit trail.
- Practice: Troubleshooting and Reviewing Packer Templates A practice lesson with the most common Packer failures (SSH timeouts, plugin errors, hung provisioners, name collisions), a debugging method, a review checklist and self-test exercises.