~/CloudSetup.In

Packer

Infrastructure template.pkr.hcl ● stable

Build identical machine images for multiple platforms from one template.

01 Overview

Packer automates building a VM or container image — installing packages, baking in config — once, then publishing it to AMI, a container registry, or another target, so every environment starts from the same golden image.

→ official documentation

02 Installation

Pick a platform. Each step is copy-pasteable on its own.

01  Install prerequisites
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl
02  Add the HashiCorp GPG key and repo
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
03  Install Packer
sudo apt-get update && sudo apt-get install -y packer
04  Verify
packer -version
01  Add the HashiCorp repo
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
02  Install Packer
sudo dnf install -y packer
03  Verify
packer -version
01  Install via Homebrew
brew tap hashicorp/tap
brew install hashicorp/tap/packer
02  Verify
packer -version
Note — commands assume sudo privileges and an up-to-date package index. Pin a specific version in production rather than tracking latest.

03 Install script

The steps above bundled into a single idempotent shell script for provisioning boxes unattended.

install-packer.sh
Ubuntu/Debian target · safe to re-run · exits non-zero on failure
↓ download script

04 Cheatsheet

The commands reached for most often once Packer is installed.

CommandWhat it does
packer init .Download required plugins for a template
packer validate template.pkr.hclCheck a template for errors
packer build template.pkr.hclBuild the image(s) defined in a template
packer fmt -recursiveAuto-format HCL templates
packer consoleInteractively evaluate HCL expressions