TOPIC IAC
Packer building golden machine images with code
IN 10 SECONDS
Packer by HashiCorp automates the creation of identical machine images (AMI for AWS, GCP Image, Vagrant box) for multiple platforms from a single template. You define the base OS, install software, apply patches, and run scripts — the output is a ready-to-boot image. No more SSHing into a running instance to customize it.
GOTCHA Rarely rebuild your images → you accumulate unpatched CVEs. Rebuild too often → long CI times. Find the cadence: daily security patches, weekly full builds, or image-bake-on-commit for immutable infrastructure.
HOW PACKER BUILDS AN IMAGE
01 Packer template a JSON/HCL file defines builders (e.g., `amazon-ebs`), provisioners (shell, Ansible), and post-processors.
02 Builder launches a temporary instance from a base AMI in your cloud account.
03 Provisioner SSHs into the instance, installs packages, copies config files, and runs setup scripts.
04 Post-processor shuts down the instance, creates an image snapshot, and optionally cleans up the temp instance.
05 Output a new AMI is registered in your account — `ami-0abc123def456...`, ready for EC2 or ASG launch templates.
POKE IT YOURSELF
packer build ami.pkr.hcl — build an image from a Packer template
packer validate ami.pkr.hcl — validate the template syntax and config
Drill this topic →
~80 sec read