Skip to main content
Hello and welcome back. In this lesson we’ll examine Cloud Functions and the fundamentals of serverless computing. We previously covered Cloud Workflows for orchestrating multi-step processes. When you only need a single piece of logic to run in response to an event — without managing servers or infrastructure — Cloud Functions is the right tool. Cloud Functions is a managed, event-driven serverless compute service. Google Cloud handles the underlying servers, scaling, and patching so you can focus on code and the events that trigger it. It is analogous to AWS Lambda.

Core serverless concepts for Cloud Functions

  • No server management
    Google Cloud manages provisioning, patching, and the runtime environment so you don’t have to.
  • Automatic scaling
    Functions scale out automatically by creating instances to handle increased traffic.
  • Pay-per-use billing
    You are charged only while your function is executing; idle time is not billed.
  • Event-driven execution
    Functions execute in response to events: HTTP requests, Pub/Sub messages, storage events, Firestore changes, etc.
  • Statelessness
    Each execution is independent and should not rely on local filesystem state. Use external services (Firestore, Cloud Storage, BigQuery) for persistence.
Cloud Functions are best for short-lived, stateless compute tasks. Persist any durable state in external stores such as Firestore, Cloud Storage, or BigQuery.
Example scenario: A user uploads a PDF to Cloud Storage. That upload triggers a Cloud Function which extracts text, creates a summary, and writes the summary into BigQuery. No servers run between uploads — the function executes only when triggered.

Supported languages and runtimes

Cloud Functions supports multiple runtimes so teams can use familiar languages. Supported languages typically include:
  • Python
  • Node.js
  • Java
  • Go
  • .NET
  • Ruby
(Note: supported runtime versions change over time—check the Cloud Functions runtime documentation for the latest versions.)
A slide titled "Runtime Environments" showing logos and supported versions for several languages. It lists Python (3.7–3.10), Node.js (12, 14, 16, 18), Java (11, 17), Go (1.16, 1.18, 1.19), .NET (3.1, 6) and Ruby (2.6, 2.7, 3.0).
If you want a quick reference, here is a compact table of languages and common use cases:
LanguageTypical use case
PythonData processing, ML glue code, ETL tasks
Node.jsHTTP APIs, webhooks, lightweight microservices
JavaEnterprise code, existing JVM libraries
GoHigh-performance, low-latency services
.NETMicrosoft-stack integrations
RubyWebhooks, scripting for Rails ecosystems

Triggers — how functions are invoked

Functions run in response to one of several trigger types:
Trigger typeDescriptionExample uses
HTTP triggerExpose a function as an HTTP(S) endpointREST APIs, webhooks
Pub/Sub triggerReact to asynchronous messagesEvent-driven pipelines, decoupled services
Cloud Storage eventsTrigger on object create/delete/metadata changesFile processing, thumbnails, virus scanning
Firestore triggersReact to document create/update/deleteDatabase-driven workflows, audit logs

Minimal examples

HTTP-triggered Cloud Function examples — these show the handler signature and basic response. Node.js (HTTP):
// index.js
exports.helloHttp = (req, res) => {
  const name = req.query.name || req.body?.name || 'World';
  res.status(200).send(`Hello ${name}!`);
};
Python (HTTP):
# main.py
def hello_http(request):
    name = request.args.get('name') or request.get_json(silent=True).get('name', 'World')
    return f'Hello {name}!', 200
Deploy example (gcloud CLI, HTTP-triggered):
gcloud functions deploy helloHttp \
  --runtime nodejs18 \
  --trigger-http \
  --allow-unauthenticated \
  --entry-point helloHttp
Use Pub/Sub or Storage triggers similarly by specifying --trigger-topic or --trigger-bucket during deployment.

Quick exam-style question

Which billing model does Cloud Functions use?
  1. Pay for servers running 24/7.
  2. Pay per use only when your code executes.
  3. Monthly subscription, regardless of usage.
Correct answer: option 2 — pay per use only when your Cloud Function executes. This billing model reduces cost by eliminating charges for idle compute.

Summary

Cloud Functions offers lightweight, event-driven compute that scales automatically and removes the burden of server management. Use Cloud Functions for small, focused units of logic that respond to events; rely on external services for persistent state. Topics to explore next:
  • Performance characteristics and cold-start behavior
  • Data-processing design patterns with Cloud Functions
  • Rate limiting, batching, and retry strategies
  • When to choose Cloud Functions vs. Cloud Run or Workflows
That is it for this lesson. Speak with you in the next lesson.

Watch Video