Overview
Packer automates machine image creation for multiple platforms from a single source configuration. Supports AWS AMI, GCP images, Azure images, Docker, and more.
Capabilities
- Multi-platform image building (AWS, GCP, Azure, Docker, VirtualBox)
- HCL2 configuration language
- Provisioners (shell, Ansible, Chef, Puppet)
- Post-processors (compress, manifest, Vagrant)
- Image testing with InSpec/Testinfra
- CI/CD integration for automated image pipelines
When to Use
Trigger phrases:
"packer images"
"HashiCorp Packer — machine image building, builders, provisioners, post-processo"
Building golden images for cloud deployments
Immutable infrastructure patterns
AMI/image creation in CI/CD pipelines
Standardized base images across teams
Compliance-hardened images
When NOT to Use
- Task is outside your authorization scope
- You need to implement controls (use implementing-* skills)
- Task is about analysis, not action (use analyzing-* skills)
- You don't have access to target systems
- Task requires compliance expertise (consult professionals)
- Task is about defense, not offense (use defensive skills)
Pseudo Code
The packer-images workflow follows a standard pipeline pattern.
Core flow:
# packer-images primary flow
input = prepare(raw_data)
result = process(input, config={azure, builders, building, hashicorp, image})
validate(result)
deliver(result)
Error handling:
on error:
log(error_details)
retry_with_backoff(max=3)
if still_failing: alert_and_escalate()
AWS AMI
# aws-ami.pkr.hcl
packer {
required_plugins {
amazon = {
version = ">= 1.2.0"
source = "github.com/hashicorp/amazon"
}
}
}
variable "version" {
type = string
default = "1.0.0"
}
source "amazon-ebs" "ubuntu" {
ami_name = "myapp-${var.version}-{{timestamp}}"
instance_type = "t3.micro"
region = "us-east-1"
source_ami_filter {
filters = {
name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
owners = ["099720109477"]
most_recent = true
}
ssh_username = "ubuntu"
}
build {
sources = ["source.amazon-ebs.ubuntu"]
provisioner "shell" {
inline = [
"sudo apt-get update",
"sudo apt-get install -y docker.io nginx",
"sudo systemctl enable docker nginx"
]
}
provisioner "file" {
source = "configs/nginx.conf"
destination = "/tmp/nginx.conf"
}
provisioner "shell" {
inline = ["sudo mv /tmp/nginx.conf /etc/nginx/nginx.conf"]
}
post-processor "manifest" {
output = "manifest.json"
strip_path = true
}
}
GCP Image
source "googlecompute" "ubuntu" {
project_id = "my-project"
zone = "us-central1-a"
image_name = "myapp-${var.version}-{{timestamp}}"
source_image = "ubuntu-2204-jammy-v20240101"
ssh_username = "ubuntu"
}
Ansible Provisioner
provisioner "ansible" {
playbook_file = "ansible/playbook.yml"
extra_arguments = [
"--extra-vars", "env=production"
]
}
Commands
# Init (download plugins)
packer init aws-ami.pkr.hcl
# Validate
packer validate aws-ami.pkr.hcl
# Build
packer build -var "version=2.0.0" aws-ami.pkr.hcl
# Build with vars file
packer build -var-file="prod.pkrvars.hcl" aws-ami.pkr.hcl
Common Patterns
- Source + build: separate reusable source blocks
- Variables: parameterize for environments
- HCL2 functions:
timestamp(), uuid(), upper()
- Artifacts: output manifests for downstream automation
- CI integration: build images on release tags
How to Use
- Define infrastructure as code (Terraform, CloudFormation, Pulumi)
- Review changes through PR process before applying
- Configure monitoring and alerting for critical paths
- Set up secrets management (Vault, AWS Secrets Manager, etc.)
- Document runbooks for deployment, rollback, and incident response
- Test disaster recovery procedures regularly
Red Flags
- Infrastructure changes without review: Unreviewed changes cause outages — use PRs for infra code
- No rollback strategy: Every deployment needs a tested rollback plan before it runs
- Secrets in configuration files: Secrets in YAML/JSON get committed to version control
- Missing monitoring and alerting: Without monitoring, outages go undetected until users report them
- No documentation for runbooks: Without runbooks, on-call engineers waste time re-discovering procedures
Verification
Process
- Analyze the task requirements
- Apply domain expertise
- Verify output quality
Anti-Rationalization Table
| Rationalization |
Reality |
| "Manual deployments are fine" |
Manual deployments are error-prone and不可 repeatable. Automate. |
| "We do not need monitoring" |
Without monitoring, you are flying blind. Add observability from day one. |
| "Infrastructure as code is overkill" |
IaC enables reproducibility, version control, and disaster recovery. |
1---2name: packer-images3description: Use when hashiCorp Packer — machine image building, builders, provisioners, post-processors for AWS/GCP/Azure. Use when working with packer images.4license: Apache-2.05---6789## Overview1011Packer automates machine image creation for multiple platforms from a single source configuration. Supports AWS AMI, GCP images, Azure images, Docker, and more.1213## Capabilities1415- Multi-platform image building (AWS, GCP, Azure, Docker, VirtualBox)16- HCL2 configuration language17- Provisioners (shell, Ansible, Chef, Puppet)18- Post-processors (compress, manifest, Vagrant)19- Image testing with InSpec/Testinfra20- CI/CD integration for automated image pipelines2122## When to Use23**Trigger phrases:**24- "packer images"25- "HashiCorp Packer — machine image building, builders, provisioners, post-processo"262728- Building golden images for cloud deployments29- Immutable infrastructure patterns30- AMI/image creation in CI/CD pipelines31- Standardized base images across teams32- Compliance-hardened images3334## When NOT to Use3536- Task is outside your authorization scope37- You need to implement controls (use implementing-* skills)38- Task is about analysis, not action (use analyzing-* skills)39- You don't have access to target systems40- Task requires compliance expertise (consult professionals)41- Task is about defense, not offense (use defensive skills)424344## Pseudo Code4546The packer-images workflow follows a standard pipeline pattern.4748Core flow:49```50# packer-images primary flow51input = prepare(raw_data)52result = process(input, config={azure, builders, building, hashicorp, image})53validate(result)54deliver(result)55```5657Error handling:58```59on error:60 log(error_details)61 retry_with_backoff(max=3)62 if still_failing: alert_and_escalate()63```646566### AWS AMI67```hcl68# aws-ami.pkr.hcl69packer {70 required_plugins {71 amazon = {72 version = ">= 1.2.0"73 source = "github.com/hashicorp/amazon"74 }75 }76}7778variable "version" {79 type = string80 default = "1.0.0"81}8283source "amazon-ebs" "ubuntu" {84 ami_name = "myapp-${var.version}-{{timestamp}}"85 instance_type = "t3.micro"86 region = "us-east-1"87 source_ami_filter {88 filters = {89 name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"90 root-device-type = "ebs"91 virtualization-type = "hvm"92 }93 owners = ["099720109477"]94 most_recent = true95 }96 ssh_username = "ubuntu"97}9899build {100 sources = ["source.amazon-ebs.ubuntu"]101102 provisioner "shell" {103 inline = [104 "sudo apt-get update",105 "sudo apt-get install -y docker.io nginx",106 "sudo systemctl enable docker nginx"107 ]108 }109110 provisioner "file" {111 source = "configs/nginx.conf"112 destination = "/tmp/nginx.conf"113 }114115 provisioner "shell" {116 inline = ["sudo mv /tmp/nginx.conf /etc/nginx/nginx.conf"]117 }118119 post-processor "manifest" {120 output = "manifest.json"121 strip_path = true122 }123}124```125126### GCP Image127```hcl128source "googlecompute" "ubuntu" {129 project_id = "my-project"130 zone = "us-central1-a"131 image_name = "myapp-${var.version}-{{timestamp}}"132 source_image = "ubuntu-2204-jammy-v20240101"133 ssh_username = "ubuntu"134}135```136137### Ansible Provisioner138```hcl139provisioner "ansible" {140 playbook_file = "ansible/playbook.yml"141 extra_arguments = [142 "--extra-vars", "env=production"143 ]144}145```146147### Commands148```bash149# Init (download plugins)150packer init aws-ami.pkr.hcl151152# Validate153packer validate aws-ami.pkr.hcl154155# Build156packer build -var "version=2.0.0" aws-ami.pkr.hcl157158# Build with vars file159packer build -var-file="prod.pkrvars.hcl" aws-ami.pkr.hcl160```161162## Common Patterns163164- **Source + build**: separate reusable source blocks165- **Variables**: parameterize for environments166- **HCL2 functions**: `timestamp()`, `uuid()`, `upper()`167- **Artifacts**: output manifests for downstream automation168- **CI integration**: build images on release tags169170## How to Use1711721. Define infrastructure as code (Terraform, CloudFormation, Pulumi)1732. Review changes through PR process before applying1743. Configure monitoring and alerting for critical paths1754. Set up secrets management (Vault, AWS Secrets Manager, etc.)1765. Document runbooks for deployment, rollback, and incident response1776. Test disaster recovery procedures regularly178179## Red Flags180181- **Infrastructure changes without review**: Unreviewed changes cause outages — use PRs for infra code182- **No rollback strategy**: Every deployment needs a tested rollback plan before it runs183- **Secrets in configuration files**: Secrets in YAML/JSON get committed to version control184- **Missing monitoring and alerting**: Without monitoring, outages go undetected until users report them185- **No documentation for runbooks**: Without runbooks, on-call engineers waste time re-discovering procedures186187## Verification188189- [ ] Skill output matches expected behavior190191## Process1921931. Analyze the task requirements1942. Apply domain expertise1953. Verify output quality196197## Anti-Rationalization Table198199| Rationalization | Reality |200|---|---|201| "Manual deployments are fine" | Manual deployments are error-prone and不可 repeatable. Automate. |202| "We do not need monitoring" | Without monitoring, you are flying blind. Add observability from day one. |203| "Infrastructure as code is overkill" | IaC enables reproducibility, version control, and disaster recovery. |