Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 45 ยท Projects

Project: Host a Static Website

Build a static website on Google Cloud: a Cloud Storage bucket, a load balancer with Cloud CDN, a DNS record and a managed HTTPS certificate, step by step.

In this project you publish a static website (HTML, CSS, JavaScript and images) on Google Cloud the way production sites do it: files in a Cloud Storage bucket, a global load balancer in front, Cloud CDN caching them near your visitors, a DNS record for your domain, and a free managed HTTPS certificate. There are no servers to patch, and the site scales to any traffic spike.

You need a Google Cloud project with billing and a domain name you control. Everything that touches your account is read-only here, so read it, then follow it in your own project. The runnable part is a stand-in cache simulator that shows why a CDN makes the site fast and cheap.

The finished architecture

Cloud DNS Name to a global IP.
Load balancer HTTPS and routing.
Cloud CDN Cached copies at the edge.
Bucket Your files, the origin.

The bucket is only touched on a cache miss.

1 / 2
flowchart LR
  V["Visitor"] --> D["Cloud DNS"]
  D --> L["HTTPS load balancer<br/>+ Cloud CDN"]
  L -->|"cache hit"| V
  L -->|"cache miss"| B["Cloud Storage bucket"]
  B --> L

A cache hit is answered at the edge; only a miss travels to the bucket.

Step 1: Create the bucket and upload the site

Pick a globally unique bucket name and a location. A single region is cheapest; a multi-region adds redundancy, though the CDN already hides most distance (see regions and zones). Use uniform bucket-level access.

Terminal
export PROJECT_ID=my-project-id
export BUCKET=my-site-assets-12345

gcloud config set project $PROJECT_ID
gcloud storage buckets create gs://$BUCKET \
  --location=us-central1 \
  --uniform-bucket-level-access

# Upload the folder that contains index.html
gcloud storage cp --recursive ./site/* gs://$BUCKET

# Tell the bucket which pages to serve for "/" and for missing files
gcloud storage buckets update gs://$BUCKET \
  --web-main-page-suffix=index.html \
  --web-error-page=404.html

A minimal site/index.html is enough to start:

HTML
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>My site</title></head>
<body><h1>Hello from Cloud Storage</h1></body>
</html>

Set a Cache-Control header on objects so the CDN and browsers know how long they may keep a file. Long for fingerprinted assets, short for HTML:

Terminal
gcloud storage objects update gs://$BUCKET/assets/** \
  --cache-control="public, max-age=31536000, immutable"
gcloud storage objects update gs://$BUCKET/index.html \
  --cache-control="public, max-age=300"

Step 2: Make the files readable, but only through the load balancer's path

A website needs its objects to be publicly readable. Grant the viewer role to allUsers on this bucket only, after thinking about it carefully (compare the warnings in common mistakes):

Terminal
gcloud storage buckets add-iam-policy-binding gs://$BUCKET \
  --member=allUsers \
  --role=roles/storage.objectViewer

Public access prevention must be off for this bucket, so keep website buckets separate from private data buckets, ideally in their own project. Never put anything secret in this bucket.

Step 3: Add the load balancer with CDN

The global external Application Load Balancer is what gives you a static anycast IP, HTTPS and caching. A backend bucket points the load balancer at your bucket; a URL map decides which backend serves which path (the lesson on load balancing and CDN explains each piece). In the console this is one wizard; with gcloud it is a chain of resources:

Terminal
# 1. A backend bucket with Cloud CDN enabled
gcloud compute backend-buckets create site-backend \
  --gcs-bucket-name=$BUCKET \
  --enable-cdn \
  --cache-mode=CACHE_ALL_STATIC

# 2. A reserved global IP address
gcloud compute addresses create site-ip --global --ip-version=IPV4

# 3. A URL map that sends everything to the backend bucket
gcloud compute url-maps create site-map \
  --default-backend-bucket=site-backend

# 4. A Google-managed certificate for your domain
gcloud compute ssl-certificates create site-cert \
  --domains=www.example.com --global

# 5. The HTTPS proxy and the forwarding rule on port 443
gcloud compute target-https-proxies create site-https-proxy \
  --url-map=site-map --ssl-certificates=site-cert
gcloud compute forwarding-rules create site-https-rule \
  --address=site-ip --global \
  --target-https-proxy=site-https-proxy --ports=443

Show the IP you need for DNS:

Terminal
gcloud compute addresses describe site-ip --global --format="value(address)"

Step 4: Point DNS at the load balancer

At your registrar, or in Cloud DNS if you host the zone there, create an A record for www.example.com with the reserved IP. With Cloud DNS:

Terminal
gcloud dns managed-zones create example-zone \
  --dns-name=example.com. --description="example.com zone"

gcloud dns record-sets create www.example.com. \
  --zone=example-zone --type=A --ttl=300 \
  --rrdatas=203.0.113.10

(203.0.113.10 is a documentation address; use your own.) If the domain is registered elsewhere, change its name servers to the ones from gcloud dns managed-zones describe example-zone.

The managed certificate becomes ACTIVE only after DNS points at the load balancer. Provisioning commonly takes from several minutes up to an hour or so; check with gcloud compute ssl-certificates describe site-cert --global. Until then, HTTPS requests fail with a certificate error, which is normal.

Step 5: Redirect HTTP to HTTPS

Visitors who type http:// hit port 80, where nothing listens yet. Add a second URL map that only redirects, and an HTTP proxy and rule on port 80 that use it:

Terminal
cat > redirect.yaml <<'EOF'
kind: compute#urlMap
name: site-redirect
defaultUrlRedirect:
  redirectResponseCode: MOVED_PERMANENTLY_DEFAULT
  httpsRedirect: true
EOF

gcloud compute url-maps import site-redirect --source=redirect.yaml --global
gcloud compute target-http-proxies create site-http-proxy --url-map=site-redirect
gcloud compute forwarding-rules create site-http-rule \
  --address=site-ip --global \
  --target-http-proxy=site-http-proxy --ports=80

Step 6: Deploy updates and invalidate the cache

To publish a change, upload the files again. Visitors still see the old cached copy until its max-age runs out. For urgent changes, invalidate the cache:

Terminal
gcloud storage rsync --recursive ./site gs://$BUCKET
gcloud compute url-maps invalidate-cdn-cache site-map --path="/index.html"

The better habit is fingerprinted file names (app.3f9a1c.js): they can be cached for a year because a new build gets a new name, and only the small HTML file needs a short lifetime.

Costs and cleanup

You pay for stored bytes, requests to the bucket on cache misses, load balancer forwarding rules, and data transferred out. The forwarding rules cost money even with no traffic, so a practice site is not free to leave running. Prices change; read the current pricing pages. When you are done:

Terminal
gcloud compute forwarding-rules delete site-https-rule site-http-rule --global
gcloud compute target-https-proxies delete site-https-proxy
gcloud compute target-http-proxies delete site-http-proxy
gcloud compute url-maps delete site-map site-redirect
gcloud compute ssl-certificates delete site-cert --global
gcloud compute backend-buckets delete site-backend
gcloud compute addresses delete site-ip --global
gcloud storage rm --recursive gs://$BUCKET

Or simply delete the project if it was only for this exercise.

Stand-in: a cache simulator

Cloud CDN is not something you can run here, but the idea is a few lines of code: keep a copy of each response, serve it until its time to live expires, and go to the origin on a miss. This simulator uses a fake clock so the output is always the same. It illustrates the logic only; the real CDN also has cache keys, edge locations and request collapsing.

JS Stand-in: a tiny CDN in front of a bucket

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the output from top to bottom: the first visit misses and reads the origin; repeat visits are hits and cost the bucket nothing. After you deploy v2, visitors keep getting v1 until the TTL expires or you invalidate. That delay is exactly why HTML gets a short max-age while fingerprinted assets get a very long one.

A second stand-in: a site-checklist validator

Before the load balancer exists, you can catch mistakes in the site folder itself. This stand-in checks a list of file names against the rules from this project.

JS Stand-in: check the site folder

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the role of the backend bucket in the load balancer?

    A backend bucket connects the URL map to a Cloud Storage bucket, with Cloud CDN optionally enabled on it.
  2. 2. Why does a Google-managed certificate stay in a provisioning state at first?

    Google validates domain control through the DNS pointing at the load balancer, so the certificate becomes active after DNS is set.
  3. 3. You deploy a new index.html but visitors still see the old one. What explains it?

    Cached responses stay valid for their time to live. Use a short max-age for HTML or invalidate the CDN cache.
  4. 4. Why use fingerprinted file names like app.3f9a1c.js?

    A changed file gets a new URL, so old cached copies never go stale and can live for a year.

Summary

  • A static site is a bucket (origin), a backend bucket and URL map, a load balancer with a managed certificate, Cloud CDN, and a DNS A record.
  • Website buckets are public by design: keep them separate and free of secrets.
  • Use short cache lifetimes for HTML and long ones for fingerprinted assets; invalidate for emergencies.
  • Add an HTTP-to-HTTPS redirect, and delete forwarding rules and IPs when the practice is over.

Next: Project: Deploy a Containerized API on Cloud Run