Set up a backend bucket

Cloud CDN leverages your choice of either the global external Application Load Balancer or the classic Application Load Balancer to provide routing, health checking, and anycast IP support. Because global external Application Load Balancers can have multiple backend instance types— Compute Engine VM instances, Google Kubernetes Engine Pods, Cloud Storage buckets, or external backends outside of Google Cloud—you can choose which backends (origins) to enable Cloud CDN for.

This setup guide shows you how to create an external Application Load Balancer with Cloud CDN enabled. The example uses the following resources:

  • The default Virtual Private Cloud (VPC) network
  • A default URL map
  • A reserved external IP address
  • A Cloud Storage bucket as the backend
  • A single load balancer backend bucket that acts as a wrapper around the Cloud Storage bucket

A backend bucket supports the following:

  • Cloud Storage buckets of any storage class, including multi-region buckets
  • Cloud CDN policies for caching content at Google's global edge

To learn how Cloud CDN works, see the Cloud CDN overview.

By default, Cloud Storage uses the same cache that Cloud CDN uses. If you enable Cloud CDN on the backend bucket, you can use Cloud CDN controls on your content. Cloud CDN controls include, for example, cache modes, signed URLs, and invalidation. Cloud CDN also lets you cache large content (> 10 MB). If you don't enable Cloud CDN on your backend bucket, you can only use origin Cache-Control headers to control caching for smaller content, as set by the Cloud Storage metadata. When Cloud CDN is enabled, you can utilize both CDN-Cache-Control and standard Cache-Control origin headers to manage edge caching behavior.

Load balancer backends

An external Application Load Balancer uses a URL map to direct traffic from specified URLs to specified services. The following table summarizes the types of backends where you can host content and services.

Load balancer backend configuration Typical media type Backend types
Backend service Dynamic (such as data)
  • Unmanaged instance groups
  • Managed instance groups
  • Network endpoint groups internal to Google Cloud
  • Network endpoint groups external to Google Cloud
Backend bucket Static (such as images)
  • Cloud Storage buckets (discussed on this page)

Before you begin

Console

  1. In the Google Cloud console, go to the Home page.

    Go to the Google Cloud home page

  2. To the right of Google Cloud, select a project from the pull-down menu.

gcloud

     gcloud config set project PROJECT_ID
   

Replace PROJECT_ID with your Google Cloud project ID.

Terraform

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID
   

Optional: Use BYOIP addresses

With bring your own IP (BYOIP), you can import your own public addresses to Google Cloud to use the addresses with Google Cloud resources. For example, if you import your own IPv4 addresses, you can assign one to the forwarding rule when you configure your load balancer. When you follow the instructions in this document to Create the external Application Load Balancer, provide the BYOIP address as the IP address.

For more information about using BYOIP, see Bring your own IP addresses.

Create a Cloud Storage bucket

If you have an existing Cloud Storage bucket that isn't already assigned to a load balancer, you can skip to the next step.

When you create a Cloud Storage bucket to use as the backend for an external Application Load Balancer with Cloud CDN, we recommend that you choose a multi-region bucket, which automatically replicates objects across multiple Google Cloud regions. This can improve the availability of your content and improve failure tolerance across your application.

Console

  1. In the Google Cloud console, open the Cloud Storage Buckets page.

    Open the Buckets page

  2. Click Create bucket.
  3. Specify values for the fields in the following table, leaving all others at their defaults.

    Property Value (type the value or select an option as specified)
    Name For each bucket, enter a globally unique name. If the name you enter is not unique, you see a message to try another name.
    Location type Multi-region
    Location Select a region, such as us (multiple regions in United States).
    Default storage class Standard
    Access control Uniform
  4. Click Create.

  5. Note the name of the newly created Cloud Storage bucket for the next step.

gcloud

gcloud storage buckets create gs://BUCKET_NAME --project=PROJECT_ID --default-storage-class=standard --location=us-east1 --uniform-bucket-level-access

Terraform

To create a bucket, use the google_storage_bucket resource.

# Cloud Storage bucket
resource "random_id" "bucket_prefix" {
  byte_length = 8
}

resource "google_storage_bucket" "default" {
  name                        = "${random_id.bucket_prefix.hex}-my-bucket"
  location                    = "us-east1"
  uniform_bucket_level_access = true
  storage_class               = "STANDARD"
  // delete bucket and contents on destroy.
  force_destroy = true
  // Assign specialty files
  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
}

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

Copy a graphic file into your Cloud Storage bucket

To enable you to test the setup, copy a graphic file from a public Cloud Storage bucket to your own Cloud Storage bucket.

gcloud

Run the following command in Cloud Shell. Replace BUCKET_NAME with your unique Cloud Storage bucket name:

gcloud storage cp gs://gcp-external-http-lb-with-bucket/three-cats.jpg gs://BUCKET_NAME/never-fetch/

Terraform

To copy the object, use the local-exec Provisioner with the gcloud storage cp command.

resource "null_resource" "upload_image" {
  provisioner "local-exec" {
    command = "gcloud storage cp gs://gcp-external-http-lb-with-bucket/three-cats.jpg gs://${google_storage_bucket.default.name}/never-fetch/ --recursive"
  }
}

Alternatively, to upload an object, use the google_storage_bucket_object resource.

# image object for testing, try to access http://<your_lb_ip_address>/test.jpg
resource "google_storage_bucket_object" "test_image" {
  name = "test-object"
  # Uncomment and add valid path to an object.
  #  source       = "/path/to/an/object"
  #  content_type = "image/jpeg"

  # Delete after uncommenting above source and content_type attributes
  content      = "Data as string to be uploaded"
  content_type = "text/plain"

  bucket = google_storage_bucket.default.name
}

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

In the Google Cloud console, click Refresh to verify that the graphic file is copied.

Make your Cloud Storage bucket accessible

You can make your Cloud Storage bucket publicly readable or restrict access to it.

Make your Cloud Storage bucket public

This example makes your Cloud Storage bucket publicly readable. This is the recommended approach for public content. With this setting, anyone on the internet can view and list your objects and their metadata, excluding ACLs. To reduce the risk of unintended data exposure, you should typically dedicate specific Cloud Storage buckets for public objects.

The following are alternatives to making an entire Cloud Storage bucket public:

The following procedure grants all users access to view objects in your Cloud Storage bucket, making the bucket publicly readable.

Console

  1. In the Google Cloud console, open the Cloud Storage Buckets page.

    Open the Buckets page

  2. Navigate to the bucket and click the Permissions tab.
  3. Click Add principals.
  4. In New principals, enter allUsers.
  5. For the role, select Cloud Storage > Storage Object Viewer.
  6. Click Save.

gcloud

gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME --member=allUsers --role=roles/storage.objectViewer

Terraform

To make your Cloud Storage bucket public, use the google_storage_bucket_iam_member resource.

# make bucket public
resource "google_storage_bucket_iam_member" "default" {
  bucket = google_storage_bucket.default.name
  role   = "roles/storage.objectViewer"
  member = "allUsers"
}

To learn how to apply or remove a Terraform configuration, see Basic Terraform commands.

Make your Cloud Storage bucket private

To enable access to your private buckets, grant the required IAM roles to the service account that performs cache fills.

Console

  1. In the Google Cloud console, go to the Buckets page.

    Go to Buckets

  2. Navigate to the bucket that you want to enable access to and click the Permissions tab.

  3. In the Permissions section, on the View by principals tab, click Grant Access.

  4. In the Add principals section, in the New principals field, enter the email address of the calling service account: service-PROJECT_NUM@https-lb.iam.gserviceaccount.com.

  5. In the Assign roles section, in the Select a role field, select Cloud Storage > Storage Object Viewer. This role provides read-only access.

  6. Click Save.

Additionally, so that all content served from the bucket is cached, set the cache mode to Force cache all content and specify a maximum TTL value.

gcloud

To grant Cloud CDN access to a bucket, grant the objectViewer role to the service account:

  gcloud storage buckets add-iam-policy-binding gs://BUCKET_NAME \
      --member=serviceAccount:service-PROJECT_NUM@https-lb.iam.gserviceaccount.com \
      --role=roles/storage.objectViewer

Replace the following:

  • BUCKET_NAME: the name of the Cloud Storage bucket to use
  • PROJECT_NUM: the number of your Google Cloud project