Skip to main content

Overview

We provide 6 production-ready Terraform modules for deploying complete GCP infrastructure with best practices built-in. These modules enable infrastructure-as-code deployments with high availability, security, and cost optimization.

6 Modules

VPC, GKE, Cloud SQL, Redis, Workload Identity, Backend

3 Environments

Dev, Staging, Production configurations

9,000+ Lines

Production-grade infrastructure code

Module Architecture


Quick Start

1. Setup Terraform Backend

This creates a GCS bucket for Terraform state with versioning, encryption, and logging. See Backend Setup Guide for details.

2. Deploy Environment

Duration: ~25 minutes Cost: $880-1,275/month

Module Reference

1. GCP VPC Module

Location: terraform/modules/gcp-vpc/ Creates VPC network optimized for GKE with:
  • VPC-native networking (secondary IP ranges for pods/services)
  • Cloud NAT with dynamic port allocation
  • Cloud Router with BGP
  • Firewall rules (internal, IAP SSH, health checks)
  • Private Service Connection for Cloud SQL/Memorystore
  • VPC Flow Logs with sampling
  • Optional Cloud Armor for DDoS protection
Outputs: Network ID, subnet names, NAT IPs, firewall rule IDs

Complete VPC Module Documentation

1,088-line README with examples, best practices, and troubleshooting

2. GKE Autopilot Module

Location: terraform/modules/gke-autopilot/ Creates fully managed GKE Autopilot cluster with:
  • Regional or zonal deployment
  • Workload Identity enabled by default
  • Binary Authorization support
  • GKE Security Posture Dashboard
  • Dataplane V2 (eBPF networking)
  • Network Policy enforcement
  • Managed Prometheus
  • Automated backups (GKE Backup)
  • Fleet registration (Anthos)
Key Features:
  • Pay-per-pod pricing: 40-60% cost savings vs. Standard GKE
  • Fully managed: Google handles nodes, upgrades, scaling
  • Security hardened: CIS benchmark compliance out-of-box
  • Auto-scaling: VPA, HPA, cluster autoscaler included

Complete GKE Module Documentation

900-line README with Autopilot vs. Standard comparison, best practices, examples

3. Cloud SQL PostgreSQL Module

Location: terraform/modules/cloudsql/ Creates highly available PostgreSQL database with:
  • Regional HA with automatic failover
  • Automated backups + point-in-time recovery (PITR)
  • Read replicas (cross-region support)
  • Query Insights (Performance Insights)
  • Private IP only (VPC integration)
  • Customer-managed encryption (CMEK) support
  • Pre-configured monitoring alerts
Supports PostgreSQL: 11, 12, 13, 14, 15, 16

4. Memorystore Redis Module

Location: terraform/modules/memorystore/ Creates highly available Redis cache with:
  • STANDARD_HA tier with automatic failover
  • Read replicas (up to 5)
  • RDB or AOF persistence
  • TLS encryption in transit
  • Redis AUTH password authentication
  • Cross-region replicas for DR
Memory Sizes: 1-300 GB Redis Versions: 4.0, 5.0, 6.X, 7.0, 7.2

5. Workload Identity Module

Location: terraform/modules/gke-workload-identity/ Creates IAM bindings for Kubernetes pods to use GCP service accounts securely (no credential files needed).
Features:
  • Replaces service account key files (more secure)
  • Pod-level IAM permissions
  • Resource-specific access control
  • Auto-generated Kubernetes SA manifests

6. Backend Setup Module

Location: terraform/backend-setup-gcp/ Creates GCS bucket for Terraform state management with:
  • Versioning enabled (10 versions retained)
  • Access logging to separate bucket
  • Encryption at rest
  • Lifecycle policies
  • Public access prevention
One-time setup per GCP project. Provides centralized state management with locking.

Module Composition Example

Here’s how modules work together in a complete environment:

Environment Configurations

Development (Cost-Optimized)

Location: terraform/environments/gcp-dev/

Configuration

  • Cluster: Zonal (single zone)
  • Cloud SQL: db-custom-1-3840, no HA
  • Redis: 1 GB BASIC tier
  • NAT: Auto-allocated IPs
  • Flow Logs: 10% sampling

Cost

$100-150/month50% cheaper than staging
Use case: Feature development, testing, experimentation

Staging (Production-Like)

Location: terraform/environments/gcp-staging/

Configuration

  • Cluster: Regional (3 zones)
  • Cloud SQL: db-custom-2-7680, HA
  • Redis: 3 GB STANDARD_HA
  • NAT: Auto-allocated IPs
  • Monitoring: Full alerts

Cost

$250-350/monthProduction-like for pre-release testing
Use case: QA, integration testing, pre-production validation

Production (Enterprise-Grade)

Location: terraform/environments/gcp-prod/

Configuration

  • Cluster: Regional (3 zones), deletion protection
  • Cloud SQL: db-custom-4-15360, HA + read replica
  • Redis: 5 GB STANDARD_HA + replicas
  • NAT: 2 static IPs (whitelisting)
  • Binary Auth: Ready to enable
  • Backups: Daily (30-day retention)

Cost

$880-1,275/monthEnterprise HA with full observabilityWith 3-year CUD: $466/month (66% savings)
Use case: Production traffic, customer-facing services
  • 99.95% SLA from GKE regional cluster
  • Automatic failover for Cloud SQL (2-3 min RTO)
  • Automatic failover for Redis (1-2 min RTO)
  • 3 pod replicas minimum (Pod Disruption Budget)
  • Topology spread across zones
  • Automated backups with 7-day PITR

Module Features

Variable Validation

All modules include comprehensive input validation:

Detailed Outputs

Every module exports useful outputs for module composition:

Built-in Monitoring

Modules include pre-configured Cloud Monitoring alerts:
  • GKE: Cluster upgrade available, high CPU
  • Cloud SQL: High CPU/memory/disk usage
  • Redis: High memory, high connections, instance down

Security by Default

All modules implement security best practices:
  • Private IPs only (no public access)
  • Encryption at rest and in transit
  • Customer-managed encryption key (CMEK) support
  • Network policies and firewall rules
  • Workload Identity integration

Best Practices

1. Use Modules, Don’t Fork

Don’t copy module code. Reference modules from a central location and customize via variables.

2. Pin Module Versions

For stability, consider pinning to specific versions:

3. Use terraform.tfvars for Secrets

Never commit sensitive values to Git:

4. Remote State for Teams

Use GCS backend for collaboration:

5. Module Dependencies

Use depends_on when modules have dependencies:

Common Tasks

View Outputs

Update Infrastructure

Import Existing Resources

Destroy Environment

Caution: This destroys all infrastructure. Use only for dev/test environments.

Troubleshooting

Issue: GCP project quota limits exceededSolution:
Common quotas to increase:
  • CPUs: 100+
  • In-use IP addresses: 50+
  • Persistent disk SSD: 1000 GB+
Issue: Required API not enabledSolution:
Issue: Terraform state lockedSolution: GCS provides automatic state locking. If stuck:
Issue: Module not found errorSolution: Ensure module paths are correct. Modules use relative paths:

Module Development

Want to create custom modules or extend existing ones?

Module Structure

Module Template


Backend Setup

Terraform state backend configuration

Multi-Environment Strategy

Dev, staging, production workflows

GKE Production Deployment

Complete production deployment guide

Operations Runbooks

Day-2 operations and troubleshooting

Additional Resources

Comprehensive Module Documentation

Each module includes a detailed README (500-1,000+ lines) with:
  • Complete variable reference
  • Output documentation
  • Usage examples
  • Best practices
  • Troubleshooting guides
See: terraform/modules/
For questions or issues, consult the module READMEs or open an issue.