Skip to main content

Packer Commands

About the Language​

It is possible to use both JSON and HashiCorp's HCL2 language.

It is strongly recommended to use HCL2 instead of using JSON. HCL2 is very similar to Terraform.

The documentation will show examples in most cases in both languages, but HashiCorp itself recommends that those using JSON migrate to HCL2.

Some commands do not work with JSON files:

  • init
  • fmt
  • use of functions

A better explanation about the HCL model will be available in Template.

CLI Commands​

For easier use, install Packer autocomplete so that tab works and autocompletes commands.

packer -autocomplete-install

Take a tour with the packer CLI and see how simple it is

packer -h
Usage: packer [--version] [--help] <command> [<args>]

Available commands are:
build build image(s) from template
console creates a console for testing variable interpolation
fix fixes templates from old versions of packer
fmt Rewrites HCL2 config files to canonical format
hcl2_upgrade transform a JSON template into an HCL2 configuration
init Install missing plugins or upgrade plugins
inspect see components of a template
plugins Interact with Packer plugins and catalog
validate check that a template is valid
version Prints the Packer version

Init​

The packer init command is used to download Packer plugin binaries. This is the first command that should be run when working with a new or existing template. This command is always safe to run multiple times. Although subsequent runs may present errors, this command will never delete anything.

packer init only fetches binaries from public GitHub projects.

packer init -upgrade will update the plugins

We will talk about plugins later on

Format​

The packer fmt command is used to format HCL2 configuration files to a canonical format and style. JSON files are not modified.

Good to be used in pipelines.

# Only shows files that will have differences for files in the current directory
packer fmt -check .
aws.pkr.hcl

# Makes a diff and shows what will happen to files in the current directory
packer fmt -diff .
aws.pkr.hcl
--- old/aws.pkr.hcl
+++ new/aws.pkr.hcl
@@ -25,7 +25,7 @@

build {
sources = ["source.amazon-ebs.teste"]
-
+
provisioner "file" {
destination = "/tmp"
source = "./teste"

Validate​

The packer validate command is used to validate the syntax and configuration of a template. The command will return a zero exit status on success and a non-zero exit status on failure.

If all variables have their default value or are statically defined, only the packer validate command will be sufficient.

However, if any variable does not have the default value defined, it is necessary to pass either the variable value or the variable configuration file (which is most recommended)

# command executed within the directory containing the templates
packer validate .
The configuration is valid.

# passing a variable values configuration file
packer validate -var-file="./vars/myproject.auto.pkrvars.hcl" .
The configuration is valid.

# or syntax only
packer validate -syntax-only .
Syntax-only check passed. Everything looks okay.

Build​

The packer build command takes a template and runs all the builds within it to generate a set of artifacts. The various builds specified in a template are executed in parallel, unless otherwise specified. And the artifacts created will be generated at the end of the build.

For any command below, the -debug parameter can be used to help troubleshoot.

As mentioned, parallel execution is possible if you are, for example, creating an image for AWS and Azure at the same time and this is enabled by default, but it can be disabled if necessary.

I really like to redirect the output to a log file using a | tee > build.log at the end of the command. The terminal ends up losing some important lines and it's good to have the entire output available for analysis.

packer build -var-file="./vars/test.auto.pkrvars.hcl" . | tee log
amazon-ebs.teste: output will be in this color.

==> amazon-ebs.teste: Prevalidating any provided VPC information
==> amazon-ebs.teste: Prevalidating AMI Name: teste-20230304194337
amazon-ebs.teste: Found Image ID: ami-09a187f07b7a0769e
==> amazon-ebs.teste: Creating temporary keypair: packer_64039f69-6288-44c4-8806-cf8929a15f90
==> amazon-ebs.teste: Creating temporary security group for this instance: packer_64039f6e-afba-1c56-8ddb-d7adfcc6da30
...

Packer build takes one argument. When a directory is passed, all files in the folder with a name ending with .pkr.hcl or .pkr.json will be parsed. When a file is passed, only this one will be parsed.