Image building with Packer · Lesson 2 of 5 · 4 min read
Packer basics
Template structure
Packer templates are written in HCL2 (the same language as Terraform) in files ending .pkr.hcl:
packer {
required_plugins {
amazon = { source = "github.com/hashicorp/amazon", version = "~> 1.3" }
}
}
variable "region" {
type = string
default = "ap-south-1"
}
variable "git_sha" {
type = string
default = env("GIT_SHA")
}
locals {
stamp = formatdate("YYYYMMDD-hhmm", timestamp())
}
source "amazon-ebs" "al2023" {
region = var.region
instance_type = "t3.small"
ssh_username = "ec2-user"
ami_name = "base-al2023-${local.stamp}"
source_ami_filter {
filters = {
name = "al2023-ami-2023.*-x86_64"
virtualization-type = "hvm"
root-device-type = "ebs"
}
owners = ["amazon"]
most_recent = true
}
tags = {
os = "al2023"
built_by = "packer"
git_sha = var.git_sha
}
}
build {
sources = ["source.amazon-ebs.al2023"]
provisioner "shell" {
scripts = ["scripts/update.sh", "scripts/agents.sh", "scripts/harden.sh"]
}
provisioner "file" {
source = "files/sshd_config"
destination = "/tmp/sshd_config"
}
provisioner "shell" {
inline = ["sudo install -m 600 /tmp/sshd_config /etc/ssh/sshd_config", "sudo /usr/local/bin/cleanup.sh"]
}
post-processor "manifest" { output = "manifest.json" }
}
| Block | Purpose |
|---|---|
packer { required_plugins } |
Which builder plugins and versions to install (packer init) |
source |
Builder configuration: where and how to create the machine (amazon-ebs, azure-arm, googlecompute, docker) |
build |
Which sources to build and what to run on them |
provisioner |
Steps that configure the machine: shell, file, powershell, and others |
post-processor |
What to do with the result: write a manifest, push a Docker image, upload metadata |
variable, locals |
Inputs and computed values, set with -var, *.pkrvars.hcl files or PKR_VAR_ environment variables |
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
The workflow and what amazon-ebs does
packer init . # install required plugins
packer fmt -check . # formatting, like terraform fmt
packer validate -var-file=prod.pkrvars.hcl .
packer build -var-file=prod.pkrvars.hcl .
During packer build, the amazon-ebs builder:
- Finds the newest source AMI matching the filter.
- Creates a temporary key pair and security group, and launches a temporary instance.
- Connects over SSH (or through Session Manager, with
ssh_interface = "session_manager", so no inbound port is opened). - Runs the provisioners in order.
- Stops the instance and creates the AMI and its EBS snapshots.
- Terminates the instance and deletes the temporary key and security group.
Enlarge diagram
packer build does with the amazon-ebs builder.Swipe sideways to see all of it, or tap to open it full screen.
If a provisioner fails, Packer cleans up and exits non-zero, so a CI job fails clearly. -on-error=ask (or abort) keeps the instance for debugging locally. The build role needs EC2 permissions to launch instances, create images and snapshots, and tag them; run it with an IAM role from CI (OIDC), never long-lived keys.
Use source_ami_filter with most_recent = true and the right owners so each build starts from the latest patched base image, and never trust an AMI by name alone from an unknown owner: anyone can publish an AMI called amzn2-ami-….