hashicorp/terraform · error

Failed to upload script: %v

Error message

Failed to upload script: %v

What it means

Returned by runScripts in remote-exec at resource_provisioner.go:270 when comm.UploadScript(remotePath, script) fails while pushing a script to the remote host. The connection is already established; the failure is in transferring script bytes to comm.ScriptPath().

Source

Thrown at internal/builtin/provisioners/remote-exec/resource_provisioner.go:270

		<-cmdCtx.Done()
		comm.Disconnect()
	}()

	for _, script := range scripts {
		var cmd *remote.Cmd

		outR, outW := io.Pipe()
		errR, errW := io.Pipe()
		defer outW.Close()
		defer errW.Close()

		go copyUIOutput(o, outR)
		go copyUIOutput(o, errR)

		remotePath := comm.ScriptPath()

		if err := comm.UploadScript(remotePath, script); err != nil {
			return fmt.Errorf("Failed to upload script: %v", err)
		}

		cmd = &remote.Cmd{
			Command: remotePath,
			Stdout:  outW,
			Stderr:  errW,
		}
		if err := comm.Start(cmd); err != nil {
			return fmt.Errorf("Error starting script: %v", err)
		}

		if err := cmd.Wait(); err != nil {
			return err
		}

		// Upload a blank follow up file in the same path to prevent residual
		// script contents from remaining on remote machine
		empty := bytes.NewReader([]byte(""))

View on GitHub (pinned to c9def3e214)

Solutions

  1. Verify the connection user can write to comm.ScriptPath() (usually the user's home/temp).
  2. Free disk space on the remote and retry.
  3. Stabilize the connection (bastion, timeouts) if sessions drop mid-upload.

Example fix

// before
connection { user = "appuser" host = aws_instance.web.public_ip }
provisioner "remote-exec" { scripts = ["run.sh"] } // Failed to upload script

// after: use a user with a writable home dir and explicit timeout
connection { user = "ec2-user" host = aws_instance.web.public_ip timeout = "5m" }
provisioner "remote-exec" { scripts = ["${path.module}/run.sh"] }
Defensive patterns

Strategy: retry

Validate before calling

// Confirm the remote upload target dir is writable.
cmd := &remote.Cmd{Command: "test -w \"$(dirname " + remotePath + ")\""}
if err := comm.Start(cmd); err != nil { return err }

Try / catch

var err error
for i := 0; i < 3; i++ {
    if err = comm.UploadScript(remotePath, script); err == nil { break }
    if !isTransientCommError(err) { return fmt.Errorf("Failed to upload script: %v", err) }
    time.Sleep(backoff(i))
}

Prevention

When it happens

Trigger: remote-exec provisioner reaches the upload step after connecting; UploadScript returns an error (permissions, disk, dropped session).

Common situations: Remote script upload directory not writable; disk full on the instance; SSH session interrupted; SELinux/AppArmor blocking execution of uploaded files.

Related errors


AI-assisted analysis of hashicorp/terraform@c9def3e214 (2026-08-07). Data as JSON: /api/errors/af181106413fe581. Report an issue: GitHub.