Skip to content

Cloud Functions

Cloud Functions is GCP’s Functions-as-a-Service (FaaS) offering. Write small, focused functions that respond to events — HTTP requests, Pub/Sub messages, Cloud Storage changes, and more.

FeatureCloud FunctionsCloud Run
UnitFunction (single handler)Container (any app)
RuntimeManaged language runtimesAny container
Max timeout9 min (HTTP), 10 min (event)60 min
Cold startsHigherLower with min-instances
Best forLightweight event handlersAPIs, web apps
  • Node.js 18, 20, 22
  • Python 3.9, 3.10, 3.11, 3.12
  • Go 1.21, 1.22
  • Java 17, 21
  • Ruby 3.2
  • .NET 6, 8
  • PHP 8.2
// Node.js HTTP function
const functions = require('@google-cloud/functions-framework');
functions.http('helloWorld', (req, res) => {
const name = req.query.name || 'World';
res.send(`Hello, ${name}!`);
});
# Python HTTP function
import functions_framework
@functions_framework.http
def hello_world(request):
name = request.args.get('name', 'World')
return f'Hello, {name}!'
Terminal window
# Deploy an HTTP function (Node.js)
gcloud functions deploy hello-world \
--runtime=nodejs20 \
--trigger-http \
--entry-point=helloWorld \
--allow-unauthenticated \
--region=europe-west2
# Deploy a Python function
gcloud functions deploy hello-world \
--runtime=python312 \
--trigger-http \
--entry-point=hello_world \
--region=europe-west2
// Triggered when a message arrives on a Pub/Sub topic
const functions = require('@google-cloud/functions-framework');
functions.cloudEvent('processPubSub', (event) => {
const message = Buffer.from(event.data.message.data, 'base64').toString();
console.log('Received:', message);
});
Terminal window
gcloud functions deploy process-pubsub \
--runtime=nodejs20 \
--trigger-topic=my-topic \
--entry-point=processPubSub \
--region=europe-west2
functions.cloudEvent('onFileUpload', (event) => {
const file = event.data;
console.log(`File uploaded: ${file.name} (${file.size} bytes)`);
});
Terminal window
gcloud functions deploy on-file-upload \
--runtime=nodejs20 \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=my-bucket-name" \
--entry-point=onFileUpload \
--region=europe-west2
Terminal window
# Set environment variables at deploy time
gcloud functions deploy my-function \
--set-env-vars="DB_HOST=localhost,LOG_LEVEL=info"
# Reference a Secret Manager secret
gcloud functions deploy my-function \
--set-secrets="API_KEY=my-api-key:latest"

In code:

const apiKey = process.env.API_KEY;
Terminal window
# Install the Functions Framework
npm install @google-cloud/functions-framework
# Run locally
npx functions-framework --target=helloWorld
# Visit http://localhost:8080

Run a Cloud Function on a schedule via Pub/Sub:

Terminal window
# Create a Pub/Sub topic
gcloud pubsub topics create daily-trigger
# Create a Cloud Scheduler job
gcloud scheduler jobs create pubsub daily-cleanup \
--schedule="0 2 * * *" \
--topic=daily-trigger \
--message-body="run"
Terminal window
# List functions
gcloud functions list
# Describe a function (URL, trigger, config)
gcloud functions describe my-function --region=europe-west2
# View logs
gcloud functions logs read my-function --region=europe-west2
# Call an HTTP function
gcloud functions call my-function \
--data='{"name":"test"}' \
--region=europe-west2
# Delete a function
gcloud functions delete my-function --region=europe-west2