Overview and What You Will Learn
Choose Compute Engine when you need full control of the operating system, GKE when you run many coordinated containerized services, Cloud Run for a stateless container or web service, and Cloud Functions for small event-driven pieces of logic. Each step down that list trades control for less operational work.
In this lab, you will:
- Run the same simple web service on a Compute Engine VM and on Cloud Run
- Deploy a small event-style function with Cloud Functions
- Compare how much operational work each option left you with
Why This Matters in Production
A team building an internal reporting tool puts it on a VM they must now patch, monitor, and secure forever, when Cloud Run would have run it with no servers to manage and scaled to zero overnight. Spotify and Shopify-scale teams run all four models side by side, and the skill is matching each workload to the lightest option that still fits. This is also one of the most heavily tested decisions on the Associate Cloud Engineer exam.
Core Principles
The right service depends on what the workload actually needs, not on which one feels more serious.
or kernel access?} A -->|Yes| CE[Compute Engine] A -->|No| B{Many interdependent
containers?} B -->|Yes| GKE[GKE] B -->|No| C{Single-purpose
event handler?} C -->|Yes| CF[Cloud Functions] C -->|No| CR[Cloud Run] classDef start fill:#475569,color:#FFFFFF classDef decision fill:#F59E0B,color:#111827 classDef svc fill:#326CE5,color:#FFFFFF class S start class A,B,C decision class CE,GKE,CF,CR svc
| Service | You manage | Scales to zero |
|---|---|---|
| Compute Engine | OS, runtime, patching | No |
| GKE | Workloads (Autopilot manages nodes) | Workloads can, cluster fee remains |
| Cloud Run and Functions | Code or container only | Yes |
Cloud Functions is now delivered as Cloud Run functions, so it shares Cloud Run's infrastructure and scaling model.
Detailed Step-by-Step Practical Lab
Use a sandbox project with billing enabled. Replace mumbai-prod-compute-lab with your own project ID.
- Set the project and enable the required APIs:
gcloud config set project mumbai-prod-compute-labgcloud services enable compute.googleapis.com run.googleapis.com \ cloudfunctions.googleapis.com cloudbuild.googleapis.com \ artifactregistry.googleapis.com- Run a web service on Compute Engine. A startup script installs nginx, but patching and monitoring remain your job from here:
gcloud compute instances create vm-reporting-tool \ --zone=asia-south1-a \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-cloud \ --tags=http-server \ --metadata=startup-script='#! /bin/bashapt-get update && apt-get install -y nginxecho "reporting-tool on a VM" > /var/www/html/index.html'- Run the same kind of service on Cloud Run using Google's public sample container:
gcloud run deploy reporting-tool \ --image=us-docker.pkg.dev/cloudrun/container/hello \ --region=asia-south1 \ --allow-unauthenticatedThe command prints a https://...run.app URL. There was no OS, no SSH, and no patching.
- Check how Cloud Run scales, then confirm the minimum instance count is zero:
gcloud run services describe reporting-tool --region=asia-south1 \ --format="value(status.url,spec.template.metadata.annotations)"- Create a small event-style function. Save this as
main.pyin an empty folder:
import functions_framework def resize_notice(request): # Stand-in for work triggered by an event, such as a file upload return "image queued for resize", 200- Deploy it as a second-generation function:
gcloud functions deploy resize-notice \ --gen2 --runtime=python312 --region=asia-south1 \ --source=. --entry-point=resize_notice \ --trigger-http --allow-unauthenticated- Consider when the picture changes. If the tool became a frontend, an API, a worker, a cache, and a queue processor that must scale and discover each other, that coordination cost is what justifies GKE. For a first experiment, use Autopilot so Google manages the nodes:
gcloud container clusters create-auto mumbai-prod-cluster --region=asia-south1- Clean up:
gcloud functions delete resize-notice --gen2 --region=asia-south1 --quietgcloud run services delete reporting-tool --region=asia-south1 --quietgcloud compute instances delete vm-reporting-tool --zone=asia-south1-a --quiet# If you created the cluster:# gcloud container clusters delete mumbai-prod-cluster --region=asia-south1 --quietProduction Best Practices & Common Pitfalls
Common MistakeChoosing GKE by default because it feels more scalable. A single stateless service then carries cluster upgrades, node sizing, and Kubernetes networking, all of which Cloud Run would have handled for you.
TipStart with Cloud Run for any stateless containerized service, and move to Compute Engine or GKE only when you can name the specific requirement that forces it.
- Use Compute Engine for genuine OS-level needs. Examples are legacy software tied to a specific OS, custom kernel modules, GPUs with special drivers, or licensed appliances.
- Use GKE for multi-service platforms. Service meshes, custom controllers, and stateful sets are where Kubernetes pays for its complexity.
- Use Cloud Functions for narrow, event-triggered logic. A full application with many routes fits Cloud Run better.
- Watch cost on always-on options. A running VM or a GKE cluster bills whether or not traffic arrives, while Cloud Run bills per request and active instance time.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute instances create |
Create a Compute Engine VM |
gcloud run deploy |
Deploy a container to Cloud Run |
gcloud container clusters create-auto |
Create a GKE Autopilot cluster |
gcloud functions deploy --gen2 |
Deploy a Cloud Run function |
gcloud run services logs read reporting-tool --region=asia-south1 |
Read Cloud Run service logs |