IAC / A CONCEPT NOTE

Packer

building golden machine images with code

~80 sec read

Overview · mechanism
pitfall · examples

01 / THE SHORT VERSION

The idea in a few sentences.

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.

02 / FOLLOW THE MECHANISM

How packer builds an image

  1. Packer template

    a JSON/HCL file defines builders (e.g., amazon-ebs), provisioners (shell, Ansible), and post-processors.

  2. Builder

    launches a temporary instance from a base AMI in your cloud account.

  3. Provisioner

    SSHs into the instance, installs packages, copies config files, and runs setup scripts.

  4. Post-processor

    shuts down the instance, creates an image snapshot, and optionally cleans up the temp instance.

  5. Output

    a new AMI is registered in your account — ami-0abc123def456..., ready for EC2 or ASG launch templates.

04 / COMMAND NOTES

Read the command, then the result.

Inspect the flags and arguments before trying an example. Snippets can need local setup, replacement values, or resources in your own environment.

EXAMPLE 01 · REFERENCE

build an image from a Packer template

packer build ami.pkr.hcl

EXAMPLE 02 · REFERENCE

validate the template syntax and config

packer validate ami.pkr.hcl

Explore command anatomy in the CLI lab