Writing Google Cloud Functions with Python 3

This is my third and final article looking at new features in Google Cloud functions as Google starts to narrow the gap to Amazon’s AWS Lambda.

Writing Google Cloud Functions with Python 3

Simon Prickett 07.27.2018

This is my third and final article looking at new features in Google Cloud functions as Google starts to narrow the gap to Amazon’s AWS Lambda product. Until recently Node.js 6 was the only option for writing Google Cloud functions. That’s changed now with the addition of Node.js 8 (read my article) and Python 3 runtimes available in public beta.

Let’s take a look at how to use the Python environment by deploying a HTTP function that performs the same task as my Node.js 8 Cloud Functions demo does. We’ll use the requests library and randomuser.me API to output a JSON object representing data about a single user, then add one extra key namedgenerator

 

Items of note here include:

  • The Python runtime uses Python 3.7.0
  • Your functions have to live in a file called main.py
  • The functions are executed within the Flask framework
  • The function’s request argument will be a Flask request object
  • The function must return anything that can be made into a Flask response object using Flask’s make_response — in this case, we pass a JSON string
  • Dependencies are specified in requirements.txt which looks like this for our example (version numbers can also be specified e.g. requests=2.19.1):
requests

(Flask is provided as part of the environment, so isn’t listed here).

When deploying this function we have to tell Google to use the Python 3 runtime. To do this we’ll need to make sure we have the latest gcloud beta commands:

$ gcloud components update
$ gcloud components install beta

Deployment is simple (we have to use the beta commands and explicitly say we want the Python runtime):

$ gcloud beta functions deploy getUserDetails --runtime python37 --trigger-http --project <projectId>

Where <projectId> is the ID of your Google Cloud project. The function can be invoked simply bu visiting its URL which will look something like:

https://<region>-<projectId>.cloudfunctions.net/getUserDetails

<region> and <projectId> will depend on your Google Cloud project setup, and the gcloud command will display the full invocation URL for your function at the end of a successful deployment.

The output looks like this:

The Cloud Functions console also shows that the function is using the Python runtime:

And that’s all there is to it!

If you’d like to use or study the code from this article feel free: I’ve put it on GitHub for you. Google’s documentation for the Python Cloud Functions runtime can be read here.

This article originally appeared on Simon's Medium.

More from Mindshare: Marketing Technology

Placeholder image

5 Technical Mistakes that are Derailing your Marketing Campaign

Easily Avoidable Mistakes That Can Save Your Campaigns

Read Article
Placeholder image

Open Source Multisite - A Nightmare for both Marketers and Technologists

The Wild West of Software, Open Source (or any hosted solution) is a Poor Choice for Multisite Marketing Needs

Read Article
Placeholder image

Website Speed Optimization: 10 Methods to Increase Page Load Times

Your page load time can make the difference between a sale and a lost customer, so it’s an important thing

Read Article
Placeholder image

Publishing My First Enterprise React Project — The Takeaway

Following declarative patterns, maintaining a flat(ish) and clean state object, and working with promises helped make this a successful release.

Read Article
Simon Prickett photo

Simon Prickett

Software Engineer, Zesty.io

Software professional, specializing in IoT, Node, mobile web & apps. Atheist, electronics hobbyist. Also aviation, cars & hockey.... Learn More

Exploring content solutions? Looking to replatform?

Speak with an Expert


Call Us

Between 9-5pm PST we are available directly at +01 (844) GO ZESTY (469-3789)


Chat with Us

To speak with someone in sales and marketing, use the chat popup on this page. To speak with a developer, join https://chat.zesty.io