hashicorp/packer · error
failed to write gzip raw data: %w
Error message
failed to write gzip raw data: %w
What it means
The `gzip()` HCL2 function compresses a string and the underlying gzip.Writer failed while writing the raw input data. This is a defensive wrap: writes into a bytes.Buffer essentially never fail, so the error is near-unreachable.
Source
Thrown at hcl2template/function/encoding.go:32
)
// Base64GzipFunc constructs a function that compresses a string with gzip and then encodes the result in
// Base64 encoding.
var Base64GzipFunc = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "str",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
s := args[0].AsString()
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte(s)); err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("failed to write gzip raw data: %w", err)
}
if err := gz.Flush(); err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("failed to flush gzip writer: %w", err)
}
if err := gz.Close(); err != nil {
return cty.UnknownVal(cty.String), fmt.Errorf("failed to close gzip writer: %w", err)
}
return cty.StringVal(base64.StdEncoding.EncodeToString(b.Bytes())), nil
},
})
// Base64Gzip compresses a string with gzip and then encodes the result in
// Base64 encoding.
//
// Packer uses the "standard" Base64 alphabet as defined in RFC 4648 section 4.
//
// Strings in the Packer language are sequences of unicode characters rather
// than bytes, so this function will first encode the characters from the stringView on GitHub (pinned to eb36e3c3e4)
Solutions
- Retry the packer command — transient memory pressure is the only plausible cause
- Inspect the wrapped error (%w) for the root cause
- Reduce template variable sizes if memory is constrained
Defensive patterns
Strategy: try-catch
Try / catch
// error surfaces during template evaluation; wrap the packer run
gz_b64=$(packer console <<< 'gzip("data")' 2>&1) || echo "gzip failed: $gz_b64" Prevention
- Retry on transient memory pressure
- Inspect the wrapped %w error for the real cause
- Keep gzip inputs to reasonable-size strings
When it happens
Trigger: Calling `gzip(someString)` in a template where gz.Write returns an error — practically only if the internal buffer write fails (out-of-memory-type conditions).
Common situations: Almost never hit in practice; the input is always a string coerced via AsString so no user input directly causes it.
Related errors
- failed to flush gzip writer: %w
- failed to close gzip writer: %w
- too many values, 1 needed: %v
- failed to read file %q: %s
- failed to write file %q as base64: %s
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/f91f52c566c8a8f0.
Report an issue: GitHub.