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 (likemysite.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. In the GCP Console, navigate to Compute Engine > VM instances.
- 2. Click Create Instance.
-
3.
Name:
my-first-webserver
-
4.
Region/Zone: Choose a region close to you (e.g.,
us-central1-a).
- 5. Machine configuration: Choose General-purpose, Series E2, Machine type e2-micro (This is free tier eligible!).
- 6. Boot disk: Leave it as the default (usually Debian Linux).
- 7. Firewall: CRITICAL STEP. Check the box that says "Allow HTTP traffic". If you forget this, your website will be invisible.
- 8. Click Create. Wait 30 seconds for the green checkmark to appear.
- 9. Click the SSH button next to your VM. A terminal will magically open in your browser!
- 10. In the terminal, install an Apache web server:
bash
- 11. Go back to the GCP Console. Find the External IP of your VM.
- 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 500c2-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 installvia 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
12. Exercises
- 1. What happens to the default External IP address of a VM if the instance is stopped and started again?
-
2.
Explain the functional difference between an
e2instance and ac2instance.
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 localhostfrom inside the SSH terminal, but the public External IP returns a "Connection Timed Out" error in the browser. Detail your troubleshooting steps.