Skip to main content
Google Cloud Platform (GCP)
CHAPTER 04

Compute Engine Virtual Machines

Updated: May 15, 2026
25 min read

# CHAPTER 4

Compute Engine Virtual Machines

1. Introduction

While serverless technologies and containers are the future of the cloud, the foundational building block of all cloud infrastructure remains the Virtual Machine (VM). In Google Cloud, this service is called Compute Engine. It allows you to rent a slice of Google's physical hardware, install any Operating System you want, and have absolute, root-level control over the server. In this chapter, we will provision a Linux server, configure its firewall rules, and transform it into a live web server accessible to the public internet.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define Compute Engine and its role as an IaaS offering.
  • Understand the concept of Machine Families (e2, n2, c2).
  • Provision a Virtual Machine in a specific Region and Zone.
  • Configure Firewall network tags to allow HTTP traffic.
  • SSH into a VM directly from the browser.
  • Install a web server (Apache) to host a public page.

3. Beginner-Friendly Explanation

Imagine renting an empty apartment (The Virtual Machine).
  • Machine Type: You decide how big the apartment is. Do you need a tiny 1-bedroom (e2-micro) or a massive 10-bedroom penthouse (n2-highmem)?
  • Boot Disk: The furniture. Do you want it pre-furnished with modern furniture (Ubuntu Linux) or corporate furniture (Windows Server)?
  • Firewall: The building security guard. By default, the guard doesn't let anyone into your apartment. If you want to throw a party, you have to explicitly give the guard a rule: "Allow people entering via the HTTP door (Port 80)."

4. Machine Families

Google offers different hardware optimized for different tasks:
  • E2 / N2 (General Purpose): Best balance of CPU and RAM. Great for web servers and basic databases.
  • C2 (Compute-Optimized): Massive CPU power. Used for video rendering or high-frequency stock trading.
  • M2 (Memory-Optimized): Massive RAM (up to 12 Terabytes!). Used for massive in-memory databases like SAP HANA.

5. Boot Disks and Images

Every VM needs a hard drive (Boot Disk). Google provides dozens of pre-configured "Public Images" (Ubuntu, Debian, CentOS, Windows Server). When you click "Create," Google instantly clones that image onto your new hard drive so the server boots in seconds.

6. Ephemeral vs. Static IPs

By default, your VM gets an Ephemeral (Temporary) External IP. If you restart the VM, Google might take that IP away and give you a new one! If you are hosting a real website and pointing a Domain Name (like mysite.com) at your server, you must reserve a Static External IP so the address never changes.

7. Mini Project: Launch a Web Server

Let's build a server and put it on the internet.

Step-by-Step Tutorial:

  1. 1. In the GCP Console, navigate to Compute Engine > VM instances.
  1. 2. Click Create Instance.
  1. 3. Name: my-first-webserver
  1. 4. Region/Zone: Choose a region close to you (e.g., us-central1-a).
  1. 5. Machine configuration: Choose General-purpose, Series E2, Machine type e2-micro (This is free tier eligible!).
  1. 6. Boot disk: Leave it as the default (usually Debian Linux).
  1. 7. Firewall: CRITICAL STEP. Check the box that says "Allow HTTP traffic". If you forget this, your website will be invisible.
  1. 8. Click Create. Wait 30 seconds for the green checkmark to appear.
  1. 9. Click the SSH button next to your VM. A terminal will magically open in your browser!
  1. 10. In the terminal, install an Apache web server:

bash
12
sudo apt-get update
sudo apt-get install apache2 -y
  1. 11. Go back to the GCP Console. Find the External IP of your VM.
  1. 12. Click the External IP link (or paste it into a new browser tab). You will see the "Apache2 Debian Default Page"! You have officially built a public cloud server.

8. Real-World Scenarios

A video game company is launching a new multiplayer game. They know launch day will be insane, but traffic will drop off a month later. Instead of buying physical servers, they use Compute Engine to launch 500 c2-standard (Compute-optimized) VMs globally. To save massive amounts of money, they use Preemptible VMs (Spot Instances)—excess Google capacity that is up to 80% cheaper, with the caveat that Google can randomly shut them down if they need the capacity back. Because the game architecture is fault-tolerant, if one VM is terminated, players seamlessly reconnect to another.

9. Best Practices

  • Startup Scripts: In the Mini Project, we manually typed apt-get install via SSH. Professionals never do this. In the VM creation menu, under "Advanced Options", you can paste a Startup Script containing your bash commands. The moment the VM boots, Google runs the script automatically, instantly transforming an empty Linux box into a fully configured web server without human intervention.

10. Cost Optimization Tips

  • Committed Use Discounts: If you know your company will need a server running 24/7 for the next 3 years, you can sign a "Commitment" contract with Google Cloud. They will slash the hourly price of that VM by up to 57%!

11. CLI Examples

To create a VM using the command line:
bash
123456
gcloud compute instances create my-cli-server \
    --zone=us-central1-a \
    --machine-type=e2-micro \
    --image-family=debian-11 \
    --image-project=debian-cloud \
    --tags=http-server

12. Exercises

  1. 1. What happens to the default External IP address of a VM if the instance is stopped and started again?
  1. 2. Explain the functional difference between an e2 instance and a c2 instance.

13. FAQs

Q: Do I need a complicated SSH Key like Putty to access my server? A: No! One of GCP's best features is "OS Login" and the "SSH-in-browser" button. Google securely manages the SSH keys in the background using your Google IAM identity, making terminal access seamless and secure.

14. Interview Questions

  • Q: Describe the architectural and financial differences between standard Compute Engine instances and Preemptible (Spot) instances. Give an example of a workload suited for a Preemptible instance.
  • Q: A junior developer launched a Compute Engine instance and installed Nginx. They can access the default webpage via curl localhost from inside the SSH terminal, but the public External IP returns a "Connection Timed Out" error in the browser. Detail your troubleshooting steps.

15. Summary

In Chapter 4, we provisioned our first raw infrastructure. We navigated the Compute Engine service, selecting optimal Machine Families and Boot Disks for our workload. We addressed network security by actively configuring firewalls to permit HTTP ingress. Finally, we utilized browser-based SSH to install an Apache web server, transforming an empty slice of Google's hardware into a publicly accessible asset on the internet.

16. Next Chapter Recommendation

Virtual Machines are great for running code, but they are a terrible, expensive place to store millions of user photos or videos. For massive data storage, we need a specialized service. Proceed to Chapter 5: Google Cloud Storage (GCS).

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·