Skip to content

r3xakead0/gcp-webstatic-storage-cdn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Workshop: Publish a Static Website with Cloud Storage and Cloud CDN

🎯 Workshop Goals

By the end of this workshop you will be able to:

  • Create a simple static web page (HTML, CSS, JS).
  • Publish it to a private bucket in Cloud Storage.
  • Serve the site through an HTTP(S) Load Balancer + Cloud CDN.
  • Configure a custom 404 error page.
  • Validate that the bucket stays private and is only accessible through the CDN.

🧱 Workshop Architecture

User β†’ Load Balancer (Frontend) β†’ Cloud CDN β†’ Backend Bucket (Private Cloud Storage)

The bucket is not public.
The Load Balancer uses a service account to access the bucket.


0. Prerequisites

  • GCP project with billing enabled.
  • gcloud authenticated and configured:
    gcloud auth login
    gcloud config set project YOUR_PROJECT_ID
  • Suggested variables:
    export PROJECT_ID="YOUR_PROJECT_ID"
    export REGION="us-central1"
    export BUCKET_NAME="${PROJECT_ID}-static-site"

1. Build the Static Web Page

On your local machine, create a folder

mkdir static-site
cd static-site

Create the file index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>My static site on GCP</title>
  <link rel="stylesheet" href="styles.css" />
</head>
<body>
  <h1>Welcome to my static site on GCP</h1>
  <p>This site is served from private Cloud Storage through Cloud CDN.</p>
  <button id="btn">Click here</button>
  <script src="app.js"></script>
</body>
</html>

Create the file styles.css

body {
  font-family: system-ui, sans-serif;
  background: #0f172a;
  color: #e5e7eb;
  text-align: center;
  padding-top: 80px;
}

h1 { color: #38bdf8; }

button {
  padding: 10px 20px;
  border-radius: 6px;
  border: none;
  cursor: pointer;
}

Create the file app.js

document.getElementById("btn").addEventListener("click", () => {
  alert("Hello from Cloud Storage + Cloud CDN πŸ˜„");
});

Create the file 404.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Page not found</title>
  <link rel="stylesheet" href="styles.css" />
</head>
<body>
  <h1>404 - Oops, this page does not exist</h1>
  <p>Check the URL or return to the <a href="/">home page</a>.</p>
</body>
</html>

2. Create Private Bucket and Upload Files

Create bucket

gcloud storage buckets create gs://$BUCKET_NAME \
  --location=$REGION \
  --uniform-bucket-level-access

Verify it is private

gcloud storage buckets update gs://$BUCKET_NAME \
  --no-public-access-prevention=false

Upload files

gcloud storage cp * gs://$BUCKET_NAME

Test that it is NOT directly accessible:

curl -I https://storage.googleapis.com/$BUCKET_NAME/index.html
# Should show 403 Forbidden

3. Create Load Balancer + Cloud CDN (from the console)

Steps

  1. Go to Network Services β†’ Load Balancing β†’ Create Load Balancer.
  2. Select HTTP(S) Load Balancer β†’ Start configuration.
  3. Create Backend β†’ type Cloud Storage bucket β†’ select $BUCKET_NAME.
  4. Enable Cloud CDN.
  5. In Host & Path Rules, route /* to the backend bucket.
  6. In Frontend, create a static IP and port 80.
  7. Create the Load Balancer.

4. Configure Custom 404 Error

In the Load Balancer:

  1. Go to Custom Error Responses.
  2. Add a rule for code 404.
  3. Use this HTML as the response:
<h1>404 - Page not found</h1>
<p>Check the URL or return to the home page.</p>

5. Test the Site

Get the Load Balancer IP

From the console β†’ Frontend section.

Test site

curl -I http://YOUR_IP

Test 404 error

curl http://YOUR_IP/not-found

6. Verify Bucket Privacy

Direct access:

curl -I https://storage.googleapis.com/$BUCKET_NAME/index.html
# 403 Forbidden

Access via CDN:

curl -I http://YOUR_IP/index.html
# 200 OK

7. Clean Up Resources

  1. Delete the Load Balancer and its IP.
  2. Delete the bucket:
    gcloud storage rm -r gs://$BUCKET_NAME

βœ”οΈ Workshop Completed

You deployed a secure static site on GCP with:

  • Private bucket
  • CDN
  • Load Balancer
  • Custom 404 page

Excellent work! πŸš€

About

Workshop: Publish a Static Website with Cloud Storage and Cloud CDN

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published