Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Google Cloud Static Website

The Google Cloud Static Website template scaffolds a Pulumi project that stores site files in a Cloud Storage bucket configured for static website hosting and serves them through Cloud CDN, fronted by a Global Address for low-latency delivery and caching. The template ships with placeholder web content so the project deploys end to end out of the box.

Architecture diagram of the Google Cloud Static Website template Google Cloud Cloud CDN Cloud Storage

Before you begin#

For this walkthrough, you'll need:

Create a new project#

Create a new project from the template:

Terminal window
mkdir my-site; cd my-site
pulumi new static-website-gcp-typescript

The pulumi new command walks you through creating and configuring the new project and stack. A stack is an instance of a project, and you can have many of them (dev, staging, prod), each with its own configuration settings. Depending on the language, you may also be asked for your preferred package manager or toolchain.

This template’s settings include:

KeyDescriptionDefault
gcp:projectThe Google Cloud project to deploy into—
pathThe path to the folder containing the websitewww
indexDocumentThe file to use for top-level pagesindex.html
errorDocumentThe file to use for error pageserror.html
You can change values like these later with pulumi config set :
Terminal window
pulumi config set gcp:project <value>
pulumi config set path <value>
pulumi config set indexDocument <value>
pulumi config set errorDocument <value>

For gcp:project, enter the ID of an existing Google Cloud project. gcloud config get-value project prints the one gcloud is using. Press Enter at the others to keep the defaults.

Review the code#

When setup completes, you’ll see a few files:

  • Pulumi.yaml contains the project name and the language it’s written in.
  • Pulumi.dev.yaml contains the dev stack’s settings, including the values you just entered.
  • index.ts contains the program code that declares your cloud resources.

Take a moment to open them up and have a look. Here’s what’s in index.ts:

index.ts
import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";
import * as synced_folder from "@pulumi/synced-folder";
// Import the program's configuration settings.
const config = new pulumi.Config();
const path = config.get("path") || "www";
const indexDocument = config.get("indexDocument") || "index.html";
const errorDocument = config.get("errorDocument") || "error.html";
// Create a storage bucket and configure it as a website.
const bucket = new gcp.storage.Bucket("bucket", {
location: "US",
website: {
mainPageSuffix: indexDocument,
notFoundPage: errorDocument,
},
});
// Create an IAM binding to allow public read access to the bucket.
const bucketIamBinding = new gcp.storage.BucketIAMBinding("bucket-iam-binding", {
bucket: bucket.name,
role: "roles/storage.objectViewer",
members: ["allUsers"],
});
// Use a synced folder to manage the files of the website.
const syncedFolder = new synced_folder.GoogleCloudFolder("synced-folder", {
path: path,
bucketName: bucket.name,
});
// Enable the storage bucket as a CDN.
const backendBucket = new gcp.compute.BackendBucket("backend-bucket", {
bucketName: bucket.name,
enableCdn: true,
});
// Provision a global IP address for the CDN.
const ip = new gcp.compute.GlobalAddress("ip", {});
// Create a URLMap to route requests to the storage bucket.
const urlMap = new gcp.compute.URLMap("url-map", {defaultService: backendBucket.selfLink});
// Create an HTTP proxy to route requests to the URLMap.
const httpProxy = new gcp.compute.TargetHttpProxy("http-proxy", {urlMap: urlMap.selfLink});
// Create a GlobalForwardingRule rule to route requests to the HTTP proxy.
const httpForwardingRule = new gcp.compute.GlobalForwardingRule("http-forwarding-rule", {
ipAddress: ip.address,
ipProtocol: "TCP",
loadBalancingScheme: "EXTERNAL",
portRange: "80",
target: httpProxy.selfLink,
});
// Export the URLs and hostnames of the bucket and CDN.
export const originURL = pulumi.interpolate`https://storage.googleapis.com/${bucket.name}/index.html`;
export const originHostname = pulumi.interpolate`storage.googleapis.com/${bucket.name}`;
export const cdnURL = pulumi.interpolate`http://${ip.address}`;
export const cdnHostname = ip.address;

Deploy the project#

First, be sure to enable the Compute Engine API in your project:

Terminal window
gcloud services enable compute.googleapis.com --project $(pulumi config get gcp:project)

Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.

Terminal window
pulumi up

Pulumi adds a random suffix to most resource names, so the storage bucket bucket becomes something like bucket-4f8a2c1. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist:

originHostname

The bucket’s host and path on Cloud Storage, like storage.googleapis.com/bucket-4f8a2c1.

originURL

The HTTPS URL of the site’s home page, served straight from the storage bucket.

cdnHostname

The IP address of the load balancer in front of Cloud CDN. Point an A record at it to use a custom domain.

cdnURL

The HTTP URL of the site, served through Cloud CDN.

Read one any time with pulumi stack output <name>, or pass it to another command. This opens the site in your browser:

Terminal window
open $(pulumi stack output cdnURL)

A new load balancer can take a few minutes to start answering, so the CDN address may show an error at first. The bucket answers right away. Fetch the home page from it with curl; it prints the template’s placeholder page, which says Hello, world!:

Terminal window
curl -s $(pulumi stack output originURL)
Need help?

If pulumi up reports failed to load application credentials, sign in again:

Terminal window
gcloud auth application-default login

Still stuck? Let us know.

Customize the project#

Projects created with this template expose the following configuration settings:

path

The path to the folder containing the files of the website. Defaults to www, which is the name (and relative path) of the folder included with the template.

indexDocument

The file to use for top-level pages. Defaults to index.html.

errorDocument

The file to use for error pages. Defaults to error.html.

All of these settings are optional and may be adjusted either by editing the stack configuration file directly (by default, Pulumi.dev.yaml) or by changing their values with pulumi config set.

Using your own web content#

If you already have a static website you’d like to deploy on Google Cloud with Pulumi, you can do so either by replacing placeholder content in the www folder or by configuring the stack to point to another folder on your computer with the path setting. Replace ../my-existing-website/build with the path to your site’s files:

Terminal window
pulumi config set path ../my-existing-website/build
pulumi up

Changing the error page#

The error page is what the site returns for a path it doesn’t have. Point it at the home page instead, then ask the CDN for a page that doesn’t exist. curl prints the home page, with Hello, world!, in place of the template’s Oops! That page wasn't found.:

Terminal window
pulumi config set errorDocument index.html
pulumi up
curl -s $(pulumi stack output cdnURL)/no-such-page

Cleaning up#

You can cleanly destroy the stack and all of its infrastructure with pulumi destroy:

Terminal window
pulumi destroy

Learn more#

Related

The infrastructure platform for humans and agents.