{"record":{"id":"af181106413fe581","repo":"hashicorp/terraform","slug":"failed-to-upload-script-v","errorCode":null,"errorMessage":"Failed to upload script: %v","messagePattern":"Failed to upload script: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/builtin/provisioners/remote-exec/resource_provisioner.go","lineNumber":270,"sourceCode":"\t\t<-cmdCtx.Done()\n\t\tcomm.Disconnect()\n\t}()\n\n\tfor _, script := range scripts {\n\t\tvar cmd *remote.Cmd\n\n\t\toutR, outW := io.Pipe()\n\t\terrR, errW := io.Pipe()\n\t\tdefer outW.Close()\n\t\tdefer errW.Close()\n\n\t\tgo copyUIOutput(o, outR)\n\t\tgo copyUIOutput(o, errR)\n\n\t\tremotePath := comm.ScriptPath()\n\n\t\tif err := comm.UploadScript(remotePath, script); err != nil {\n\t\t\treturn fmt.Errorf(\"Failed to upload script: %v\", err)\n\t\t}\n\n\t\tcmd = &remote.Cmd{\n\t\t\tCommand: remotePath,\n\t\t\tStdout:  outW,\n\t\t\tStderr:  errW,\n\t\t}\n\t\tif err := comm.Start(cmd); err != nil {\n\t\t\treturn fmt.Errorf(\"Error starting script: %v\", err)\n\t\t}\n\n\t\tif err := cmd.Wait(); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Upload a blank follow up file in the same path to prevent residual\n\t\t// script contents from remaining on remote machine\n\t\tempty := bytes.NewReader([]byte(\"\"))","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/builtin/provisioners/remote-exec/resource_provisioner.go#L252-L288","documentation":"The remote-exec provisioner's runScripts function wraps UploadScript failures with this message. After establishing a connection, it calls comm.UploadScript to copy each script to the remote machine at comm.ScriptPath(). Any error during the upload (SSH/WinRM transfer failure, disk full, permission denied on the remote temp directory) is wrapped and returned.","triggerScenarios":"Running a remote-exec provisioner where comm.UploadScript (resource_provisioner.go:269) fails. This happens after a successful connection but during the file transfer of the script to the remote machine. The remote temp directory may not exist, be unwritable, or the connection may drop mid-transfer.","commonSituations":"The remote machine's temporary directory (where ScriptPath points) does not exist or is not writable. The SSH connection is established but drops during transfer. Insufficient disk space on the remote for the script content. SELinux or AppArmor blocking writes to the default script path.","solutions":["Ensure the remote machine has a writable temp directory (typically /tmp on Linux).","Verify the connection user has write access to the default script path on the remote.","Check disk space on the remote with a pre-flight SSH check.","Stabilize the connection by increasing timeout or using a bastion host."],"exampleFix":"# before\nconnection {\n  type    = \"ssh\"\n  host    = var.ip\n  user    = \"ubuntu\"\n}\nprovisioner \"remote-exec\" {\n  inline = [\"echo hello\"]\n}\n\n# after — add timeout and verify remote /tmp\nconnection {\n  type    = \"ssh\"\n  host    = var.ip\n  user    = \"ubuntu\"\n  timeout = \"5m\"\n}\nprovisioner \"remote-exec\" {\n  inline = [\n    \"test -w /tmp || { echo '/tmp not writable'; exit 1; }\",\n    \"echo hello\"\n  ]\n}","handlingStrategy":"retry","validationCode":"// Before runScripts, verify remote connectivity and temp dir\n// (best done via a quick remote-exec inline check)\n// Check connection is alive\nif err := comm.Connect(nil); err != nil {\n    return fmt.Errorf(\"connection failed before upload: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"// Retry script upload\nfor i := 0; i < 3; i++ {\n    err := comm.UploadScript(remotePath, script)\n    if err == nil {\n        break\n    }\n    if i == 2 {\n        return fmt.Errorf(\"Failed to upload script after retries: %v\", err)\n    }\n    comm.Disconnect()\n    time.Sleep(time.Duration(1<<i) * time.Second)\n    comm.Connect(nil)\n}","preventionTips":["Ensure the remote machine has a writable temp directory (/tmp on Linux).","Set a generous connection timeout in the connection block.","Use a bastion host or VPN if the remote machine is behind a firewall.","Consider using 'inline' instead of 'script' to avoid file upload entirely."],"tags":["terraform","remote-exec","provisioner","ssh","upload","go"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}