47

Can I write to the container's local filesystem from a Google Cloud Function? AWS Lambda allows writing to /tmp:

Q: What if I need scratch space on disk for my AWS Lambda function?
Each Lambda function receives 500MB of non-persistent disk space in its own /tmp directory.

Is there something equivalent in GCF?

Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109

1 Answers1

61

Yes, the whole filesystem is writeable and mapped to memory. From https://cloud.google.com/functions/docs/concepts/exec#file_system:

The filesystem itself is entirely writeable (except for files used by the underlying OS) and is stored within the Cloud Functions instance's memory.

Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
  • Is there any way to deploy files into this tmp directory so that they can just be there at startup rather than having to download them from somewhere else first? – pete Jul 01 '17 at 06:21
  • 2
    No. But you can just bundle any files in your zip file, or, if they're too big, read from Cloud Storage. – Trevor Dixon Jul 02 '17 at 20:35
  • 1
    Do these go away when the function is complete? I have one function writing to /tmp and another trying to read from it, but the second one isn't finding the file – sonicblis Sep 22 '17 at 21:25
  • 8
    @sonicblis Each function executes in a different container and has its own isolated /tmp mount. Even subsequent calls to the same function will sometimes execute in a different container, so they'll have different /tmp mounts. So you can't use /tmp to communicate between functions. /tmp might be good for, for example, caching, or storing intermediate results from an external process. – Trevor Dixon Sep 22 '17 at 22:55
  • 3
    @Trevor-dixon I'm doing the same thing, but using /tmp to create the file and cloud storage to store it for the next read. – DaShaun Jan 19 '19 at 20:00
  • Hi is there a way to grab a downloadable link of a file stored in tmp folder? or perhaps send the file back using http response in the same call? – Umair M Mar 28 '20 at 01:28
  • @DaShaun, do you have the code for that ? Is there a limit to tmp size file we can write ? I'm running into the issue that my file is written in cloud function but is only 20 Bytes when it should be 1 Mo – foufrix Mar 30 '20 at 08:48
  • @foufrix I think this is the relevant bits you need: ``` const fs = require("fs"); const os = require('os'); const destFileName = '/tmp/timestamp'; const options = { destination: destFileName, }; function writeValToFile(val) { let writeStream = fs.createWriteStream(destFileName); writeStream.write('' + val + ''); writeStream.end(); }``` – DaShaun Apr 12 '20 at 06:57
  • what is the maximum space that can be allocated in /tmp folder in case of gcp cloud function execution ? – PRUDHVI CHOWDHARY NEKKALAPUDI Sep 08 '20 at 15:08
  • what is the maximum space that can be allocated in /tmp folder in case of gcp cloud function execution ? – PRUDHVI CHOWDHARY NEKKALAPUDI Sep 08 '20 at 15:19
  • @PRUDHVICHOWDHARYNEKKALAPUDI 500 mb – cjcurrie Nov 02 '20 at 22:22
  • @foufrix For how to move a file from container `/tmp` dir to the GCS bucket in Python runtime, see [Create new csv file in Google Cloud Storage from cloud function](https://stackoverflow.com/a/70624213/11154841). Also explained in that answer: perhaps you can use `gcsfs` Python package instead and directly save and load from the GCS? – questionto42 Jan 09 '22 at 22:56
  • 2
    Is this outdated? The doc above now says `The filesystem itself is entirely writeable (except for files used by the underlying OS) and is stored within the Cloud Functions instance's memory.` – Leonard Jun 27 '22 at 11:46