arrow_back

VPC Flow Logs - Analyzing Network Traffic

Join Sign in

VPC Flow Logs - Analyzing Network Traffic

1 hour 5 Credits

GSP212

Google Cloud selp-paced labs logo

Overview

In this lab, you will learn how to configure a network to record traffic to and from an Apache web server using VPC Flow Logs. You will then export the logs to BigQuery for analysis.

There are multiple use cases for VPC Flow Logs. For example, you might use VPC Flow Logs to determine where your applications are being accessed from to optimize network traffic expense, to create HTTP Load Balancers to balance traffic globally, or to denylist unwanted IP addresses with Cloud Armor.

Objectives

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

  • Configure a custom network with VPC Flow Logs.
  • Create an Apache web server.
  • Verify that network traffic is logged.
  • Export the network traffic to BigQuery to further analyze the logs.
  • Setup VPC flow log aggregation.

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 will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that 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 or private browser window to run this lab. This prevents any 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: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your 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 pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google 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 Console. 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 from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. 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 Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

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 Activate Cloud Shell icon at the top of the Google Cloud console.

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

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

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

gcloud auth list

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Configure a custom network with VPC Flow Logs

Create the custom network

By default, VPC Flow Logs are disabled for a network. Therefore, you will create a new custom-mode network and enable VPC Flow Logs.

  1. In the Console, navigate to Navigation menu (mainmenu.png) > VPC network > VPC networks.

    navmenu_vpcnetworks.png

  2. Click Create VPC Network.

  3. Set the following values, leave all others at their defaults:

    Property Value (type value or select option as specified)
    Name vpc-net
    Description Enter an optional description
  4. For Subnet creation mode, click Custom.

  5. Set the following values, leave all others at their defaults:

    Property Value (type value or select option as specified)
    Name vpc-subnet
    Region us-west1
    IP address range 10.1.3.0/24
    Flow Logs On
  6. Click Done, and then click Create.

Test Completed Task

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

Create the custom network. Create a subnet for the custom network in us-west1 region.

Create the firewall rule

In order to serve HTTP and SSH traffic on the network, you need to create a firewall rule.

  1. In the left menu, click Firewall.

  2. Click Create Firewall Rule.

  3. Set the following values, leave all others at their defaults:

    Property Value (type value or select option as specified)
    Name allow-http-ssh
    Network vpc-net
    Targets Specified target tags
    Target tags http-server
    Source filter IPv4 ranges
    Source IP ranges 0.0.0.0/0
    Protocols and ports Specified protocols and ports, and then check tcp, type: 80, 22
  1. Click Create.

Test Completed Task

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

Create the firewall rule in the custom network.

Create an Apache web server

Create the web server

  1. In the Console, navigate to Navigation menu (mainmenu.png) > Compute Engine > VM instances.

  2. Click CREATE INSTANCE.

  3. Set the following values, leave all others at their defaults:

    Property Value (type value or select option as specified)
    Name web-server
    Region us-west1
    Zone us-west1-b
    Machine type f1-micro (1 vCPU, 614 MB memory)
    Firewall Allow HTTP traffic
  4. Click Management, security, disks, networking, sole tenancy.

  5. Click Networking.

  6. For Network interfaces, click on default to edit.

  7. Set the following values, leave all others at their defaults:

    Property Value (type value or select option as specified)
    Network vpc-net
    Subnetwork vpc-subnet
  8. Click Done, and then click Create.

Test Completed Task

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

Create the web server in the custom network (zone: us-west1-b).

Install Apache

Configure the VM instance that you created as an Apache webserver and overwrite the default web page.

  1. Return to the Console, still on the VM instances page (Navigation menu (mainmenu.png) > Compute Engine > VM instances). For web-server, click SSH to launch a terminal and connect.

  2. In the web-server SSH terminal, update the package index:

sudo apt-get update
  1. Install the Apache2 package:

sudo apt-get install apache2 -y
  1. Create a new default web page by overwriting the default:

echo '<!doctype html><html><body><h1>Hello World!</h1></body></html>' | sudo tee /var/www/html/index.html
  1. Exit the SSH terminal:
exit

Test Completed Task

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

Install Apache in web server.

Verify that network traffic is logged

Generate network traffic

  1. Return to the Console, still on the VM instances page (Navigation menu (mainmenu.png) > Compute Engine > VM instances).
  2. To view web-server, click the External IP to access the server.

Find your IP address

Find the IP address of the computer you are using. One easy way to do this is to go to a website that provides this address.

  1. Click this whatismyip.host to find your IP v4 address.

  2. Copy your IP address. It will be referred to as YOUR_IP_ADDRESS.

Access the VPC Flow Logs

  1. In the Console, navigate to Navigation menu > Logging > Logs Explorer.

  2. In the Log fields panel, under Resource, click Subnetwork. In the Query results pane, entries from the subnetwork logs appear.

  3. In the Log fields panel, under Log name, click compute.googleapis.com/vpc_flows.

  4. Enter "YOUR_IP_ADDRESS" in the Query search box at the top. Then Click Run Query.

logging_filter.png

  1. Click on one of the log entries to expand it.
  2. Within the entry, click the arrows to expand the jsonPayload and then the connection. You may have to click Expand all to see the connection.

view_logs.png

Feel free to explore other fields within the log entry before moving to the next task.

Export the network traffic to BigQuery to further analyze the logs

Create an export sink

  1. In the Console, in the left pane, click Logs Explorer.

  2. From Resources dropdown, select Subnetwork. Then click Add.

  3. From Log name dropdown, check vpc_flows and click Add. Then, click Run query.

  4. Click More Actions > Create Sink.

  5. For "Sink Name", type vpc-flows and click NEXT.

  6. For "Select sink service", select the BigQuery dataset.

  7. For "Sink Destination", select Create new BigQuery dataset.

  8. For "Dataset ID", type bq_vpcflows, and then click CREATE DATASET.

  9. Click CREATE SINK. The Logs Router Sinks page appears. You should be able to see the sink you created (vpc-flows). If you are unable to see the sink click on Logs Router.

Generate log traffic for BigQuery

Now that the network traffic logs are exported to BigQuery, generate more traffic by accessing the web-server several times. Using Cloud Shell, you can curl the IP Address of the web-server several times.

  1. In the Console, navigate to Navigation menu (mainmenu.png) > Compute Engine > VM instances.

  2. Note the External IP address for the web-server instance to use in the next step. It will be referred to as EXTERNAL_IP.

  3. In the Cloud Shell command line, run the following command to store the EXTERNAL_IP in an environment variable. Replace the <EXTERNAL_IP> with the address you just noted.

export MY_SERVER=<EXTERNAL_IP>
  1. Access the web-server 50 times from Cloud Shell:
for ((i=1;i<=50;i++)); do curl $MY_SERVER; done

Test Completed Task

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

Export the network traffic to BigQuery.

Visualize the VPC Flow Logs in BigQuery

  1. In the Console, navigate to Navigation menu (mainmenu.png) > BigQuery.
  2. If prompted, re-enter the Qwiklabs-provided student password and click Sign in.
  3. On the left-hand side, expand the bq_vpcflows dataset to reveal the table. You might have to first expand the Project ID to reveal the dataset.
  4. Click on the name of the table. It should start with compute_googleapis.
  1. Click on Details tab.

  2. Copy the portion of the Table ID that is after the colon(:). It will be referred to as TABLE_ID.

  3. Add the following to the Query Editor and replace your_table_id with TABLE_ID while leaving the accents (`) on both sides:

#standardSQL SELECT jsonPayload.src_vpc.vpc_name, SUM(CAST(jsonPayload.bytes_sent AS INT64)) AS bytes, jsonPayload.src_vpc.subnetwork_name, jsonPayload.connection.src_ip, jsonPayload.connection.src_port, jsonPayload.connection.dest_ip, jsonPayload.connection.dest_port, jsonPayload.connection.protocol FROM `your_table_id` GROUP BY jsonPayload.src_vpc.vpc_name, jsonPayload.src_vpc.subnetwork_name, jsonPayload.connection.src_ip, jsonPayload.connection.src_port, jsonPayload.connection.dest_ip, jsonPayload.connection.dest_port, jsonPayload.connection.protocol ORDER BY bytes DESC LIMIT 15
  1. Click Run.

Analyze the VPC Flow Logs in BigQuery

The previous query gave you the same information that you saw in the Cloud Console. Now change the query to identify the top IP addresses that have exchanged traffic with your web-server.

  1. Create a new query in the Query Editor with the following and replace your_table_id with TABLE_ID while leaving the accents (`) on both sides:
#standardSQL SELECT jsonPayload.connection.src_ip, jsonPayload.connection.dest_ip, SUM(CAST(jsonPayload.bytes_sent AS INT64)) AS bytes, jsonPayload.connection.dest_port, jsonPayload.connection.protocol FROM `your_table_id` WHERE jsonPayload.reporter = 'DEST' GROUP BY jsonPayload.connection.src_ip, jsonPayload.connection.dest_ip, jsonPayload.connection.dest_port, jsonPayload.connection.protocol ORDER BY bytes DESC LIMIT 15
  1. Click Run.

Feel free to generate more traffic to the web-server from multiple sources and query the table again to determine the bytes sent to the server.

Add VPC Flow Log aggregation

You will now explore a new release of VPC flow log volume reduction. Not every packet is captured into its own log record. However, even with sampling, log record captures can be quite large.

You can balance your traffic visibility and storage cost needs by adjusting specific aspects of logs collection, which you will explore in this section.

Setting up aggregation

  1. In the Console, navigate to Navigation menu (mainmenu.png) > VPC network > VPC networks.

  2. Click vpc-net, and then click Edit.

  3. In the Subnets tab, click vpc-subnet:

vpc-subnet.png

  1. Click Edit > Configure logs to expose the following fields:

flowlog-settings.png

The purpose of each field is explained below:

  • Aggregation time interval: Sampled packets for a time interval are aggregated into a single log entry. This time interval can be 5 sec (default), 30 sec, 1 min, 5 min, 10 min, or 15 min.

  • Metadata annotations: By default, flow log entries are annotated with metadata information, such as the names of the source and destination VMs or the geographic region of external sources and destinations. This metadata annotation can be turned off to save storage space.

  • Log entry sampling: Before being written to the database, the number of logs can be sampled to reduce their number. By default, the log entry volume is scaled by 0.50 (50%), which means that half of entries are kept. You can set this from 1.0 (100%, all log entries are kept) to 0.0 (0%, no logs are kept).

  1. Set the Aggregation Interval to 30 seconds.

  2. Set the Sample rate to 25%.

  3. Click Save. You should see the following message:

50-percent.png

Setting the aggregation level to 30 seconds can reduce your flow logs size by up to 83% compared to the default aggregation interval of 5 seconds. Configuring your flow log aggregation can seriously affect your traffic visibility and storage costs.

Congratulations!

You have configured a VPC network, enabled VPC Flow Logs and created a webserver in that network. Then, you generated HTTP traffic to the webserver, viewed the traffic logs in the Cloud Console and analyzed the traffic logs in BigQuery.

Networking_125.png

Finish Your Quest

This self-paced lab is part of the Qwiklabs Quest, Network Performance and Optimization. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this Quest and get immediate completion credit if you've taken this lab. See other available Qwiklabs Quests.

Take Your Next Quest

Continue your learning with another Quest, for example Google Cloud Solutions I: Scaling Your Infrastructure, or check out these suggestions:

Next Steps / Learn More

For information on the basic concepts of Google Cloud Identity and Access Management:

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual last updated February 9, 2022
Lab last tested February 9, 2022

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.