loading image
Back to glossary

Google Cloud Functions

Google Cloud's serverless service for running event-driven code without managing server infrastructure.

Updated on January 24, 2026

Google Cloud Functions is Google Cloud Platform's (GCP) serverless solution that enables developers to run code in response to events without worrying about underlying infrastructure. This fully managed service integrates natively with the Google Cloud ecosystem and supports Node.js, Python, Go, Java, .NET, Ruby, and PHP. Google Cloud Functions offers automatic scaling, pay-per-use billing, and seamless integration with GCP services.

Fundamentals of Google Cloud Functions

  • Event-driven architecture based on HTTP triggers, Pub/Sub, Cloud Storage, Firestore, and other GCP sources
  • Isolated execution environment with automatic provisioning and scaling from 0 to thousands of instances
  • Two generations available: 1st gen (Cloud Functions) and 2nd gen (Cloud Run-based) with extended capabilities
  • Billing model charged to the nearest 100ms, billing only for resources consumed during execution

Benefits of Google Cloud Functions

  • Zero infrastructure management: complete focus on business code without server provisioning or configuration
  • Native integration with Google Cloud ecosystem: BigQuery, Cloud Storage, Firestore, Pub/Sub, Cloud Scheduler
  • Rapid development with deployment in seconds via CLI, console, or integrated CI/CD
  • Instant automatic scaling to handle traffic spikes without prior configuration
  • Optimal cost-efficiency through millisecond billing and no idle costs

Practical Implementation Example

index.ts
import { CloudEvent } from '@google-cloud/functions-framework';
import { Storage } from '@google-cloud/storage';
import * as sharp from 'sharp';

const storage = new Storage();

/**
 * Cloud Function triggered on image upload
 * Automatically generates optimized thumbnails
 */
export async function generateThumbnail(cloudEvent: CloudEvent) {
  const file = cloudEvent.data;
  const bucketName = file.bucket;
  const filePath = file.name;
  
  // Ignore already processed files
  if (filePath.includes('thumb_')) {
    console.log('Thumbnail already exists, skipping.');
    return;
  }

  const bucket = storage.bucket(bucketName);
  const originalFile = bucket.file(filePath);
  const thumbnailPath = `thumbnails/thumb_${filePath}`;
  
  try {
    // Download original image
    const [imageBuffer] = await originalFile.download();
    
    // Create thumbnail with Sharp
    const thumbnail = await sharp(imageBuffer)
      .resize(200, 200, {
        fit: 'cover',
        position: 'center'
      })
      .webp({ quality: 80 })
      .toBuffer();
    
    // Upload thumbnail
    await bucket.file(thumbnailPath).save(thumbnail, {
      metadata: {
        contentType: 'image/webp',
        metadata: {
          originalFile: filePath,
          generatedAt: new Date().toISOString()
        }
      }
    });
    
    console.log(`Thumbnail created: ${thumbnailPath}`);
  } catch (error) {
    console.error('Error generating thumbnail:', error);
    throw error;
  }
}
function.yaml
# Deployment configuration (Cloud Functions 2nd gen)
runtime: nodejs20
entry_point: generateThumbnail

# Cloud Storage trigger
event_trigger:
  event_type: google.cloud.storage.object.v1.finalized
  resource: projects/_/buckets/my-upload-bucket
  
# Resource configuration
resources:
  memory: 512Mi
  cpu: 1
  timeout: 60s
  max_instances: 100
  min_instances: 0

# Environment variables
environment_variables:
  NODE_ENV: production
  LOG_LEVEL: info

Implementation Steps

  1. GCP project setup: create a project, enable Cloud Functions API, and configure billing
  2. Install tools: Google Cloud CLI (gcloud) and configure local authentication
  3. Function development: create code with necessary dependencies and define entry point
  4. Trigger configuration: choose between HTTP, Pub/Sub, Cloud Storage, Firestore, or other GCP events
  5. Deploy via CLI: use gcloud functions deploy with runtime, memory, and timeout parameters
  6. Security configuration: define IAM policies, secrets via Secret Manager, and VPC Connector if needed
  7. Monitoring and observability: enable Cloud Logging, Cloud Monitoring, and configure performance alerts

Pro Tip: Performance Optimization

For Cloud Functions 2nd gen, use minimum instances (min_instances) to reduce cold starts on critical functions. Cache heavy connections (databases, APIs) at the global level rather than within the function for reuse between invocations. Prefer Cloud Run for workloads requiring more than 60 minutes of execution or significant resources (>16GB RAM).

  • Cloud Run: alternative for containers with more control and flexibility
  • Functions Framework: open-source library for local development and testing
  • Cloud Build: native CI/CD for automated deployment from Git
  • Secret Manager: secure management of secrets and API keys
  • Cloud Scheduler: scheduled triggering of functions via Pub/Sub or HTTP
  • Eventarc: unified event routing for 100+ Google Cloud event sources
  • Cloud Trace and Cloud Profiler: performance analysis and code optimization

Google Cloud Functions represents a mature serverless solution deeply integrated with the Google Cloud ecosystem, particularly suited for event-driven architectures and microservices. Its granular billing, automatic scaling, and native integration with GCP services make it a strategic choice for reducing operational complexity while maintaining performance and reliability. For organizations invested in Google Cloud, Cloud Functions offers excellent value for processing event-driven workloads, automating workflows, and building lightweight APIs.

Themoneyisalreadyonthetable.

In 1 hour, discover exactly how much you're losing and how to recover it.

Web development, automation & AI agency

contact@peaklab.fr
Newsletter

Get our tech and business tips delivered straight to your inbox.

Follow us

© PeakLab 2025