Cloud Functions run in a fully-managed, serverless environment where Google handles infrastructure, operating systems, and runtime environments completely on your behalf. Each Cloud Function runs in its own isolated secure execution context, scales automatically, and has a lifecycle independent from other functions.
Things to know before starting
There are two types of Cloud Functions,
Google Cloud Functions, which allow you to run snippets of code in Google’s infrastructure in response to events.
Cloud Functions for Firebase, which triggers Google Cloud Functions based on events in Firebase (such as database or file writes, user creation, etc)
Functions can be triggered by HTTP, publishing a message to Pub/Sub topic, based on events in Firebase/Firestore, Cloud Scheduler, Cloud Storage. Functions can be categorized as two types, i) HTTP triggers and ii) Background functions.
Functions must be stateless — one function invocation should not rely on in-memory state set by a previous invocation.
Each instance of a function handles only one concurrent request at a time.
If there is extremely rapid increase in inbound traffic can intermittently cause some requests to fail with an HTTP code of 500. This is because the requests are timing out in the pending queue while waiting for new instances to be created.
Cold starts may create latency problems this happens during new deployment and creating new instances during scaling.
Global Scope in a function is executed only once, when its first invoked. In a situation where the same funtion is reused for another execution, the variables you had set in global scope would not be set/triggered. So, write programs in such a way, it should not depend on global variables (or) reinitialize the variable at the end of execution.
At certain times, functions might be triggered more than once, your application should be able to handle that. You functions should be idempotent, an identical request can be made once or several times in a row with the same effect while leaving the server in the same state.
By default, a function times out after 1 minute, but you can extend this period up to 9 minutes(Add flag —timeout=540 during gcloud functions deploy)
/tmp/ path can be used for local storage during execution but be aware it takes up the space which you had allocated in memory default is 256MB. When CF completes its execution the files in /tmp/ are not deleted automatically. So, in a scenario where, if the same instance of the CF is used by the next request, memory can be used up by the files you had created in the previous innovation. Have a practice of cleaning up /tmp/ before program completion. To increase memory add flag --memory=512MB during gcloud functions deploy.
To share data between functions, use Firebase, Cloud Storage and Datastore do not use /tmp/.
When there is a version migration of any languages, you will be notified about it in a mail and there will be a mention of whether its going to be minor or major and a deadline will be given past that deadline, your function may be redeployed. If alls well, it will work, otherwise it will fail. So recommendation is, when you get a mail prepare for it. See the features of newer versions and google with keywords like “Migrating Cloud Functions Runtimes” and know the differences as some things might not be backward compatible.
For NodeJS, GCP version migration happens in even number like NodeJS 12 → NodeJS 14 → NodeJS 16
Timestamps and Timezone used in Cloud Function is UTC. So if your program uses datetime, you need to make your program timezone aware.
Writing a Python Cloud Function
Python and Go Admin SDKs, all write methods are blocking. That is, the write methods do not return until the writes are committed to the database.
This reduces a lot of my code. Seriously, i was doing a lot to make things synchronous in my NodeJS Firebase CF’s earlier.
Structuring your folder path
Below is the recommended folder path structure to create cloud function
fn-function_name
├── main.py <-- Your cloud function's entrypoint should be defined in this file
├── requirements.txt <-- Specifies all the program dependencies
└── localPkg/
├── __init__.py <-- adding this file turns localPkg folder into a module
└── myFunctions.py <-- Contains your helper functions which is called from main.py in root directory.
Python Environment Setup
Simple Python Program Template
Below is the code in sub-folder localPkg
This is the myFunctions module
Sample Python Cloud Function Program
This cloud function does the following,
Download the bhavcopy file from Bombay Stock Exchange to cloud functions local folder /tmp/