Skip to main content
Serverless Architecture
CHAPTER 04 Intermediate

Google Cloud Functions Basics

Updated: May 15, 2026
20 min read

# CHAPTER 4

Google Cloud Functions Basics

1. Introduction

While AWS Lambda pioneered the serverless landscape, Google Cloud Functions (GCF) is renowned for its elegant developer experience and seamless integration with the broader Google ecosystem (like Firebase and BigQuery). If you are building modern web applications, Google Cloud Functions often require less configuration overhead than AWS. In this chapter, we will explore the Google Cloud environment, differentiate between HTTP and Background triggers, and deploy a serverless function on GCP.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the architecture of Google Cloud Functions.
  • Differentiate between 1st Gen and 2nd Gen Cloud Functions.
  • Compare HTTP Triggers vs. Background (Event) Triggers.
  • Deploy a Google Cloud Function via the GCP Console.
  • Test a function using a public URL.

3. Beginner-Friendly Explanation

Imagine a smart home system.
  • HTTP Trigger (The Doorbell): You press the physical doorbell button, and a chime immediately rings inside the house. This is a direct, synchronous request. A user visits a URL, and the function runs instantly.
  • Background Event Trigger (The Motion Sensor): You walk past a sensor in the hallway. The sensor notices you, writes a note ("Motion Detected"), and sends it to the smart home hub. A few milliseconds later, the hub reads the note and turns on the lights. This is asynchronous. A file is uploaded to cloud storage, the storage system publishes an "event," and the function reacts to it in the background.

4. 1st Gen vs. 2nd Gen Functions

Google currently offers two versions of their FaaS platform:
  • 1st Gen: The original. Simple, fast, but has strict limitations on execution time (max 9 minutes) and concurrency (1 request per container).
  • 2nd Gen: Built on modern Kubernetes architecture (Cloud Run). Highly recommended. It supports much larger instances, longer execution times (up to 60 minutes for HTTP), and most importantly, Concurrency—a single warm container can handle up to 1000 simultaneous requests, massively reducing Cold Starts!

5. HTTP vs. Background Triggers

  1. 1. HTTP Functions: When you deploy this, Google generates a unique, public URL (e.g., https://us-central1-myproject.cloudfunctions.net/helloWorld). Anyone who visits that URL triggers the function. Perfect for building REST APIs.
  1. 2. Event-Driven (Background) Functions: These do not have public URLs. They are triggered internally by Google Cloud services, such as a message arriving in a Pub/Sub queue, or a new document being added to a Firestore database.

6. Mini Project: Deploy a Google Cloud Function

Let's build an HTTP API endpoint on Google Cloud.

Step-by-Step Tutorial: *(Assumption: You have a Google Cloud Platform account).*

  1. 1. Go to the Google Cloud Console (console.cloud.google.com).
  1. 2. Search for Cloud Functions and click Create function.
  1. 3. Environment: Select 2nd gen.
  1. 4. Function name: my-gcp-api.
  1. 5. Region: us-central1.
  1. 6. Trigger type: Select HTTPS.
  1. 7. Authentication: Select Allow unauthenticated invocations (This makes the URL public so we can test it in our browser. *Do not do this for sensitive databases!*). Click Next.
  1. 8. Runtime: Select Node.js 20.
  1. 9. You will see the default source code, which looks slightly different from AWS Lambda:

javascript
12345
const functions = require('@google-cloud/functions-framework');

functions.http('helloHttp', (req, res) => {
  res.send(`Hello from Google Cloud!`);
});

*(Notice that GCF uses the standard Express.js req, res format, which is very familiar to Node.js developers!)*

  1. 10. Click Deploy. (Wait 1-2 minutes for Google to build the container).
  1. 11. Once deployed, click on the function, navigate to the Trigger tab, and copy the Trigger URL.
  1. 12. Paste the URL into a new browser tab. You will see Hello from Google Cloud!

7. Real-World Scenarios

A mobile app developer uses Firebase (which is powered by Google Cloud) to manage user accounts. They need to send a "Welcome Email" every time a new user registers. Instead of building a complex backend server, they write a Google Cloud Function with an Event Trigger listening to providers/firebase.auth/eventTypes/user.create. The exact millisecond a user signs up on the mobile app, Google automatically triggers the function in the background to send the email.

8. Best Practices

  • Use the Functions Framework: Google open-sourced the @google-cloud/functions-framework. You can install it on your local laptop using npm. This allows you to run and test your Cloud Functions completely offline on localhost:8080 before you ever upload them to the cloud, dramatically speeding up development time.

9. Cost Optimization Tips

  • Understand Concurrency (2nd Gen): Because 2nd Gen functions allow multiple requests to share the same container simultaneously, you require far fewer container instances to handle a traffic spike compared to 1st Gen. This significantly reduces your overall compute bill during high-traffic events.

10. CLI Examples

To deploy a Google Cloud Function from your local terminal using the gcloud CLI:
bash
12345
gcloud functions deploy my-local-function \
  --gen2 \
  --runtime nodejs20 \
  --trigger-http \
  --allow-unauthenticated

11. Exercises

  1. 1. Explain the operational advantage of Concurrency in 2nd Gen Google Cloud Functions compared to 1st Gen functions.
  1. 2. Why does an Event-Driven (Background) Function not possess a public HTTPS URL?

12. FAQs

Q: Is Google Cloud Functions cheaper than AWS Lambda? A: They are very competitively priced, and both offer generous free tiers (Google offers 2 million free invocations a month). Your choice should be based on which ecosystem (AWS vs. GCP) your other data and services live in.

13. Interview Questions

  • Q: Contrast the architectural differences between a 1st Gen and 2nd Gen Google Cloud Function. Highlight the implications for Cold Starts and execution timeouts.
  • Q: A developer has written a Node.js Google Cloud Function using an HTTP Trigger. Describe the object structure passed into the function (i.e., the Express.js req and res objects) and how it differs from the AWS Lambda event object.

14. Summary

In Chapter 4, we expanded our serverless toolkit by exploring Google Cloud Functions. We differentiated between the legacy 1st Gen architecture and the modern, Kubernetes-backed 2nd Gen architecture, highlighting the massive performance benefits of Concurrency. We distinguished between public HTTP triggers and internal Background events, and successfully deployed a live, publicly accessible Node.js API endpoint directly from the GCP Console.

15. Next Chapter Recommendation

We have explored AWS and Google. To complete the "Big Three" cloud providers, we must look at Microsoft's highly integrated enterprise serverless offering. Proceed to Chapter 5: Azure Functions Basics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·