hashicorp/packer · error

ErrInvalidCompressionLevel

ErrInvalidCompressionLevel

Error message

Invalid compression level. Expected an integer from -1 to 9.

What it means

ErrInvalidCompressionLevel is a sentinel error returned by the compress post-processor's writer factories when the configured compression level is not accepted by the underlying compressor. Despite the message mentioning -1..9 (gzip/flate range), it is also returned by bgzf and lz4 when their level mapping fails.

Source

Thrown at post-processor/compress/post-processor.go:34

	"regexp"
	"runtime"

	"github.com/biogo/hts/bgzf"
	"github.com/dsnet/compress/bzip2"
	"github.com/hashicorp/hcl/v2/hcldec"
	"github.com/hashicorp/packer-plugin-sdk/common"
	packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
	"github.com/hashicorp/packer-plugin-sdk/template/config"
	"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
	"github.com/klauspost/pgzip"
	"github.com/pierrec/lz4/v4"
	"github.com/ulikunitz/xz"
)

var (
	// ErrInvalidCompressionLevel is returned when the compression level passed
	// to gzip is not in the expected range. See compress/flate for details.
	ErrInvalidCompressionLevel = fmt.Errorf(
		"Invalid compression level. Expected an integer from -1 to 9.")

	ErrWrongInputCount = fmt.Errorf(
		"Can only have 1 input file when not using tar/zip")

	filenamePattern = regexp.MustCompile(`(?:\.([a-z0-9]+))`)
)

type Config struct {
	common.PackerConfig `mapstructure:",squash"`

	// Fields from config file
	OutputPath       string `mapstructure:"output"`
	Format           string `mapstructure:"format"`
	CompressionLevel int    `mapstructure:"compression_level"`

	// Derived fields
	Archive   string

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Set compression_level to an integer in -1..9 (or omit it for the default)
  2. Check which algorithm you configured; confirm the level is valid for that specific algorithm (lz4 accepts only values present in its levels map)
  3. Re-run packer validate on the template

Example fix

// before
compression_level = 10
// after
compression_level = 6
Defensive patterns

Strategy: validation

Validate before calling

level := cfg.CompressionLevel // int
if level < -1 || level > 9 {
    return fmt.Errorf("compression_level %d out of range -1..9", level)
}

Try / catch

out, err := postProcessor.Configure(cfg)
if errors.Is(err, compress.ErrInvalidCompressionLevel) {
    // fall back to a safe default level
    cfg.CompressionLevel = -1
    out, err = postProcessor.Configure(cfg)
}

Prevention

When it happens

Trigger: 1) makeBGZFWriter: bgzf.NewWriterLevel rejects the CompressionLevel value. 2) makeLZ4Writer: CompressionLevel is not a key in the levels map (e.g. 10, or a value outside 0-9 for lz4 mapping). 3) makePgzipWriter: pgzip writer level out of gzip range.

Common situations: Setting compression_level to a value valid for one algorithm but not another (e.g. lz4 with level 10, or negative values other than -1); HCL config typo; copy-pasting a gzip-level value into an lz4/bgzf config.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/08efb4663ba65d5f. Report an issue: GitHub.