Deploy Node & Python on Google Cloud function- a first attempt but NOT successful

I went to a python meetup where one of the presentors was running a Python app using a google cloud function (GCF). I had to have it explained to me what these were, the thicko in the room. It ends up as an online environment that will run your code. It is dormant until you trigger it (eg via a HTTP request) at which point it runs the code that you have on the function. Open source FaaS (function as a service) framework to run functions across multiple environments.

There are Node.JS and Python 3.7 functions that you can run, and they give you a URL to point to for the function.

I was watching a node JS vid and saw the chap use GCF and I was keen to give it a crack. I’d set up a Google Cloud account (there are free services but you have to give your credit card details for the account and billing) when I was playing with the data collection app Collect, so used that account to setup a function. I got as far as getting the Hello World test to run on the app but when I tried to run the function from the given URL I got a 403 Forbidden .

You have to make sure, when setting up the function that you allow, under authorisation that you Allow Unorthenticated Invocations – for the testing. I may explore other alternatives later. If you do this then the URL will let you run the code.

Project- Selfie App from code Train

After getting the basic function working with the “Hello World” test and also testing the URL link, which is a HTTPS link, so I can try and deploy the Selfie App that I deployed to Heroku previously.

Why would I want to test that app? Well, one reason for the test is that the database is a file, once deployed on Heroku I couldn’t access that file or coy info from within the file. So my thinking is I’d test the GCF and see if I could access that file so be able to use that data elsewhere.

To upload the project I zipped up all the directory and used Zip upload (4 in image above) and it asks you for a cloud storage location. I had a cloud store from another project and used that (7). I poiinted to index.js (8) for the function I wanted to run.

I then tried to run the function & just got errors. It tells you you have an error and points to a log that isn’t created.

So a quick deployment did not happen. I think there are a couple of things that I need to check, in the Heroku deployment I had to point to somewhere where Heroku would choose a port:

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`listening at port ${port}`));

There is, in GCF a place where you can add environmental variables.

Also, on the function edit image above the project is being stored as a ZIP file in the storage . So that will make it difficult to write to that db file if its zipped up. Also hard to access it as well.

Another item that may be an issue is that the code was built up from Node version 12 on my PC, so the packages may not be the appropriate ones for the node version 10 in the app.

I had thought of creating new files with the inline editor, but it doesn’t let you do that, as shown below.

End comment

I was off on another project for a couple of days, so want to finish this article as I’ve broken my workflow.

At first look this seems to be great, but doing a simple project that I have running on Heroku does not deploy that easily on GCF. The project was designed for Heroku so that is OK. It was just good to test similarities/differences.

The demo I saw was for a python piece of code to run on GCF and I do not think there is as much issue with version control as there may be for the Node js.

At present I don’t want to get tangled with versioning in Node so I’ll put this aside for a later time. I can’t recollect that being an issue on Heroku.