Google Cloud Functions, Serverless and Python

Diary Note turns into Article

It was supposed to be something short, but it became an article, since that the topic could be quite extensive. It might be a tour for you, and I hope you might find something useful. It also has a tutorial with a lot of images and few text (don't worry). Here we go.

First Steps with Cloud Functions

I recently started to work with Google Cloud solutions, specially using Cloud Functions, for a US based client, developing some components for its COVID-19 assessment and notification system. My experience with Google Cloud solutions was equivalent to 0 before this project. I was quite used with AWS for the last 2 years (EC2, S3, RDS, Lambda, etc.), and I felt a little bit lost at the beginning and to be honest, unhappy. It was quite of a journey, to bring things fast on a platform which I just started to work with and the pressure for releasing the product, was something to add up on an almost traumatic process. Fortunately, the day to day experience made me able to get through it successfully.

As a side task, I decide to create my own Google Cloud Free Trial account, make some tests and understand a little bit more about Google Cloud, on a sandbox environment. While I still have some credits, I decided to bring some tips and instructions, which can help someone with creating its own Google Cloud Free Trial account and deploy a simple Python-based Cloud Function.

What if Google charges me all of sudden?

Unlikely to happen. Google Cloud Free Trial is provided with U$ 300 for 90 days of usage. Which ever comes first, spending all the credits or reaching 90 days of usage, you won't have charges on your credit/debit card. The access to Google Cloud will be blocked and only when you decide to unblock it, you'll be charged, depending on usage of the service and its pricing. While setting up you're first Cloud Function, you have to enable billing, but don't worry, since that the billing will only consume the credits from your Free Trial.

Can I make big things with Cloud Functions?

Although I'm presenting just the snippet from Cloud Function editor (and also a small modification of this), you can have key components of your platform running as a Cloud Function, without setting up a server and setting up a lot of stuff. Using this, you're starting to have Microservices/Serverless oriented architecture, in other words. In a real case scenario, you can have a Cloud Function which receives a POST or GET request (Query String) from your main web application, and with the right logic, your Cloud Function can use the information provided on the request to lookup for some specific data from your database system, and do some businness rule with this like sending an email with a report to a costumer or notifying your main web application about the end of service for a user which has an expired service. And for such scenarios, I recommend to configure authentication on your Cloud Function in order to safely access its URL.

You Have to Know About Flask

Flask is a microframework for developing web applications and web services based on Python. It's very simplistic and easy to manage, letting you start little by little, instead of all steps and application structure that Django demands. Both have its own bennefits, but Flask is the framework which will handle HTTP requests to your Python-based Cloud Function. Here's a taste of a minimal Flask application and the handling of HTTP methods. You might also want to understand a little bit about flask.request object, which handles the HTTP request performed to your Cloud Function, which is also the parameter of your entrypoint function.

So we don't need to have Servers anymore ?

Besides being Software Developer, I'm experienced on infrastructure services and can guarantee, that a server designed and configured by an experienced System Administrator, could be much more secure, reliable and faster than a Cloud Function, allowing you to debug, secure, identify and improve your application performance with a flexibility that a Cloud Function won't provide to you. But you need someone to deal with this and the costs might be different from a Cloud Function. To have or not to have Serverless architecture, demands a deep look on all pros and cons, instead of following the buzz of Serverless and Microservices that Google, AWS, etc. makes. Might be a good thing for companies which don't have one or two individuals dedicated to infrastructure or startups, but would it be the right thing to your business?

Requesting Free Trial

Access Google Cloud and click on "Get started for free":

After login, a screen similar to this one will be prompted:

Navigate to Google Cloud Functions option:

Enable billing option (CTRL + click, for opening another window):

Come back to the Google Cloud Function window, reload it and start to create a function:


Setup a Google Cloud Function

Configure the name of the Cloud Function and allow unanthenticated access for its URL:

Configure the memory allocated for the Cloud Function (see pricing and capacities):

For now, it won't be necessary to setup environment variables:

Select Python on the Runtime menu:

No need to define additional packages on requirements.txt. Click on DEPLOY button:

Wait for the green check of the DEPLOY and click on the created function:

Click on TRIGGER menu and click on the URL defined to your Cloud Function:

Or perform a request from your terminal with curl:

For simulating a POST request or a GET with a Query String, you can do something like this:

On the screen where the URL is provided, click on VIEW LOGS for observing the activity of the HTTP requests performed at the Cloud Function:

Environment Variables

Using Environment Variables is a power up to your code and also to your Cloud Function. You can provide dynamic changes on your code, like setting up a different database address and credentials, or turning on/off other functionalities from your Flask app. For your local environment, it would be better to have all the Environment Variables necessary to your app, defined on a .env file, loading it via Python Dotenv. Here's an example below, using the same code, but defining an Environment Variable and obtaining its content inside the Cloud Function, just as you might normally do on your local environment.

After clicking on your Cloud Function, follow this click path and define your Environment Variables: EDIT => VARIABLES, NETWORKING AND ADVANCED SETTINGS => ENVIRONMENT VARIABLES:

Modify your Cloud Function by importing os library, then obtain the content of your defined Environment Variables via os.getenv(). After editing, click DEPLOY again for updating the Cloud Funtion:

Performing a simple GET request, without a query string, will bring the environemt variable DEFAULT MESSAGE. For POST with application/json as Content-Type and GET with Query String "message", it will bring the message sent via the request:


Have a mockup before a Cloud Function

Being on dev, stage or production environment, it's a good thing to guarantee that everything is working fine locally, before moving on with your Cloud Function. Here's a Gist for helping out with mocking up your Cloud Function:

Final Words

Indeed, relatively easy to setup a Cloud Function and a handy solution in order to bring up your component alive, without dealing with all the setups on an instance/server. Serverless architectures are here to stay and spreading very fast. From this Python-based Cloud Function example, Thousands of products from startups to established businesses, are delivering great solution on a Microservices oriented architecture, and taking advantage of what it has to offer, but it's not the end of the classic Server architecture. Remember, that although a Serverless architecture is easy to deal, maintain and also to scale, it doesn't provide the power and reliability that you can achieve while tunning up manny aspects from your administered server, from Kernel parameters to specific configurations to your Web Server, bringing more control, security and reliability to your products, not having a blackbox which you don't have control over it.

As an advice: analyze carefully, pros and cons. Don't get excited about the buzz. Both approaches have great things to offer, but only through a deep analysis, you can determine which one is better to your business, or maybe both.

Questions, doubts or improvements, feel free to leave your comments. Thanks for reading.

Segmentation Fault in Python

A code that fails and no further information is provided, is a situation of despair: no Traceback, no nothing. This is what a Segmentation Fault provides.

I was almost starting to questioning my career choice, while I was in a hurry for delivering a code ASAP and this catastrophe happens.
What a nightmare.

It happened while using MySQL connector for Python.
The reason: a Bug on version 8.0.8 where the connector crashes, if you have imported Python random library or if any component of your project has it on its implementation. In my case, I was using Python Client for Google PubSub, which has Python random library on its implementation.

To identify this all this scenario, it was only possible with faulthandler library:

Thanks to Cookie Monster's Answer on StackOverflow. Really helped.

I hope you might not face this situation too often.

Stay at home. Regards.

Mastodon