{"record":{"id":"4598a213a09538dc","repo":"hashicorp/terraform","slug":"error-creating-temporary-file-for-upload-s","errorCode":null,"errorMessage":"Error creating temporary file for upload: %s","messagePattern":"Error creating temporary file for upload: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/communicator/ssh/communicator.go","lineNumber":659,"sourceCode":"\t\treturn errors.New(string(message))\n\t}\n\n\treturn nil\n}\n\nvar testUploadSizeHook func(size int64)\n\nfunc scpUploadFile(dst string, src io.Reader, w io.Writer, r *bufio.Reader, size int64) error {\n\tif testUploadSizeHook != nil {\n\t\ttestUploadSizeHook(size)\n\t}\n\n\tif size == 0 {\n\t\t// Create a temporary file where we can copy the contents of the src\n\t\t// so that we can determine the length, since SCP is length-prefixed.\n\t\ttf, err := ioutil.TempFile(\"\", \"terraform-upload\")\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"Error creating temporary file for upload: %s\", err)\n\t\t}\n\t\tdefer os.Remove(tf.Name())\n\t\tdefer tf.Close()\n\n\t\tlog.Println(\"[DEBUG] Copying input data into temporary file so we can read the length\")\n\t\tif _, err := io.Copy(tf, src); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\t// Sync the file so that the contents are definitely on disk, then\n\t\t// read the length of it.\n\t\tif err := tf.Sync(); err != nil {\n\t\t\treturn fmt.Errorf(\"Error creating temporary file for upload: %s\", err)\n\t\t}\n\n\t\t// Seek the file to the beginning so we can re-read all of it\n\t\tif _, err := tf.Seek(0, 0); err != nil {\n\t\t\treturn fmt.Errorf(\"Error creating temporary file for upload: %s\", err)","sourceCodeStart":641,"sourceCodeEnd":677,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/communicator/ssh/communicator.go#L641-L677","documentation":"Raised in scpUploadFile when ioutil.TempFile fails to create the staging file used to determine length for zero-size inputs (SCP is length-prefixed). The temp file lives in the OS default temp dir (os.TempDir()). A failure here is an environment problem on the Terraform-running machine, not the remote host.","triggerScenarios":"The local temp directory does not exist or is not writable, the disk is full, the process hit an open-file limit, or a security policy blocks temp file creation. Triggered specifically when uploading a zero-length file (size == 0) so Terraform must buffer it to compute length.","commonSituations":"Running Terraform in a container/CI with a read-only or size-limited TMPDIR; disk pressure on the runner; ulimit -n too low; SELinux denying tempfile creation.","solutions":["Ensure TMPDIR/TEMP is writable and has free space (df -h $TMPDIR; touch $TMPDIR/tf-test).","Free disk space or raise the container's tmpfs size.","Raise the open-file ulimit if 'too many open files' appears in the wrapped error.","If possible, avoid uploading genuinely empty files (omit the resource or supply minimal content)."],"exampleFix":"// before\nprovisioner \"file\" {\n  source      = \"empty.txt\"   // 0-byte file\n  destination = \"/etc/app/empty.txt\"\n}\n\n// after\nprovisioner \"file\" {\n  source      = \"config.txt\"  // non-empty\n  destination = \"/etc/app/config.txt\"\n}","handlingStrategy":"validation","validationCode":"# Before apply, confirm TMPDIR is writable with free space:\n#   df -h \"${TMPDIR:-/tmp}\" && touch \"${TMPDIR:-/tmp}/tf-probe\" && rm \"${TMPDIR:-/tmp}/tf-probe\"\n# In CI, mount a writable tmpfs/emptyDir of adequate size.","typeGuard":null,"tryCatchPattern":"// In Go, if you control the upload path, skip staging for known sizes:\nif size > 0 {\n    return scpUploadFile(dst, src, w, r, size)\n}\n// otherwise surface tempfile errors with environment guidance:\nif err != nil && strings.Contains(err.Error(), \"temporary file for upload\") {\n    return fmt.Errorf(\"cannot stage upload; check TMPDIR/space/ulimit: %w\", err)\n}","preventionTips":["Set TMPDIR to a writable volume with free space in CI/containers.","Avoid uploading zero-byte files.","Raise ulimit -n where many files are uploaded."],"tags":["terraform","ssh","scp","upload","tempfile","filesystem","provisioner"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}