The Challenge
Deploying virtual machines manually is prone to human error and difficult to scale. The goal of this project was to design a robust, self-healing cloud architecture that scales automatically based on traffic, and to automate its provisioning entirely through code.
The system needed to:
- Automatically scale instances up during high traffic and scale down to save costs.
- Distribute global incoming traffic across multiple instances using a Load Balancer.
- Ensure a secure networking topology with public and private subnets, utilizing Cloud NAT.
Architecture Overview
The solution leverages a modern multi-tier GCP networking topology, built strictly using Terraform declarative code.
Fig 1. GCP Architecture — Auto-Scaling Web Infrastructure
How It Works
Custom VPC & NAT
Terraform provisions a custom VPC with distinct subnets. A Cloud NAT gateway ensures private instances can securely download updates without public IP addresses.
Instance Templates
A Compute Engine Instance Template is defined to pull a startup script securely from Google Cloud Storage, installing Apache and serving the application automatically upon boot.
Managed Instance Group
An Auto-scaling MIG dynamically adds or removes Virtual Machines based on CPU utilization thresholds, ensuring high performance during traffic spikes.
Load Balancing
A Regional External HTTP Load Balancer with a proxy-only subnet routes incoming user requests to the healthiest instances across multiple zones.
Implementation
The entire infrastructure is defined as code. A single command handles the provisioning of the VPC, subnets, routers, firewalls, instance templates, and load balancers.
-
1. Initialize the Terraform backend and plugins:
terraform init
-
2. Review the infrastructure execution plan:
terraform plan
-
3. Deploy the full architecture to GCP:
terraform apply -auto-approve