GCP for DevOps — Cloud Functions
Cloud Functions
Cloud Functions (Gen 2) is Google Cloud’s serverless Functions-as-a-Service (FaaS) platform. Write a function, deploy it, and GCP handles scaling, patching, and infrastructure. Gen 2 is built on Cloud Run.
Gen 1 vs Gen 2
| Feature | Gen 1 | Gen 2 |
|---|---|---|
| Runtime | Custom | Built on Cloud Run |
| Concurrency | 1 request per instance | Up to 1000 per instance |
| Timeout | 9 minutes | 60 minutes |
| Max memory | 8 GB | 32 GB |
| Traffic splitting | No | Yes |
| Eventarc triggers | No | Yes |
| Min instances | Yes | Yes |
Always use Gen 2 for new functions.
HTTP Function (Python)
# main.py
import functions_framework
@functions_framework.http
def hello(request):
"""HTTP Cloud Function."""
name = request.args.get("name", "World")
return f"Hello, {name}!"
# requirements.txt
functions-framework==3.*
# Deploy HTTP function
$ gcloud functions deploy hello \
--gen2 \
--runtime=python312 \
--region=us-central1 \
--source=. \
--entry-point=hello \
--trigger-http \
--allow-unauthenticated
# Test it
$ curl https://us-central1-my-project.cloudfunctions.net/hello?name=DevOps
Hello, DevOps!
HTTP Function (Node.js)
// index.js
const functions = require('@google-cloud/functions-framework');
functions.http('hello', (req, res) => {
const name = req.query.name || 'World';
res.send(`Hello, ${name}!`);
});
// package.json
{
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0"
}
}
Pub/Sub Triggered Function
# main.py
import base64
import functions_framework
from cloudevents.http import CloudEvent
@functions_framework.cloud_event
def process_message(cloud_event: CloudEvent):
"""Triggered by a Pub/Sub message."""
data = base64.b64decode(cloud_event.data["message"]["data"]).decode()
print(f"Received message: {data}")
# Deploy Pub/Sub function
$ gcloud functions deploy process-message \
--gen2 \
--runtime=python312 \
--region=us-central1 \
--source=. \
--entry-point=process_message \
--trigger-topic=my-topic
# Test by publishing a message
$ gcloud pubsub topics publish my-topic --message="Hello from Pub/Sub"
Cloud Storage Triggered Function
# main.py
import functions_framework
from cloudevents.http import CloudEvent
@functions_framework.cloud_event
def process_upload(cloud_event: CloudEvent):
"""Triggered when a file is uploaded to GCS."""
data = cloud_event.data
bucket = data["bucket"]
name = data["name"]
print(f"File uploaded: gs://{bucket}/{name}")
$ gcloud functions deploy process-upload \
--gen2 \
--runtime=python312 \
--region=us-central1 \
--source=. \
--entry-point=process_upload \
--trigger-event-filters="type=google.cloud.storage.object.v1.finalized" \
--trigger-event-filters="bucket=my-uploads-bucket"
Environment Variables and Secrets
# Set environment variables
$ gcloud functions deploy hello \
--gen2 \
--runtime=python312 \
--region=us-central1 \
--source=. \
--entry-point=hello \
--trigger-http \
--set-env-vars="ENV=production,LOG_LEVEL=info"
# Mount Secret Manager secrets
$ gcloud functions deploy hello \
--gen2 \
--runtime=python312 \
--region=us-central1 \
--source=. \
--entry-point=hello \
--trigger-http \
--set-secrets="DB_PASSWORD=db-password:latest"
Scaling Configuration
# Set min and max instances
$ gcloud functions deploy hello \
--gen2 \
--runtime=python312 \
--region=us-central1 \
--source=. \
--entry-point=hello \
--trigger-http \
--min-instances=1 \
--max-instances=100 \
--memory=512Mi \
--cpu=1 \
--concurrency=80 \
--timeout=60s
Manage Functions
# List functions
$ gcloud functions list --region=us-central1
# View logs
$ gcloud functions logs read hello --region=us-central1 --gen2
# Describe a function
$ gcloud functions describe hello --region=us-central1 --gen2
# Delete a function
$ gcloud functions delete hello --region=us-central1 --gen2
Common Mistakes
| Mistake | Why It’s Wrong | Fix |
|---|---|---|
| Using Gen 1 for new functions | Lower limits, no concurrency | Use --gen2 |
| No min instances for production | Cold start latency | Set --min-instances=1 |
--allow-unauthenticated on internal |
Security risk | Use IAM invoker role |
| No timeout set | Function runs too long | Set explicit --timeout |
| Large deployment package | Slow cold starts | Minimize dependencies |
Quick Reference
| Command | Description |
|---|---|
gcloud functions deploy F --gen2 --trigger-http |
Deploy HTTP function |
gcloud functions deploy F --gen2 --trigger-topic=T |
Deploy Pub/Sub function |
gcloud functions list |
List functions |
gcloud functions logs read F |
View logs |
gcloud functions delete F |
Delete function |
Next → Cloud Run Serverless