Cloud CDN
Deploying and managing Google Cloud CDN for content delivery
Key Features
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
Architecture Overview
Terraform Implementation
Shell Script for Content Deployment
Best Practices
Common Issues and Troubleshooting
Cache Misses
SSL/TLS Issues
Performance Problems
Content Updates Not Appearing
Further Reading
Last updated