A highly available, scalable, multi-region application infrastructure deployed on Google Cloud Platform using Terraform. This solution provides enterprise-grade resilience, security, and performance optimization across multiple cloud regions.
- Architecture Overview
- Components
- Architecture Diagrams
- Features
- Prerequisites
- Quick Start
- Module Structure
- Configuration
- Deployment
- Environment Management
- Security
- Monitoring
- Best Practices
- Troubleshooting
- Contributing
- License
This Terraform module deploys a robust multi-region application architecture on Google Cloud Platform, designed for high availability, scalability, and disaster recovery. The architecture spans multiple GCP regions with automatic failover capabilities.
- High Availability: Multi-region deployment with automatic failover
- Scalability: Auto-scaling instance groups and load balancing
- Security: Network isolation, encrypted communications, and secure secret management
- Observability: Comprehensive logging, monitoring, and alerting
- Infrastructure as Code: Fully automated deployment and management
- VPC Network: Custom regional VPC with isolated subnets
- Cloud NAT: Outbound internet access for private instances
- Cloud Router: Regional routing for NAT functionality
- Firewall Rules: Secure network access controls
- Instance Groups: Auto-scaling web and application servers
- Instance Templates: Standardized VM configurations
- Auto-scalers: Dynamic scaling based on metrics
- Health Checks: Application-level health monitoring
- Global HTTP(S) Load Balancer: Multi-region traffic distribution
- Backend Services: Instance group integration
- Cloud CDN: Content delivery optimization
- SSL Certificates: Automatic SSL/TLS management
- Cloud SQL: Regional PostgreSQL instances
- Read Replicas: Cross-region data replication
- Automated Backups: Point-in-time recovery capability
- Private IP: Database connectivity via VPC
- Cloud Storage: Multi-class storage buckets
- Versioning: Data protection and recovery
- Lifecycle Management: Automated data archival
- Access Controls: IAM-based permissions
- Secret Manager: Secure credential storage
- IAM: Fine-grained access controls
- Service Accounts: Application identity management
- Network Security: Private endpoints and firewall rules
graph TB
subgraph "Internet"
U[Users] --> GLB[Global Load Balancer]
end
subgraph "us-central1"
GLB --> FR1[Frontend Instances]
FR1 --> AR1[App Instances]
AR1 --> DB1[(Primary SQL)]
AR1 --> S1[Cloud Storage]
end
subgraph "us-west1"
GLB --> FR2[Frontend Instances]
FR2 --> AR2[App Instances]
AR2 --> DB2[(Replica SQL)]
AR2 --> S2[Cloud Storage]
end
subgraph "Security & Management"
DB1 & DB2 --> SM[Secret Manager]
AR1 & AR2 --> SM
AR1 & AR2 --> LOG[Cloud Logging]
AR1 & AR2 --> MON[Cloud Monitoring]
end
DB1 -.->|Replication| DB2
S1 -.->|Cross-region| S2
graph LR
subgraph "VPC Network"
subgraph "us-central1"
SN1[10.0.0.0/16]
NAT1[Cloud NAT]
RT1[Cloud Router]
end
subgraph "us-west1"
SN2[10.1.0.0/16]
NAT2[Cloud NAT]
RT2[Cloud Router]
end
subgraph "Firewall Rules"
FW1[Internal Communication]
FW2[HTTP/HTTPS Access]
FW3[SSH Access]
FW4[Health Check Access]
end
end
SN1 <--> NAT1
SN2 <--> NAT2
NAT1 --> RT1
NAT2 --> RT2
- Development: Single region, reduced resources
- Staging: Multi-region with limited redundancy
- Production: Full multi-region with high availability
- CPU utilization-based scaling
- Network traffic-based scaling
- Custom metric integration support
- Auto-healing for unhealthy instances
- VPC isolation with private endpoints
- Encrypted data at rest and in transit
- IAM-based access controls
- Automated secret rotation
- Network security with Cloud Armor
- Cloud Logging integration
- Cloud Monitoring dashboards
- Custom alerting policies
- Distributed tracing support
- Performance insights
- Terraform: v1.4.0 or higher
- Google Cloud SDK: Latest version
- GCP Project: With billing enabled
- Service Account: With appropriate permissions
- Domain Name: For SSL certificate (production environments)
gcloud services enable compute.googleapis.com
gcloud services enable iam.googleapis.com
gcloud services enable cloudresourcemanager.googleapis.com
gcloud services enable dns.googleapis.com
gcloud services enable sqladmin.googleapis.com
gcloud services enable storage-api.googleapis.com
gcloud services enable secretmanager.googleapis.com-
Clone the Repository
git clone https://github.com/your-org/gcp-multi-region-app.git cd gcp-multi-region-app -
Configure Backend
# Create a GCS bucket for Terraform state gsutil mb gs://your-terraform-state-bucket -
Initialize Terraform
cd environments/dev terraform init -
Plan Deployment
terraform plan -var-file="terraform.tfvars" -
Apply Configuration
terraform apply -var-file="terraform.tfvars"
gcp-multi-region-app/
βββ environments/ # Environment-specific configurations
β βββ dev/
β βββ staging/
β βββ prod/
βββ modules/ # Reusable Terraform modules
β βββ networking/
β βββ compute/
β βββ load-balancer/
β βββ database/
β βββ storage/
β βββ dns/
βββ docs/ # Additional documentation
βββ scripts/ # Utility scripts
βββ .gitignore
βββ LICENSE
βββ README.md
βββ main.tf
βββ variables.tf
βββ outputs.tf
βββ providers.tf
Create a terraform.tfvars file for each environment:
# terraform.tfvars
project_id = "your-gcp-project-id"
environment = "dev"
regions = ["us-central1", "us-west1"]
# Instance configuration
instance_group_configs = {
"web" = {
machine_type = "e2-standard-2"
instances_count = 2
disk_size = 20
disk_type = "pd-standard"
image_family = "ubuntu-2004-lts"
image_project = "ubuntu-os-cloud"
}
"app" = {
machine_type = "e2-standard-4"
instances_count = 2
disk_size = 50
disk_type = "pd-ssd"
image_family = "ubuntu-2004-lts"
image_project = "ubuntu-os-cloud"
}
}
# DNS configuration
domain_name = "your-domain.com"All resources support custom labeling:
labels = {
terraform = "true"
environment = "prod"
team = "platform"
cost-center = "infrastructure"
}cd environments/dev
terraform init
terraform plan -var-file="terraform.tfvars"
terraform apply -var-file="terraform.tfvars"cd environments/staging
terraform init
terraform plan -var-file="terraform.tfvars"
terraform apply -var-file="terraform.tfvars"cd environments/prod
terraform init
terraform plan -var-file="terraform.tfvars"
terraform apply -var-file="terraform.tfvars"Each environment maintains separate Terraform state:
terraform {
backend "gcs" {
bucket = "your-terraform-state-bucket"
prefix = "environments/prod"
}
}To promote changes from staging to production:
- Test in staging environment
- Review and validate changes
- Apply to production with appropriate approval
- Private subnets with Cloud NAT
- Restricted firewall rules
- VPC flow logs enabled
- Private Google Access enabled
- Cloud SQL with private IP
- Encrypted storage buckets
- Secret Manager for credentials
- IAM least-privilege access
- Service account per application
- Network policies enforcement
- SSL/TLS termination at load balancer
- Cloud Armor integration ready
- Cloud Monitoring integration
- Custom dashboards
- Alerting policies
- Health check monitoring
# View application logs
gcloud logging read "resource.type=gce_instance" --format=json
# Set up log-based metrics
gcloud logging metrics create app-error-rate \
--filter='severity>=ERROR' \
--description='Application error rate'- Use consistent naming conventions
- Include environment in resource names
- Use hyphens for multi-word names
- Use separate state files per environment
- Enable versioning on state buckets
- Implement state locking
- Never commit secrets to version control
- Use IAM roles instead of keys when possible
- Regularly rotate credentials
- Use auto-scaling to match demand
- Implement lifecycle policies for storage
- Right-size instances for workload
-
State Lock Issues
terraform force-unlock <LOCK_ID>
-
API Quota Errors
gcloud compute project-info describe --project=<PROJECT>
-
Network Connectivity
gcloud compute ssh <INSTANCE> -- -vvv
# Check resource status
terraform show
# View detailed logs
export TF_LOG=DEBUG
terraform apply
# Validate configuration
terraform validate- Create feature branch
- Make changes
- Add tests
- Submit pull request
- Use consistent formatting (
terraform fmt) - Add comments for complex logic
- Follow Terraform best practices
- Update documentation
This project is licensed under the Apache License 2.0 - see the LICENSE file for details
