hashicorp/packer · error

Error uploading envVarFile: %s

Error message

Error uploading envVarFile: %s

What it means

After preparing the env-var temp file, Packer uploads it to the remote machine via the communicator's Upload inside a retry loop (provisioner/shell/provisioner.go:274). This error wraps a failed Upload of the var file to RemoteFolder. It means the communicator connection could not transfer the file — connection loss, authentication failure, or a bad remote path.

Source

Thrown at provisioner/shell/provisioner.go:274

		}

		p.config.envVarFile = tf.Name()

		// upload the var file
		var cmd *packersdk.RemoteCmd
		err = retry.Config{StartTimeout: p.config.StartRetryTimeout}.Run(ctx, func(ctx context.Context) error {
			if _, err := tf.Seek(0, 0); err != nil {
				return err
			}

			var r io.Reader = tf
			if !p.config.Binary {
				r = &UnixReader{Reader: r}
			}
			remoteVFName := fmt.Sprintf("%s/%s", p.config.RemoteFolder,
				fmt.Sprintf("varfile_%d.sh", rand.Intn(9999)))
			if err := comm.Upload(remoteVFName, r, nil); err != nil {
				return fmt.Errorf("Error uploading envVarFile: %s", err)
			}
			tf.Close()

			cmd = &packersdk.RemoteCmd{
				Command: fmt.Sprintf("chmod 0600 %s", remoteVFName),
			}
			if err := comm.Start(ctx, cmd); err != nil {
				return fmt.Errorf("Error chmodding script file to 0600 in remote machine: %s", err)
			}
			cmd.Wait()
			p.config.envVarFile = remoteVFName
			return nil
		})
		if err != nil {
			return err
		}
	}

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Verify communicator credentials and connectivity (ssh -i key user@host works manually).
  2. Confirm remote_folder exists and is writable by the SSH user.
  3. Increase start_retry_timeout so the retry loop survives slow reconnection after a reboot.
  4. Add a pause_after or ensure restart provisioners wait for connectivity before the shell provisioner runs.
  5. Run with PACKER_LOG=1 to inspect the underlying communicator error.

Example fix

// before
provisioner "shell" {
  use_env_var_file = true
}
// after — tolerate slow reconnects
provisioner "shell" {
  use_env_var_file    = true
  start_retry_timeout = "15m"
}
Defensive patterns

Strategy: retry

Validate before calling

// pre-flight connectivity check before packer build
nc -z -w5 "$SSH_HOST" 22 && echo ok || { echo "cannot reach $SSH_HOST:22"; exit 1; }

Try / catch

// the provisioner already retries within start_retry_timeout; surface the final error
if err := p.Provision(ctx, ui, comm, data); err != nil {
    var retriable = strings.Contains(err.Error(), "Error uploading envVarFile")
    if retriable {
        ui.Say("transient upload failure, re-running provisioner")
        // re-invoke or fail the build with context
    }
    return err
}

Prevention

When it happens

Trigger: comm.Upload(remoteVFName, ...) fails while retrying during Provision: SSH/WinRM connection dropped mid-provision, remote_folder is unwritable or missing, communicator credentials are wrong, or the remote machine was rebooted by a prior provisioner.

Common situations: A prior provisioner restarted the VM and the connection is still down; invalid ssh_private_key_file or wrong username; remote_folder pointing to a path that does not exist or lacks write permission; transient network flakiness on cloud instances.

Related errors


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