You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Cloud Functions Overview
Cloud Functions Overview
Google Cloud Functions is a lightweight, event-driven, serverless compute platform that lets you run single-purpose functions in response to cloud events. You write small pieces of code — functions — and Google manages the servers, scaling, and infrastructure for you.
What Are Cloud Functions?
Cloud Functions is Google Cloud's Function-as-a-Service (FaaS) offering. Each function is a small, self-contained piece of code that executes in response to a trigger. You deploy source code, and Google Cloud handles provisioning, scaling, patching, and monitoring the underlying infrastructure.
Key Characteristics
| Characteristic | Description |
|---|---|
| Event-driven | Functions run in response to events — HTTP requests, Pub/Sub messages, Cloud Storage changes, Firestore updates, and more |
| Serverless | No servers to provision, manage, or patch |
| Auto-scaling | Scales from zero to thousands of instances automatically |
| Pay-per-use | Billed only for the compute time your function consumes (100ms granularity) |
| Short-lived | Designed for quick execution — default timeout is 60 seconds (configurable up to 60 minutes for Gen 2) |
Cloud Functions Generations
Google Cloud offers two generations of Cloud Functions:
1st Generation (Gen 1)
The original Cloud Functions product, built on a proprietary infrastructure. Gen 1 functions support HTTP triggers, Pub/Sub triggers, and Cloud Storage triggers. They have a maximum timeout of 540 seconds (9 minutes) and a maximum memory allocation of 8 GiB.
2nd Generation (Gen 2)
Built on Cloud Run, Gen 2 functions offer significant improvements over Gen 1. They support longer timeouts (up to 60 minutes for event-driven functions), larger instance sizes (up to 16 GiB memory, 4 vCPUs), concurrency (up to 1,000 concurrent requests per instance), and traffic splitting. Gen 2 is the recommended generation for all new functions.
Supported Runtimes
Cloud Functions supports multiple programming languages:
| Runtime | Versions |
|---|---|
| Node.js | 18, 20, 22 |
| Python | 3.10, 3.11, 3.12 |
| Go | 1.21, 1.22 |
| Java | 11, 17, 21 |
| Ruby | 3.2, 3.3 |
| .NET | 6, 8 |
| PHP | 8.2, 8.3 |
Trigger Types
Cloud Functions can be triggered by various event sources:
HTTP Triggers
HTTP functions are invoked via standard HTTP requests (GET, POST, PUT, DELETE, etc.). Each HTTP function gets a unique URL endpoint. These are ideal for building REST APIs, webhooks, and lightweight web services.
# Deploy an HTTP function
gcloud functions deploy helloHttp \
--gen2 \
--runtime nodejs20 \
--trigger-http \
--allow-unauthenticated \
--region europe-west2
Event Triggers
Event-driven functions respond to events from Google Cloud services. Common event sources include:
- Cloud Storage — object creation, deletion, archiving, metadata updates
- Pub/Sub — messages published to a topic
- Firestore — document creation, updates, deletions
- Firebase Authentication — user creation, deletion
- Cloud Audit Logs — any audited API call across Google Cloud
Eventarc Triggers (Gen 2)
Gen 2 functions use Eventarc for event routing. Eventarc supports over 130 Google Cloud event sources and can receive custom events via Pub/Sub. This provides a unified eventing layer across all Google Cloud services.
Pricing Model
Cloud Functions pricing is based on three dimensions:
| Dimension | Description |
|---|---|
| Invocations | Number of function invocations (first 2 million per month free) |
| Compute time | GB-seconds and GHz-seconds of compute (first 400,000 GB-seconds free) |
| Networking | Outbound data transfer (ingress is free) |
The free tier is generous for development and low-traffic applications. Beyond the free tier, you pay only for what you use with no minimum fees.
Use Cases
Cloud Functions excels in several scenarios:
| Use Case | Example |
|---|---|
| Data processing | Resize images uploaded to Cloud Storage |
| Webhooks | Receive and process GitHub or Stripe webhooks |
| IoT | Process telemetry data from IoT Core via Pub/Sub |
| ETL | Transform and load data into BigQuery |
| Chatbots | Handle Dialogflow webhook fulfilment |
| Scheduled tasks | Run periodic jobs via Cloud Scheduler |
Summary
Cloud Functions is Google Cloud's FaaS offering for running event-driven code without managing servers. Gen 2, built on Cloud Run, is the recommended generation, offering longer timeouts, higher concurrency, traffic splitting, and support for Eventarc. Functions scale automatically from zero, and you pay only for the compute time consumed. Cloud Functions is ideal for lightweight, event-driven workloads such as data processing, webhooks, IoT pipelines, and scheduled tasks.