Skip to content
Latchkey

packer build Command Reference

Build machine images from a Packer template.

packer build runs the builders and provisioners in a template to produce one or more machine images (for example an AMI). In CI it bakes images from versioned templates non-interactively.

What it does

packer build launches a temporary build instance, runs provisioners to configure it, and snapshots the result into an image artifact. The same template can target multiple platforms, producing parallel artifacts in one run.

Common flags and usage

  • -var KEY=VALUE: set a single template variable
  • -var-file FILE: load variables from a file
  • -only / -except: build a subset of sources
  • -force: delete and rebuild an existing artifact
  • -color=false: disable color for clean CI logs
  • run packer init first to install required plugins

Example

shell
- name: Build AMI
  env:
    AWS_REGION: us-east-1
  run: |
    packer init .
    packer build -color=false \
      -var "region=us-east-1" \
      -var "version=${{ github.sha }}" \
      runner-ami.pkr.hcl

In CI

Run packer init before build to install plugins, then build with -color=false for readable logs and -var/-var-file to inject a version tag. Authenticate the builder via OIDC (AWS) rather than static keys, and pass an immutable version so each image is traceable to a commit.

Key takeaways

  • packer build provisions a temporary instance and snapshots an image.
  • Run packer init first, then build with -color=false and -var for CI.
  • Tag images with an immutable version tied to the commit for traceability.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →