Skip to content
Skillv1.0.0

packer

HashiCorp Packer for building automated machine images. Use when the user needs to create AMIs, Docker images, or GCP images using HCL templates with provisioners for consistent, reproducible infrastr

by terminalskills(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from terminalskills/skills (skills/packer/SKILL.md). Install upstream with npx skills add terminalskills/skills --skill packer. Copyright stays with the author (Apache-2.0).

Packer

Packer automates the creation of machine images for multiple platforms from a single source configuration.

Installation

# Install Packer
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install packer

# Verify
packer version

AWS AMI Builder

# aws-ubuntu.pkr.hcl — Build Ubuntu AMI with Nginx
packer {
  required_plugins {
    amazon = {
      version = ">= 1.2.0"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

variable "aws_region" {
  type    = string
  default = "us-east-1"
}

variable "app_version" {
  type = string
}

source "amazon-ebs" "ubuntu" {
  ami_name      = "app-{{timestamp}}"
  instance_type = "t3.medium"
  region        = var.aws_region

  source_ami_filter {
    filters = {
      name                = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }

  ssh_username = "ubuntu"

  tags = {
    Name        = "app-${var.app_version}"
    Environment = "production"
    Builder     = "packer"
  }
}

build {
  sources = ["source.amazon-ebs.ubuntu"]

  provisioner "shell" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install -y nginx curl jq",
      "sudo systemctl enable nginx"
    ]
  }

  provisioner "file" {
    source      = "files/nginx.conf"
    destination = "/tmp/nginx.conf"
  }

  provisioner "shell" {
    inline = [
      "sudo mv /tmp/nginx.conf /etc/nginx/sites-available/default",
      "sudo nginx -t"
    ]
  }

  provisioner "shell" {
    inline = ["sudo apt-get clean", "sudo rm -rf /var/lib/apt/lists/*"]
  }

  post-processor "manifest" {
    output     = "manifest.json"
    strip_path = true
  }
}

Docker Builder

# docker-app.pkr.hcl — Build Docker image with Packer
source "docker" "app" {
  image  = "python:3.12-slim"
  commit = true

  changes = [
    "EXPOSE 8080",
    "WORKDIR /app",
    "CMD [\"python\", \"app.py\"]"
  ]
}

build {
  sources = ["source.docker.app"]

  provisioner "shell" {
    inline = [
      "pip install --no-cache-dir flask gunicorn",
      "mkdir -p /app"
    ]
  }

  provisioner "file" {
    source      = "app/"
    destination = "/app/"
  }

  post-processor "docker-tag" {
    repository = "myregistry.com/myapp"
    tags       = ["latest", var.app_version]
  }

  post-processor "docker-push" {
    login          = true
    login_server   = "myregistry.com"
    login_username = var.registry_user
    login_password = var.registry_pass
  }
}

GCP Image Builder

# gcp-image.pkr.hcl — Build GCP Compute Engine image
source "googlecompute" "ubuntu" {
  project_id   = var.gcp_project
  source_image = "ubuntu-2204-jammy-v20240101"
  zone         = "us-central1-a"
  machine_type = "e2-medium"
  ssh_username = "packer"
  image_name   = "app-{{timestamp}}"

  image_labels = {
    environment = "production"
    builder     = "packer"
  }
}

build {
  sources = ["source.googlecompute.ubuntu"]

  provisioner "shell" {
    scripts = [
      "scripts/base-setup.sh",
      "scripts/install-app.sh"
    ]
    environment_vars = [
      "APP_VERSION=${var.app_version}"
    ]
  }

  provisioner "ansible" {
    playbook_file = "ansible/configure.yml"
  }
}

Multi-Builder Template

# multi-platform.pkr.hcl — Build for AWS and GCP simultaneously
variable "app_version" {
  type    = string
  default = "1.0.0"
}

source "amazon-ebs" "base" {
  ami_name      = "app-${var.app_version}-{{timestamp}}"
  instance_type = "t3.medium"
  region        = "us-east-1"
  source_ami_filter {
    filters = { name = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*" }
    most_recent = true
    owners      = ["099720109477"]
  }
  ssh_username = "ubuntu"
}

source "googlecompute" "base" {
  project_id   = var.gcp_project
  source_image = "ubuntu-2204-jammy-v20240101"
  zone         = "us-central1-a"
  ssh_username = "packer"
  image_name   = "app-${var.app_version}-{{timestamp}}"
}

build {
  sources = [
    "source.amazon-ebs.base",
    "source.googlecompute.base"
  ]

  provisioner "shell" {
    script = "scripts/setup.sh"
  }
}

Common Commands

# Validate template
packer validate .

# Build image
packer build .
packer build -var "app_version=2.0.0" aws-ubuntu.pkr.hcl

# Build specific source only
packer build -only="amazon-ebs.ubuntu" .

# Initialize plugins
packer init .

# Format HCL files
packer fmt .

# Inspect template
packer inspect .

# Debug mode
PACKER_LOG=1 packer build .

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/terminalskills-skills-packer/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

terminalskills-skills-packer.ocm.jsonjson
{
  "ocm": "1",
  "id": "terminalskills-skills-packer",
  "kind": "skill",
  "name": "packer",
  "description": "HashiCorp Packer for building automated machine images. Use when the user needs to create AMIs, Docker images, or GCP images using HCL templates with provisioners for consistent, reproducible infrastructure images.",
  "publisher": "terminalskills",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "coding"
    ],
    "tags": [
      "skill-md",
      "packer",
      "machine-images",
      "ami",
      "docker",
      "infrastructure",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "HashiCorp Packer for building automated machine images. Use when the user needs to create AMIs, Docker images, or GCP images using HCL templates with provisioners for consistent, reproducible infrastructure images."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/terminalskills/skills",
      "path": "skills/packer/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/terminalskills/skills/blob/HEAD/skills/packer/SKILL.md",
      "key": "terminalskills/skills/skills/packer/SKILL.md"
    },
    "compatibility": "linux, macos, windows",
    "license": "Apache-2.0"
  },
  "instructions": "# Packer\n\nPacker automates the creation of machine images for multiple platforms from a single source configuration.\n\n## Installation\n\n```bash\n# Install Packer\nwget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg\necho \"deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main\" | sudo tee /etc/apt/sources.list.d/hashicorp.list\nsudo apt update && sudo apt install packer\n\n# Verify\npacker version\n```\n\n## AWS AMI Builder\n\n```hcl\n# aws-ubuntu.pkr.hcl — Build Ubuntu ",
  "cost": {
    "context_tokens": 1210
  }
}

Fetch it by URL: GET /api/v1/registry/terminalskills-skills-packer/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.