Cloud CDN

Deploying and managing Google Cloud CDN for content delivery

Google Cloud CDN (Content Delivery Network) uses Google's globally distributed edge points of presence to cache HTTP(S) content close to your users. By caching content at the edge, Cloud CDN accelerates content delivery, reduces serving costs, and helps alleviate the load on your backend servers.

Key Features

  • Global Edge Network: Leverage Google's global edge network spanning 100+ locations

  • Origin Support: Works with Cloud Storage buckets, Compute Engine instances, GKE services, or external origins

  • HTTP/2 and QUIC: Support for modern web protocols

  • SSL: Automatic SSL certificate management

  • Cache Control: Fine-grained cache control through standard HTTP headers

  • Cache Invalidation: API for fast content invalidation

  • Signed URLs/Cookies: Protected access to cached content

  • Metrics and Logging: Real-time monitoring and detailed logging

  • Media Optimization: Optimize delivery for video streaming and large files

  • Security Features: Integration with Cloud Armor for WAF and DDoS protection

Deploying Cloud CDN with Terraform

Basic Setup with Cloud Storage Backend

# Create a storage bucket to serve as the CDN origin
resource "google_storage_bucket" "cdn_bucket" {
  name          = "my-cdn-content-bucket"
  location      = "US"
  storage_class = "STANDARD"
  
  # Enable website serving
  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
  
  # Enforce uniform bucket-level access
  uniform_bucket_level_access = true
  
  # Make objects publicly readable
  force_destroy = true
}

# Upload sample content
resource "google_storage_bucket_object" "sample_content" {
  name        = "index.html"
  bucket      = google_storage_bucket.cdn_bucket.name
  content     = "<html><body>Hello from Cloud CDN!</body></html>"
  content_type = "text/html"
}

# Grant public read access to objects
resource "google_storage_bucket_iam_member" "public_read" {
  bucket = google_storage_bucket.cdn_bucket.name
  role   = "roles/storage.objectViewer"
  member = "allUsers"
}

# Create a compute backend service for the CDN
resource "google_compute_backend_bucket" "cdn_backend" {
  name        = "cdn-backend-bucket"
  description = "Backend bucket for serving content via CDN"
  bucket_name = google_storage_bucket.cdn_bucket.name
  
  # Enable CDN
  enable_cdn = true
  
  # Configure caching behavior
  cdn_policy {
    cache_mode        = "CACHE_ALL_STATIC"
    client_ttl        = 3600  # 1 hour
    default_ttl       = 3600  # 1 hour
    max_ttl           = 86400 # 24 hours
    negative_caching  = true
    
    # Cache static content types automatically
    cache_key_policy {
      include_host         = true
      include_protocol     = true
      include_query_string = false
    }
  }
}

# Create a URL map to route requests to the backend
resource "google_compute_url_map" "cdn_url_map" {
  name            = "cdn-url-map"
  description     = "URL map for CDN"
  default_service = google_compute_backend_bucket.cdn_backend.id
}

# Create an HTTP target proxy
resource "google_compute_target_http_proxy" "cdn_http_proxy" {
  name    = "cdn-http-proxy"
  url_map = google_compute_url_map.cdn_url_map.id
}

# Create a global forwarding rule (the entrypoint for the CDN)
resource "google_compute_global_forwarding_rule" "cdn_forwarding_rule" {
  name       = "cdn-forwarding-rule"
  target     = google_compute_target_http_proxy.cdn_http_proxy.id
  port_range = "80"
  ip_protocol = "TCP"
}

# Output the CDN URL
output "cdn_url" {
  value = "http://${google_compute_global_forwarding_rule.cdn_forwarding_rule.ip_address}"
}

HTTPS Configuration with Custom Domain

Advanced Configuration with Load Balancer and Cloud Armor

Managing Cloud CDN with gcloud CLI

Setting up Cloud CDN with a Load Balancer

Setting up Cloud CDN with Compute Engine Backends

Managing Cache Invalidation

Real-World Example: Multi-Region Static Website with Cloud CDN

This example demonstrates a complete setup for a multi-region, high-performance static website using Cloud Storage, Cloud CDN, and Cloud Load Balancing:

Architecture Overview

  1. Cloud Storage buckets in multiple regions for origin content

  2. Cloud Load Balancer for global routing

  3. Cloud CDN for edge caching

  4. Cloud Armor for security

  5. Custom domain with SSL

Terraform Implementation

Shell Script for Content Deployment

Best Practices

  1. Content Optimization

    • Use appropriate caching headers (Cache-Control, Expires)

    • Enable gzip/Brotli compression for text-based content

    • Optimize images and use modern formats (WebP)

    • Implement HTTP/2 for better performance

  2. Cache Control Strategy

    • Set longer TTLs for static content (images, JS, CSS)

    • Use shorter TTLs for frequently changing content

    • Implement versioned URLs for cache-busting

    • Consider negative caching for 404/5xx errors

  3. Security

    • Use HTTPS with modern TLS versions

    • Implement Cloud Armor security policies

    • Use signed URLs/cookies for private content

    • Apply proper CORS settings for web applications

  4. Performance Monitoring

    • Monitor cache hit ratios

    • Track origin load and bandwidth savings

    • Set up alerts for latency or error rate increases

    • Use Cloud Monitoring dashboards for CDN metrics

  5. Operations

    • Plan cache invalidation strategies

    • Implement CI/CD for content updates

    • Configure multi-region origins for redundancy

    • Use canary deployments for high-traffic sites

Common Issues and Troubleshooting

Cache Misses

  • Verify the cache policy is correctly configured

  • Check the Cache-Control headers from the origin

  • Ensure query strings are handled correctly in cache key

  • Monitor for cache sharding (many unique URLs)

  • Check if client caching headers differ from server headers

SSL/TLS Issues

  • Verify SSL certificate is valid and not expired

  • Check SSL certificate covers all domains being served

  • Ensure OCSP stapling is enabled

  • Verify TLS version compatibility with clients

  • Check for mixed content warnings

Performance Problems

  • Monitor latency by region and edge location

  • Check for origin server performance issues

  • Verify content compression is working

  • Analyze cache hit ratios

  • Optimize critical rendering path

Content Updates Not Appearing

  • Check cache invalidation status

  • Verify cache TTL settings

  • Use versioned URLs for static assets

  • Check for stale-while-revalidate issues

  • Monitor origin server reachability

Further Reading

Last updated