Cloud Functions (Road to Google Associate Cloud Engineer 2020 certification)

Emanuele Pecorari
3 min readMar 21, 2021

--

Another totally managed service offered by Google is Cloud Function, similar to the AWS Lambda function. There is no need to manage the servers (serverless) but only some configuration parameters (like for example the memory available for the function) and focus on the code.

Of course, to complete a real production system, some aspects like deployment, security need to be adjusted for functions as well.

The service manage scaling automatically, it creates one instance of function every request that is received.

There are no many concepts that need to be understood for the certification:

  • the event-driven approach and the fact that it can be used to decouple the services in your architecture
  • how the functions are triggered
  • the integration with other services
  • the limits of the services

A service for event-driven architectures: function triggers

The code of the cloud functions is executed in response to events:

  • http and https requests: POST, GET, PUT, DELETE, OPTIONS
  • Cloud storage events: finalize/create, delete, archive, metadata update for a file
  • Pub/Sub: publish a message
  • Firebase events (from realtime database, storage, authentication and analytics)
  • Firestore events
  • Stackdriver logging

When you deploy your function, you will have to define:

  • the runtime you want to use choosing between different versions of Node, Python, Go, Java, .NET and Ruby
  • the resource from which the function will receive the trigger
  • the type of trigger that the function will get

Some triggers you can configure:

  • Trigger http: — trigger-http
  • Trigger from Pub/Sub: — trigger-topic <topic_name>
  • Others: — trigger-event <event_type> — trigger-resource <resource>

Cloud function is, for example, the right solution in situation where you have to process images that are uploaded to storage, for example to resize and build thumbnails for them.

Also, it is the right service when you need to keep your architecture decoupled, combining it with the Pub/Sub service.

This is an example of architecture where a cloud function is used to resize a picture and create a thumbnail when the file is uploaded to storage:

The quotas and limits

One of the most important limit to consider is the memory that can be allocated for each function instance: 2 GB.

Then, the maximum timeout is 9 minutes: cloud functions can not be used for long processing task.

Every project has a maximum of 1000 functions that can be deployed.

Below the tables from Google Cloud website summarizing the limits:

Pricing

You can find questions asking how you can estimate the price of cloud function usage. To have an estimation you need to specify the following information on the GCP pricing calculator:

  • type
  • bandwidth
  • execution time
  • invocations

Dependencies

There are two ways to specify dependencies for Cloud Functions written in Python: using the pip package manager’s requirements.txt file or packaging local dependencies alongside your function.

For node, you can specify them in the package.json file and the platform will install them using npm.

A complete How-to guide is available here on GCP site

Focus on CLI commands

Deploy a cloud function

gcloud functions deploy NAME --entry-point ENTRY-POINT --runtime RUNTIME TRIGGER [FLAGS...]

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Emanuele Pecorari
Emanuele Pecorari

Written by Emanuele Pecorari

Cloud Architect and Tech Product Owner. Soccer player and coach in the free time.

No responses yet

Write a response