# VPC Networks - Controlling Access - GSP213

## Overview

In the real-world you need to protect sensitive data and ensure the continued availability of your web applications at all times. Learn how to use the Google Cloud VPC network to create a more secure, scalable, and manageable web server deployment within your Google Cloud environment.

In this lab, you create two nginx web servers on the default VPC network and control external HTTP access to the web servers using tagged firewall rules. Then, you explore IAM roles and service accounts.

* Two web servers gives you redundancy - if one web server fails, the other can continue serving web traffic, preventing downtime.
    
* Tagged firewall rules provide granular control over which traffic is allowed to reach specific web servers.
    
* By assigning a service account permission to perform tasks, you're upholding the principal of least priviledge, keeping your Cloud resources safe.
    

### Objectives

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

* Create a nginx web server on a vpc network
    
* Create tagged firewall rules
    
* Create a service account with IAM roles
    
* Explore permissions for the Network Admin and Security Admin roles
    

## 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-e68b0c82bab2@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
    ouzJD9bdTlnM
    ```
    
    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-01-d374a228517b`. 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-01-d374a228517b
```

`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-e68b0c82bab2@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-01-d374a228517b
```

**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 the web servers

In this section, you will create two web servers (**blue** and **green**) in the **default** VPC network. Then, you will install **nginx** on the web servers and modify the welcome page to distinguish the servers.

### **Create the blue server**

Create the **blue** server with a network tag.

1. In the Cloud console, navigate to **Navigation menu** () &gt; **Compute Engine** &gt; **VM instances**.
    
2. To create a new instance, click **Create Instance**.
    
3. In the **Machine configuration**.
    
    Select the following values:
    
    | **Property** | **Value (type value or select option as specified)** |
    | --- | --- |
    | Name | blue |
    | Region | `us-central1` |
    | Zone | `us-central1-c` |
    
    For more information on available regions and zones, in the Google Cloud Compute Engine Region and Zone guide, see [the Available regions and zones section of the Compute Engine Guides](https://cloud.google.com/compute/docs/regions-zones/#available).
    
4. Click **Networking**.
    
    * For **Network tags**, type `web-server`.
        
    
    **Note:** Networks use network tags to identify which VM instances are subject to certain firewall rules and network routes. Later in this lab, you create a firewall rule to allow HTTP access for VM instances with the **web-server** tag. Alternatively, you could check the **Allow HTTP traffic** checkbox, which would tag this instance as **http-server** and create the tagged firewall rule for tcp:80 for you.
    
5. Click **Create**.
    

#### Test Completed Task

Click **Check my progress** to verify your performed task. If you have completed the task successfully you will be granted an assessment score.

Create the blue server.

**Check my progress**

### **Create the green server**

Create the **green** server without a network tag.

1. Still in the Console, on the **VM instances** page, click **Create Instance**.
    
2. In the **Machine configuration**.
    
    Select the following values:
    
    | **Property** | **Value (type value or select option as specified)** |
    | --- | --- |
    | Name | green |
    | Region | `us-central1` |
    | Zone | `us-central1-c` |
    
3. Click **Create**.
    

#### Test Completed Task

Click **Check my progress** to verify your performed task. If you have completed the task successfully you will be granted an assessment score.

Create the green server.

**Check my progress**

### **Install nginx and customize the welcome page**

Install nginx on both VM instances and modify the welcome page to distinguish the servers.

1. Still in the **VM instances** dialog, for **blue**, click **SSH** to launch a terminal and connect.
    
2. In the SSH terminal to blue, run the following command to install nginx:
    

```apache
sudo apt-get install nginx-light -y
```

3. Open the welcome page in the nano editor:
    

```apache
sudo nano /var/www/html/index.nginx-debian.html
```

4. Replace the `<h1>Welcome to nginx!</h1>` line with `<h1>Welcome to the blue server!</h1>`.
    
5. Press **CTRL+o**, **ENTER**, **CTRL+x**.
    
6. Verify the change:
    

```apache
cat /var/www/html/index.nginx-debian.html
```

The output should contain the following:

```apache
<h1>Welcome to the blue server!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
```

7. Close the SSH terminal to **blue**:
    

```apache
exit
```

Repeat the same steps for the **green** server:

8. For **green**, click **SSH** to launch a terminal and connect.
    
9. Install nginx:
    

```apache
sudo apt-get install nginx-light -y
```

10. Open the welcome page in the nano editor:
    

```apache
sudo nano /var/www/html/index.nginx-debian.html
```

11. Replace the `<h1>Welcome to nginx!</h1>` line with `<h1>Welcome to the green server!</h1>`.
    
12. Press **CTRL+o**, **ENTER**, **CTRL+x**.
    
13. Verify the change:
    

```apache
cat /var/www/html/index.nginx-debian.html
```

The output should contain the following:

```apache
<h1>Welcome to the green server!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
```

14. Close the SSH terminal to **green**:
    

```apache
exit
```

#### Test Completed Task

Click **Check my progress** to verify your performed task. If you have completed the task successfully you will be granted an assessment score.

Install Nginx and customize the welcome page.

**Check my progress**

## Task 2. Create the firewall rule

Create the tagged firewall rule and test HTTP connectivity.

### Create the tagged firewall rule

Create a firewall rule that applies to VM instances with the **web-server** network tag.

1. In the Cloud console, navigate to **Navigation menu** (
    
    ![Navigation menu icon](https://cdn.qwiklabs.com/tkgw1TDgj4Q%2BYKQUW4jUFd0O5OEKlUMBRYbhlCrF0WY%3D align="left")
    
    ) &gt; **VPC network** &gt; **Firewall**.
    
2. Notice the **default-allow-internal** firewall rule.
    

**Note:** The **default-allow-internal** firewall rule allows traffic on all protocols/ports within the **default** network. You want to create a firewall rule to allow traffic from outside this network to only the **blue** server, by using the network tag **web-server**.

3. Click **Create Firewall Rule**.
    
4. Set the following values, leave all other values at their defaults.
    
    | **Property** | **Value (type value or select option as specified)** |
    | --- | --- |
    | Name | allow-http-web-server |
    | Network | default |
    | Targets | Specified target tags |
    | Target tags | web-server |
    | Source filter | IPv4 Ranges |
    | Source IPv4 ranges | 0.0.0.0/0 |
    | Protocols and ports | Specified protocols and ports, and then *check* tcp, *type:* 80; and *check* Other protocols, *type:* icmp. |
    

**Note:** Make sure to include the **/0** in the **Source IP ranges** to specify all networks.

5. Click **Create**.
    

#### Test Completed Task

Click **Check my progress** to verify your performed task. If you have completed the task successfully you will be granted an assessment score.

Create the tagged firewall rule.

**Check my progress**

### Create a test-vm

Create a **test-vm** instance using the Cloud Shell command line.

1. Open a new Cloud Shell terminal.
    
2. Run the following command to create a **test-vm** instance, in the `us-central1-c` zone:
    

```apache
gcloud compute instances create test-vm --machine-type=e2-micro --subnet=default --zone=us-central1-c
```

The output should look like this:

```apache
NAME     ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
test-vm  us-central1-c  e2-micro                   10.142.0.4   35.237.134.68  RUNNING
```

**Note:** You can easily create VM instances from the Console or the gcloud command line.

#### Test Completed Task

Click **Check my progress** to verify your performed task. If you have completed the task successfully you will be granted an assessment score.

Create a test-vm.

**Check my progress**

### Test HTTP connectivity

From the **test-vm**, `curl` the internal and external IP addresses of **blue** and **green**.

1. In the Console, navigate to **Navigation menu** (
    
    ![Navigation menu icon](https://cdn.qwiklabs.com/tkgw1TDgj4Q%2BYKQUW4jUFd0O5OEKlUMBRYbhlCrF0WY%3D align="left")
    
    ) &gt; **Compute Engine** &gt; **VM instances**.
    
2. Note the internal and external IP addresses of **blue** and **green**.
    
3. For **test-vm**, click **SSH** to launch a terminal and connect.
    
4. To test HTTP connectivity to **blue**'s internal IP, run the following command, replacing **blue**'s internal IP:
    

```apache
curl <Enter blue's internal IP here>
```

You should see the `Welcome to the blue server!` header.

5. To test HTTP connectivity to **green**'s internal IP, run the following command, replacing **green**'s internal IP:
    

```apache
curl -c 3 <Enter green's internal IP here>
```

You should see the `Welcome to the green server!` header.

**Note:** You are able to HTTP access both servers using their internal IP addresses. The connection on tcp:80 is allowed by the **default-allow-internal** firewall rule, as **test-vm** is on the same VPC network as the web server's **default** network.

6. To test HTTP connectivity to **blue**'s external IP, run the following command, replacing **blue**'s external IP:
    

```apache
curl <Enter blue's external IP here>
```

You should see the `Welcome to the blue server!` header.

7. To test HTTP connectivity to **green**'s external IP, run the following command, replacing **green**'s external IP:
    

```apache
curl -c 3 <Enter green's external IP here>
```

**Note:** This should not work! The request hangs.

8. Press **CTRL+c** to stop the HTTP request.
    

**Note:** As expected, you are only able to HTTP access the external IP address of the **blue** server as the **allow-http-web-server** only applies to VM instances with the **web-server** tag.

You can verify the same behavior from your browser by opening a new tab and navigating to `http://[External IP of server]`.

## Task 3. Explore the Network and Security Admin roles

Cloud IAM lets you authorize who can take action on specific resources, giving you full control and visibility to manage cloud resources centrally. The following roles are used in conjunction with single-project networking to independently control administrative access to each VPC Network:

* **Network Admin**: Permissions to create, modify, and delete networking resources, except for firewall rules and SSL certificates.
    
* **Security Admin**: Permissions to create, modify, and delete firewall rules and SSL certificates.
    

Explore these roles by applying them to a service account, which is a special Google account that belongs to your VM instance, instead of to an individual end user. Rather than creating a new user, you will authorize **test-vm** to use the service account to demonstrate the permissions of the **Network Admin** and **Security Admin** roles.

### Verify current permissions

Currently, **test-vm** uses the [Compute Engine default service account](https://cloud.google.com/compute/docs/access/service-accounts#compute_engine_default_service_account), which is enabled on all instances created by Cloud Shell command-line and the Cloud Console.

Try to list or delete the available firewall rules from **test-vm**.

1. Return to the **SSH** terminal of the **test-vm** instance.
    
2. Try to list the available firewall rules:
    

```apache
gcloud compute firewall-rules list
```

The output should look like this:

```apache
ERROR: (gcloud.compute.firewall-rules.list) Some requests did not succeed:
 - Insufficient Permission
```

**Note:** This should not work!

3. Try to delete the **allow-http-web-server** firewall rule:
    

```apache
gcloud compute firewall-rules delete allow-http-web-server
```

4. Enter **Y**, if asked to continue.
    

The output should look like this:

```apache
ERROR: (gcloud.compute.firewall-rules.delete) Could not fetch resource:
 - Insufficient Permission
```

**Note:** This should not work!

**Note:** The **Compute Engine default service account** does not have the right permissions to allow you to list or delete firewall rules. The same applies to other users who do not have the right roles.

### Create a service account

Create a service account and apply the **Network Admin** role.

1. In the Console, navigate to **Navigation menu** () &gt; **IAM & admin** &gt; **Service Accounts**.
    
2. Notice the **Compute Engine default service account**.
    
3. Click **Create service account**.
    
4. Set the **Service account name** to `Network-admin` and click **CREATE AND CONTINUE**.
    
5. For **Select a role**, select **Compute Engine** &gt; **Compute Network Admin** and click **CONTINUE** then click **DONE**.
    
6. After creating the service account `Network-admin`, click on the three dots at the right corner and click **Manage Keys** in the dropdown, then click on **Add Key** and select **Create new key** from the dropdown. Click **Create** to download your JSON output.
    
7. Click **Close**.
    
    A JSON key file download to your local computer. Find this key file, you will upload it into the VM in a later step.
    
8. Rename the JSON key file on your local machine to **credentials.json**
    

#### Test Completed Task

Click **Check my progress** to verify your performed task. If you have completed the task successfully you will be granted an assessment score.

Create a Network-admin service account.

**Check my progress**

### Authorize test-vm and verify permissions

Authorize **test-vm** to use the **Network-admin** service account.

The Network Admin role provides permissions to:Neither list, create, modify, or delete the available firewall rulesList the available firewall rulesModify the available firewall rulesDelete the available firewall rulesCreate a firewall rules

**Submit**

1. Return to the **SSH** terminal of the **test-vm** instance.
    
2. To upload **credentials.json** through the SSH VM terminal, click on the **Upload file** icon in the upper-right corner.
    
3. Select **credentials.json** and upload it.
    
4. Click **Close** in the File Transfer window.**Note**: If prompted, click **Retry** on the *Connection via Cloud Identity-Aware Proxy Failed* dialog and re-upload the file.
    
5. Authorize the VM with the credentials you just uploaded:
    

```apache
gcloud auth activate-service-account --key-file credentials.json
```

**Note:** The image you are using has the Cloud SDK pre-installed; therefore, you don’t need to initialize the Cloud SDK. If you are attempting this lab in a different environment, make sure you have followed the [procedures regarding installing the Cloud SDK](https://cloud.google.com/sdk/downloads).

6. Try to list the available firewall rules:
    

```apache
gcloud compute firewall-rules list
```

The output should look like this:

```apache
NAME                    NETWORK  DIRECTION  PRIORITY  ALLOW     DENY
allow-http-web-server   default  INGRESS    1000      tcp:80
default-allow-icmp      default  INGRESS    65534     icmp
default-allow-internal  default  INGRESS    65534     all
default-allow-rdp       default  INGRESS    65534     tcp:3389
default-allow-ssh       default  INGRESS    65534     tcp:22
```

This should work!

7. Try to delete the **allow-http-web-server** firewall rule:
    

```apache
gcloud compute firewall-rules delete allow-http-web-server
```

8. Enter **Y**, if asked to continue.
    

The output should look like this:

```apache
ERROR: (gcloud.compute.firewall-rules.delete) Could not fetch resource:
 - Required 'compute.firewall.delete' permission for 'projects/[PROJECT_ID]/global/firewall/allow-http-web-server'
```

**Note:** This should not work!

**Note:** As expected, the **Network Admin** role has permissions to list but not modify/delete firewall rules.

### Update service account and verify permissions

Update the **Network-admin** service account by providing it the **Security Admin** role.

The Security Admin role, provides permissions to:List the available firewall rulesNeither list, create, modify, or delete the available firewall rulesCreate a firewall rulesModify the available firewall rulesDelete the available firewall rules

**Submit**

1. In the Console, navigate to **Navigation menu** () &gt; **IAM & admin** &gt; **IAM**.
    
2. Find the **Network-admin** account. Focus on the **Name** column to identify this account.
    
3. Click on the pencil icon for the **Network-admin** account.
    
4. Change **Role** to **Compute Engine &gt; Compute Security Admin**.
    
5. Click **Save**.
    
6. Return to the **SSH** terminal of the **test-vm** instance.
    
7. Try to list the available firewall rules:
    

```apache
gcloud compute firewall-rules list
```

The output should look like this:

```apache
NAME                    NETWORK  DIRECTION  PRIORITY  ALLOW     DENY
allow-http-web-server   default  INGRESS    1000      tcp:80
default-allow-icmp      default  INGRESS    65534     icmp
default-allow-internal  default  INGRESS    65534     all
default-allow-rdp       default  INGRESS    65534     tcp:3389
default-allow-ssh       default  INGRESS    65534     tcp:22
```

This should work!

8. Try to delete the **allow-http-web-server** firewall rule:
    

```apache
gcloud compute firewall-rules delete allow-http-web-server
```

9. Enter **Y**, if asked to continue.
    

The output should look like this:

```apache
Deleted [https://www.googleapis.com/compute/v1/projects/qwiklabs-gcp-00e186e4b1cec086/global/firewall/allow-http-web-server].
```

This should work!

**Note:** As expected, the **Security Admin** role has permissions to list and delete firewall rules.

### Verify the deletion of the firewall rule

Verify that you can no longer HTTP access the external IP of the **blue** server, because you deleted the **allow-http-web-server** firewall rule.

1. Return to the **SSH** terminal of the **test-vm** instance.
    
2. To test HTTP connectivity to **blue**'s external IP, run the following command, replacing **blue**'s external IP:
    

```apache
curl -c 3 <Enter blue's external IP here>
```

**Note:** This should not work!

3. Press **CTRL+c** to stop the HTTP request.
    

**Note:** Provide the **Security Admin** role to the right user or service account to avoid any unwanted changes to your firewall rules!

---

## Solution of Lab

%[https://youtu.be/iKQmeOmlHLs] 
```apache
curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/GSP213/lab.sh
source lab.sh
```

**Script Alternative**

```apache
curl -LO https://raw.githubusercontent.com/Itsabhishek7py/GoogleCloudSkillsboost/refs/heads/main/VPC%20Networks%20-%20Controlling%20Access/abhishek.sh
sudo chmod +x abhishek.sh
./abhishek.sh
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1757228528835/87349ac4-776c-460e-be99-d250aa8e0cb4.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1750125699076/24b9f3df-7e29-49c6-93a2-df92c826ea21.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1757229269392/6ac42c06-6a38-4abb-ae38-8c9df5fa1ada.png align="center")
