# Cloud CDN - GSP217

## Overview

In this lab, you configure Google Cloud CDN (Content Delivery Network) for a backend bucket and verify caching of an image. Cloud CDN uses Google's globally distributed edge points of presence to cache HTTP(S) load balanced content close to your users. Caching content at the edges of Google's network provides faster delivery of content to your users while reducing serving costs.

For an up-to-date list of Google's Cloud CDN cache sites, refer to the [Cache locations resource](https://cloud.google.com/cdn/docs/locations).

### Objectives

In this lab, you will learn how to perform the following tasks:

* Create and populate a Cloud Storage bucket
    
* Create an HTTP Load Balancer with Cloud CDN
    
* Verify the caching of your bucket's content
    

## Setup and requirements

### Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click **Start Lab**, shows how long Google Cloud resources are made available to you.

This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

* Access to a standard internet browser (Chrome browser recommended).
    

**Note:** Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.

* Time to complete the lab—remember, once you start, you cannot pause a lab.
    

**Note:** Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

### How to start your lab and sign in to the Google Cloud console

1. Click the **Start Lab** button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
    
    * The Open Google Cloud console button
        
    * Time remaining
        
    * The temporary credentials that you must use for this lab
        
    * Other information, if needed, to step through this lab
        
2. Click **Open Google Cloud console** (or right-click and select **Open Link in Incognito Window** if you are running the Chrome browser).
    
    The lab spins up resources, and then opens another tab that shows the Sign in page.
    
    ***Tip:*** Arrange the tabs in separate windows, side-by-side.
    
    **Note:** If you see the **Choose an account** dialog, click **Use Another Account**.
    
3. If necessary, copy the **Username** below and paste it into the **Sign in** dialog.
    
    ```apache
    student-04-224ff48fa8d7@qwiklabs.net
    ```
    
    You can also find the Username in the Lab Details pane.
    
4. Click **Next**.
    
5. Copy the **Password** below and paste it into the **Welcome** dialog.
    
    ```apache
    LWmvrhmQ9gV4
    ```
    
    You can also find the Password in the Lab Details pane.
    
6. Click **Next**.
    
    **Important:** You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.
    
    **Note:** Using your own Google Cloud account for this lab may incur extra charges.
    
7. Click through the subsequent pages:
    
    * Accept the terms and conditions.
        
    * Do not add recovery options or two-factor authentication (because this is a temporary account).
        
    * Do not sign up for free trials.
        

After a few moments, the Google Cloud console opens in this tab.

**Note:** To access Google Cloud products and services, click the **Navigation menu** or type the service or product name in the **Search** field.

![Navigation menu icon and Search field](https://cdn.qwiklabs.com/9Fk8NYFp3quE9mF%2FilWF6%2FlXY9OUBi3UWtb2Ne4uXNU%3D align="left")

### Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

1. Click **Activate Cloud Shell** at the top of the Google Cloud console.
    
2. Click through the following windows:
    
    * Continue through the Cloud Shell information window.
        
    * Authorize Cloud Shell to use your credentials to make Google Cloud API calls.
        

When you are connected, you are already authenticated, and the project is set to your **Project\_ID**, `qwiklabs-gcp-00-a547825f60f3`. The output contains a line that declares the **Project\_ID** for this session:

```apache
Your Cloud Platform project in this session is set to qwiklabs-gcp-00-a547825f60f3
```

`gcloud` is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

3. (Optional) You can list the active account name with this command:
    

```apache
gcloud auth list
```

4. Click **Authorize**.
    

**Output:**

```apache
ACTIVE: *
ACCOUNT: student-04-224ff48fa8d7@qwiklabs.net

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
```

5. (Optional) You can list the project ID with this command:
    

```apache
gcloud config list project
```

**Output:**

```apache
[core]
project = qwiklabs-gcp-00-a547825f60f3
```

**Note:** For full documentation of `gcloud`, in Google Cloud, refer to [the gcloud CLI overview guide](https://cloud.google.com/sdk/gcloud).

## Task 1. Create and populate a Cloud Storage bucket

Cloud CDN content can originate from two types of backends:

* Compute Engine virtual machine (VM) instance groups
    
* Cloud Storage buckets
    

In this lab, you will configure a Cloud Storage bucket as the backend.

### Create a unique Cloud Storage bucket

1. In the Cloud Console, from the **Navigation menu** click **Cloud Storage** &gt; **Buckets**.
    
2. Click **Create bucket**.
    
3. Specify values for the following fields, leave all others at their defaults:
    
    | **Property** | **Value (type value or select option as specified)** |
    | --- | --- |
    | Name | Enter a globally unique name.  
    *If the name you enter is not unique, you'll see a message to try another name.* |
    | Location type | Multi-regional |
    | Location | Choose a location that is very far from you. |
    

**Note:** Try to choose a location that is either half-way around the world from you or at least on a different continent. Doing this will provide a greater difference between accessing the image with and without Cloud CDN enabled.

4. Click **Continue**.
    
5. For **Choose how to control access to objects** in **Prevent public access** Dialog box uncheck `Enforce public access prevention on this bucket` and click **Continue**.
    
6. Click **Create**.
    
7. Note the name of your storage bucket for the next step. It will be referred to as `[your-storage-bucket]`.
    

### Copy an image file into your bucket

Copy an image from a public Cloud Storage bucket to your own bucket.

1. Run the following command in Cloud Shell, replacing `[your-storage-bucket]` with your bucket's name:
    

```apache
gsutil cp gs://cloud-training/gcpnet/cdn/cdn.png gs://[your-storage-bucket]
```

2. Click **Authorize**.
    
3. On the Bucket details page, click **Refresh** to verify that the image was copied into your bucket.
    

![refresh button and cdn.png file copied into bucket highlighted](https://cdn.qwiklabs.com/UCK8gLHdS9QkRxzlf%2F%2FLA115hcbyFSFuOwSNUjYMVdQ%3D align="left")

### Make the bucket public

The recommended practice is to dedicate specific Cloud Storage buckets for public objects.

1. On the Bucket details page click the **Permissions** tab.
    
2. Click **Grant Access**.
    
3. In New principals, start typing "allUsers" then select the choice from the dropdown menu.
    
4. For the role, select **Cloud Storage** &gt; **Storage Object Viewer**.
    
5. Click **Save**.
    
6. Click **Allow public access**.
    
7. Click on the **Objects** tab.
    
8. Click **Copy URL** under Public access, then open a new browser tab and paste it in to verify that you can access the image.
    

![Google Cloud CDN image displaying over 80 CDN points of presence on the world map](https://cdn.qwiklabs.com/yhlKiNRPtfXeXDb7ZA2lUraFXG9XGuuJrbnUG0K3%2Fg0%3D align="left")

Click *Check my progress* to verify the objective.

Create and populate a Cloud Storage bucket

**Check my progress**

## Task 2. Create the HTTP Load Balancer with Cloud CDN

HTTP(S) load balancing provides global load balancing for HTTP(S) requests of static content to a Cloud Storage bucket (backend). By enabling Cloud CDN on your backend, your content is cached at a [location at the edge of Google's network](https://cloud.google.com/cdn/docs/locations), which is usually far closer to the user than your backend.

### Start the HTTP Load Balancer Configuration

1. In the Cloud Console, from the **Navigation menu** (
    
    ![Navigation menu icon](https://cdn.qwiklabs.com/tkgw1TDgj4Q%2BYKQUW4jUFd0O5OEKlUMBRYbhlCrF0WY%3D align="left")
    
    ) click **View All Products &gt; Network services** &gt; **Load Balancing**.
    
2. Click **\+ Create load balancer**.
    
3. Under **Type of load balancer**, select **Application Load Balancer (HTTP/HTTPS)** and click **Next**.
    
4. Leave all the settings as default and click **Configure**.
    
5. For **Load Balancer Name**, type **cdn-lb**.
    

### Configure the backend

1. Click on **Backend configuration**.
    
2. For **Backend services & backend buckets**, click **backend services & backend buckets** dropdown menu, then click **Create a backend bucket**.
    
3. Set the **Name** to `cdn-bucket`.
    
4. Under **Cloud Storage bucket** click **Browse**.
    
5. Click your bucket and click **Select**.
    
6. Check **Enable Cloud CDN**.
    
7. **Cache Static Content** is selected by default, for this lab, set the **Client TTL**, **Default TTL** and **Maximum TTL** to **1 minute**.
    
8. Click **Create**.
    

**Note:** Yes, enabling Cloud CDN is as simple as checking **Enable Cloud CDN**!

### Configure the frontend

**Note:** The host and path rules determine how your traffic will be directed. For example, you could direct video traffic to one backend and image traffic do another backend. However, you are not configuring the Host and path rules in this lab. Ignore **Host and path rules** option.

1. Click **Frontend configuration**.
    
2. Specify the following, leave all other values with their defaults:
    
    | **Property** | **Value (type value or select option as specified)** |
    | --- | --- |
    | Protocol | HTTP |
    | IP version | IPv4 |
    | IP address | Ephemeral |
    | Port | 80 |
    
3. Click **Done**.
    

### Review and create the HTTP Load Balancer

1. Click **Review and finalize**.
    
2. Review the **Backend Buckets** and **Frontend**.
    
    ![The frontend protocol: http, IP port: 80, and the backend cdn-bucket with Cloud CDN enabled](https://cdn.qwiklabs.com/eGKF1gdWWyazZZP94PyQynXlK%2Ba2TiPJ6clVWLhv28E%3D align="left")
    
3. Click **Create** and wait for the load balancer to be created.
    
4. Click on the name of the load balancer (**cdn-lb**).
    
5. Note the IP address of the load balancer for the next task. It will be referred to as `[LB_IP_ADDRESS]`.
    

Click *Check my progress* to verify the objective.

Create the HTTP Load Balancer with Cloud CDN

**Check my progress**

## Task 3. Verify the caching of your bucket's content

Now that you have created the HTTP Load Balancer for your bucket and enabled Cloud CDN, it is time to verify that the image gets cached on the edge of Google's network.

### Time the HTTP request for the image

One way to verify that the image gets cached is to time the HTTP request for the image. The first request should take significantly longer as content is only cached at an edge location after being accessed through that location.

1. In Cloud Shell, store the IP address of the Load Balancer in an environment variable:
    

```apache
export LB_IP_ADDRESS=<Enter the IP address of the Load Balancer>
```

2. Run the following to time 3 consecutive HTTP requests:
    

```apache
for i in {1..3};do curl -s -w "%{time_total}\n" -o /dev/null http://$LB_IP_ADDRESS/cdn.png; done
```

The output should look like this example:

```apache
1.234357
0.009600
0.006652
```

**Note:** In this example output, the second and third request take less than 1% of the time of the first request. This demonstrates that the image was cached during the first request and accessed from an edge location on further requests. Depending on how far you placed your storage bucket and where your closest edge location is, you will see different results.

3. Repeat the command a few times to provide logs:
    

```apache
for i in {1..3};do curl -s -w "%{time_total}\n" -o /dev/null http://$LB_IP_ADDRESS/cdn.png; done
```

### Explore the Cloud CDN logs

Another way to verify that the image got cached in the previous step is to explore the Cloud CDN logs. These logs will contain information on when content was cached and when the cache was accessed.

1. In the Cloud Console, from the **Navigation menu**, click **View all products**. Under the **Observability** section, click **Logging** &gt; **Logs Explorer**.
    
2. Under the **Resources** filter, select **Application Load Balancer** &gt; **cdn-lb-forwarding-rule** &gt; **cdn-lb**. Then click **Apply**.
    
3. Click **Run Query**.
    

**Note:** If you see "warnings" in the query, go back to Cloud Shell and re-run the last command a few times to generate more logs.

4. Expand the first log entry (on top).
    
5. Within the entry, expand the **httpRequest** and notice that the **cacheLookup** is `true` but there is no **cacheHit** field. This illustrates that the cache did not contain the image on this first request.
    
6. Expand the **jsonPayload** and notice that the **statusDetails** field contains `response_sent_by_backend`. This also illustrates that the image came from the backend bucket on this first request.
    
7. Close the current log entry and expand a log entry towards the bottom of the query results.
    
8. Within the entry, expand the **httpRequest** and notice that the **cacheLookup** and **CacheHit** is `true`. This illustrates that the cache contained the image on this request.
    
9. Expand the **jsonPayload** and notice that the **statusDetails** field contains `response_from_cache`. This also illustrates that the cache provided the image on this request, instead of the backend.
    

**Note:** The Cloud CDN logs clearly demonstrate that the image was provided from the backend on the first request. This request filled the cache on the edge location and all future requests got the image from that cache.

---

## Solution of Lab

%[https://youtu.be/MonCIYHGcxE] 

```apache
curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/GSP217/lab.sh
source lab.sh
```

**Script Alternative**

```apache
curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/GSP217/solution-2.sh
source solution-2.sh
```

**Script Alternative**

```apache

PROJECT_ID=$(gcloud config get-value project)
BUCKET_NAME="$PROJECT_ID"

gsutil mb -l US gs://$BUCKET_NAME


gsutil cp gs://cloud-training/gcpnet/cdn/cdn.png gs://$BUCKET_NAME


gsutil iam ch allUsers:objectViewer gs://$BUCKET_NAME


TOKEN=$(gcloud auth application-default print-access-token)

curl -X POST -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "bucketName": "'"$PROJECT_ID"'",
    "cdnPolicy": {
      "cacheMode": "CACHE_ALL_STATIC",
      "clientTtl": 60,
      "defaultTtl": 60,
      "maxTtl": 60,
      "negativeCaching": false,
      "serveWhileStale": 0
    },
    "compressionMode": "DISABLED",
    "description": "",
    "enableCdn": true,
    "name": "cdn-bucket"
  }' \
  "https://compute.googleapis.com/compute/v1/projects/$PROJECT_ID/global/backendBuckets"

sleep 20

curl -X POST -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "defaultService": "projects/'"$PROJECT_ID"'/global/backendBuckets/cdn-bucket",
    "name": "cdn-lb"
  }' \
  "https://compute.googleapis.com/compute/v1/projects/$PROJECT_ID/global/urlMaps"


sleep 20

curl -X POST -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "name": "cdn-lb-target-proxy",
    "urlMap": "projects/'"$PROJECT_ID"'/global/urlMaps/cdn-lb"
  }' \
  "https://compute.googleapis.com/compute/v1/projects/$PROJECT_ID/global/targetHttpProxies"


sleep 20

curl -X POST -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "IPProtocol": "TCP",
    "ipVersion": "IPV4",
    "loadBalancingScheme": "EXTERNAL_MANAGED",
    "name": "cdn-lb-forwarding-rule",
    "networkTier": "PREMIUM",
    "portRange": "80",
    "target": "projects/'"$PROJECT_ID"'/global/targetHttpProxies/cdn-lb-target-proxy"
  }' \
  "https://compute.googleapis.com/compute/v1/projects/$PROJECT_ID/global/forwardingRules"
```
