Cloud Functions
Cloud Functions
Section titled “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.
Cloud Functions vs Cloud Run
Section titled “Cloud Functions vs Cloud Run”| Feature | Cloud Functions | Cloud Run |
|---|---|---|
| Unit | Function (single handler) | Container (any app) |
| Runtime | Managed language runtimes | Any container |
| Max timeout | 9 min (HTTP), 10 min (event) | 60 min |
| Cold starts | Higher | Lower with min-instances |
| Best for | Lightweight event handlers | APIs, web apps |
Supported Runtimes
Section titled “Supported Runtimes”- 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
HTTP Functions
Section titled “HTTP Functions”// Node.js HTTP functionconst functions = require('@google-cloud/functions-framework');
functions.http('helloWorld', (req, res) => { const name = req.query.name || 'World'; res.send(`Hello, ${name}!`);});# Python HTTP functionimport functions_framework
@functions_framework.httpdef hello_world(request): name = request.args.get('name', 'World') return f'Hello, {name}!'Deploying
Section titled “Deploying”# 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 functiongcloud functions deploy hello-world \ --runtime=python312 \ --trigger-http \ --entry-point=hello_world \ --region=europe-west2Event-Driven Functions
Section titled “Event-Driven Functions”Pub/Sub Trigger
Section titled “Pub/Sub Trigger”// Triggered when a message arrives on a Pub/Sub topicconst functions = require('@google-cloud/functions-framework');
functions.cloudEvent('processPubSub', (event) => { const message = Buffer.from(event.data.message.data, 'base64').toString(); console.log('Received:', message);});gcloud functions deploy process-pubsub \ --runtime=nodejs20 \ --trigger-topic=my-topic \ --entry-point=processPubSub \ --region=europe-west2Cloud Storage Trigger
Section titled “Cloud Storage Trigger”functions.cloudEvent('onFileUpload', (event) => { const file = event.data; console.log(`File uploaded: ${file.name} (${file.size} bytes)`);});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-west2Environment Variables and Secrets
Section titled “Environment Variables and Secrets”# Set environment variables at deploy timegcloud functions deploy my-function \ --set-env-vars="DB_HOST=localhost,LOG_LEVEL=info"
# Reference a Secret Manager secretgcloud functions deploy my-function \ --set-secrets="API_KEY=my-api-key:latest"In code:
const apiKey = process.env.API_KEY;Local Development
Section titled “Local Development”# Install the Functions Frameworknpm install @google-cloud/functions-framework
# Run locallynpx functions-framework --target=helloWorld# Visit http://localhost:8080Scheduling with Cloud Scheduler
Section titled “Scheduling with Cloud Scheduler”Run a Cloud Function on a schedule via Pub/Sub:
# Create a Pub/Sub topicgcloud pubsub topics create daily-trigger
# Create a Cloud Scheduler jobgcloud scheduler jobs create pubsub daily-cleanup \ --schedule="0 2 * * *" \ --topic=daily-trigger \ --message-body="run"Useful Commands
Section titled “Useful Commands”# List functionsgcloud functions list
# Describe a function (URL, trigger, config)gcloud functions describe my-function --region=europe-west2
# View logsgcloud functions logs read my-function --region=europe-west2
# Call an HTTP functiongcloud functions call my-function \ --data='{"name":"test"}' \ --region=europe-west2
# Delete a functiongcloud functions delete my-function --region=europe-west2