Setup Google Cloud Functions Using Python

Apr 20, 2019

Create a Google Cloud Project

Enable Billing

Enable Cloud Functions API

Install & Setup Google Cloud SDK

NOTE: There is an alternative method using the console if you do not wish to use Google Cloud SDK/gcloud.

Code

Create a project directory.

Create main.py with the following code.

def test_message(request):    request_json = request.get_json()    if request.args and 'message' in request.args:        return request.args.get('message')    elif request_json and 'message' in request_json:        return request_json['message']    else:        return f'Hello World!'

If you need to import python libraries, create requirements.txt file.

python-dateutil==2.7.5
from dateutil.parser import parse as datetime_parser

Deploy & Test

Deploy the function.

gcloud functions deploy test_message --runtime python37 --trigger-http --project [PROJECT_NAME]

You can test the could functions.

  • Using URL: https://us-central1-[PROJECT_NAME].cloudfunctions.net/test_message
  • Access Cloud Functions Console, find the function name and click on the 3 vertical dots (Kebab icon) and select Test function.

Each deployment takes between 20s - 2 minutes, so it is not great for testing.

NOTE: Refer to testing cloud functions on local machine.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.