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,4 post-processors for AWS/GCP/Azure. Use when working with packer images.5domain: devops6author: oyi777license: Apache-2.08subdomain: devops9tags:10- aws11- azure12- ci-cd13- devops14- gcp15- images16- infrastructure17- packer18version: 1.0.019category: devops20metadata:21 source: https://skillmd.com/skills/oyi77/packer-images22---23242526## Overview2728Packer automates machine image creation for multiple platforms from a single source configuration. Supports AWS AMI, GCP images, Azure images, Docker, and more.2930## Capabilities3132- Multi-platform image building (AWS, GCP, Azure, Docker, VirtualBox)33- HCL2 configuration language34- Provisioners (shell, Ansible, Chef, Puppet)35- Post-processors (compress, manifest, Vagrant)36- Image testing with InSpec/Testinfra37- CI/CD integration for automated image pipelines3839## When to Use40**Trigger phrases:**41- "packer images"42- "HashiCorp Packer — machine image building, builders, provisioners, post-processo"434445- Building golden images for cloud deployments46- Immutable infrastructure patterns47- AMI/image creation in CI/CD pipelines48- Standardized base images across teams49- Compliance-hardened images5051## When NOT to Use5253- Task is outside your authorization scope54- You need to implement controls (use implementing-* skills)55- Task is about analysis, not action (use analyzing-* skills)56- You don't have access to target systems57- Task requires compliance expertise (consult professionals)58- Task is about defense, not offense (use defensive skills)596061## Pseudo Code6263The packer-images workflow follows a standard pipeline pattern.6465Core flow:66```67# packer-images primary flow68input = prepare(raw_data)69result = process(input, config={azure, builders, building, hashicorp, image})70validate(result)71deliver(result)72```7374Error handling:75```76on error:77 log(error_details)78 retry_with_backoff(max=3)79 if still_failing: alert_and_escalate()80```818283### AWS AMI84```hcl85# aws-ami.pkr.hcl86packer {87 required_plugins {88 amazon = {89 version = ">= 1.2.0"90 source = "github.com/hashicorp/amazon"91 }92 }93}9495variable "version" {96 type = string97 default = "1.0.0"98}99100source "amazon-ebs" "ubuntu" {101 ami_name = "myapp-${var.version}-{{timestamp}}"102 instance_type = "t3.micro"103 region = "us-east-1"104 source_ami_filter {105 filters = {106 name = "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*"107 root-device-type = "ebs"108 virtualization-type = "hvm"109 }110 owners = ["099720109477"]111 most_recent = true112 }113 ssh_username = "ubuntu"114}115116build {117 sources = ["source.amazon-ebs.ubuntu"]118119 provisioner "shell" {120 inline = [121 "sudo apt-get update",122 "sudo apt-get install -y docker.io nginx",123 "sudo systemctl enable docker nginx"124 ]125 }126127 provisioner "file" {128 source = "configs/nginx.conf"129 destination = "/tmp/nginx.conf"130 }131132 provisioner "shell" {133 inline = ["sudo mv /tmp/nginx.conf /etc/nginx/nginx.conf"]134 }135136 post-processor "manifest" {137 output = "manifest.json"138 strip_path = true139 }140}141```142143### GCP Image144```hcl145source "googlecompute" "ubuntu" {146 project_id = "my-project"147 zone = "us-central1-a"148 image_name = "myapp-${var.version}-{{timestamp}}"149 source_image = "ubuntu-2204-jammy-v20240101"150 ssh_username = "ubuntu"151}152```153154### Ansible Provisioner155```hcl156provisioner "ansible" {157 playbook_file = "ansible/playbook.yml"158 extra_arguments = [159 "--extra-vars", "env=production"160 ]161}162```163164### Commands165```bash166# Init (download plugins)167packer init aws-ami.pkr.hcl168169# Validate170packer validate aws-ami.pkr.hcl171172# Build173packer build -var "version=2.0.0" aws-ami.pkr.hcl174175# Build with vars file176packer build -var-file="prod.pkrvars.hcl" aws-ami.pkr.hcl177```178179## Common Patterns180181- **Source + build**: separate reusable source blocks182- **Variables**: parameterize for environments183- **HCL2 functions**: `timestamp()`, `uuid()`, `upper()`184- **Artifacts**: output manifests for downstream automation185- **CI integration**: build images on release tags186187## How to Use1881891. Define infrastructure as code (Terraform, CloudFormation, Pulumi)1902. Review changes through PR process before applying1913. Configure monitoring and alerting for critical paths1924. Set up secrets management (Vault, AWS Secrets Manager, etc.)1935. Document runbooks for deployment, rollback, and incident response1946. Test disaster recovery procedures regularly195196## Red Flags197198- **Infrastructure changes without review**: Unreviewed changes cause outages — use PRs for infra code199- **No rollback strategy**: Every deployment needs a tested rollback plan before it runs200- **Secrets in configuration files**: Secrets in YAML/JSON get committed to version control201- **Missing monitoring and alerting**: Without monitoring, outages go undetected until users report them202- **No documentation for runbooks**: Without runbooks, on-call engineers waste time re-discovering procedures203204## Verification205206- [ ] Skill output matches expected behavior207208## Process2092101. Analyze the task requirements2112. Apply domain expertise2123. Verify output quality213214## Anti-Rationalization Table215216| Rationalization | Reality |217|---|---|218| "Manual deployments are fine" | Manual deployments are error-prone and不可 repeatable. Automate. |219| "We do not need monitoring" | Without monitoring, you are flying blind. Add observability from day one. |220| "Infrastructure as code is overkill" | IaC enables reproducibility, version control, and disaster recovery. |