GCLOUD_PROJECT Environment Variable Missing, or my Brief Introduction to Google Serverless
TL;DR the solution to the missing variable is at the very end.
I have an admission to make: I dislike Command-line Interface (CLI). I'm a Graphical User Interface (GUI) person. This is perhaps the root cause why I still believe Azure is a superior cloud offering to AWS…it's just a story of Windows GUI vs. Unix shell.
I like GUI because it shows all the options right there on the screen, no need to scroll through all the possible commands and memorize their syntax.
Click-click-click and you are done. Yes yes, GUI is hard to automate; there is a whole RPA industry being built on that tiny bit of problem…
Anyhow, the other day I was looking into using Google's Firebase to create a backend for my Android mobile application (side)project. Android is Google, so why not use their cloud and save time on the integration part…it Should Just Work right?
Everything was going swell, as long as I was sticking to authentication and NoSQL. Integration with Android was a dream…so much win.
However, I wanted to build a serverless function to deal with adding new users to the database. So there I was, wandering off the beaten path. Upgraded from the free tier, found my way to the Google Cloud Platform (GCP).
Alright! Here we go! Create Function!
- Found some (meanwhile outdated) source code in a long forgotten (but official) YouTube video tutorial.
- Realized the cloud execution environment does not have the required packages to run node.js…what?
- I opened a cloud shell window in the GCP and used the same npm commands that the how-to guides recommend to set up the local execution environment, to install the missing node.js packages…it was not in the documentation that this should be also done in GCP shell.
- Five hours later I have the code, the dependencies and the cloud execution environment squared away and my function has a green checkmark.
- Then it dies. Error: process.env.GCLOUD_PROJECT is not set.
- I google around a bit; this is a known issue with node.js 10 since 2019… what? I google a bit more, you should revert back to node.js 8…nope, node.js 8 is no longer supported and being discontinued.
- Someone else makes a snarky remark on GitHub; you should always use the CLI to deploy the functions; the CLI will set everything up for you…
Aha! I understand now! I did not follow the Happy Flow™ and now I'm paying for it.
Spoiler Alert: no, the CLI did not set up the GCLOUD_PROJECT variable on its own, after I went through the steps of setting everything up on my laptop and re-deployed the function from there. You need to explicitly specify that it should, in the parameters.
Anyhow, the GUI solution, for future generations:
- Edit your function (if already deployed)
- Under Environment Variables find Runtime environment variables
- Add the key-value pair where Name is GCLOUD_PROJECT and the value is your Firebase Project ID (you can look this up on the Firebase console)
Why is this not done automatically?
Why isn't this documented?