A streamlined Terraform configuration for deploying Google Kubernetes Engine (GKE) Autopilot clusters with cost optimization and best practices.
- Simplified Architecture: Clean, minimal configuration focused on essential features
- GKE Autopilot: Fully managed Kubernetes with automatic scaling and cost optimization
- Cost-Optimized: Minimal logging/monitoring components and efficient resource usage
- Easy Deployment: Single command deployment with sensible defaults
- Workload Identity: Secure, keyless access to Google APIs
- Sample Application: Includes a Go web app for testing deployment
.
├── main.tf # Root Terraform configuration
├── variables.tf # Configuration variables
├── outputs.tf # Cluster outputs
├── modules/
│ └── gke-autopilot/ # GKE Autopilot module
│ ├── main.tf
│ ├── variables.tf
│ ├── outputs.tf
│ └── versions.tf
├── app/ # Sample Go web application
│ ├── main.go
│ ├── Dockerfile
│ └── README.md
└── k8s/ # Kubernetes manifests
└── deployment.yaml
- Google Cloud SDK installed and authenticated
- Terraform >= 1.0
- kubectl for cluster management
-
Clone and navigate to the repository:
git clone <repository-url> cd terraform-gke-autopilot
-
Configure your GCP project (optional):
gcloud config set project YOUR_PROJECT_ID gcloud config set compute/region YOUR_REGION
-
Deploy with Terraform:
terraform init terraform plan terraform apply
-
Configure kubectl:
gcloud container clusters get-credentials <cluster-name> --region <region>
-
Build and push the container image:
cd app gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/demo-go-app:latest .
-
Deploy to Kubernetes:
kubectl apply -f ../k8s/deployment.yaml
-
Get the external IP:
kubectl get service demo-go-app-service
| Variable | Description | Default |
|---|---|---|
cluster_name |
Name of the GKE cluster | "autopilot-cluster" |
region |
GCP region for deployment | "" (uses gcloud config) |
project_id |
GCP project ID | "" (uses gcloud config) |
environment |
Environment label | "dev" |
deletion_protection |
Enable deletion protection | false |
Create a terraform.tfvars file:
cluster_name = "my-autopilot-cluster"
environment = "production"
region = "us-central1"- Autopilot Mode: Pay only for running workloads, not idle nodes
- Minimal Logging: Only system components logging enabled by default
- Efficient Monitoring: Basic monitoring configuration to reduce costs
- Regional Deployment: Required for Autopilot, provides high availability
- Workload Identity: Secure service-to-service authentication
- Latest Kubernetes: STABLE release channel for security updates
- Resource Labels: Proper tagging for governance and cost tracking
- Automatic Updates: Managed by GKE Autopilot
- Maintenance Window: Configured for off-hours (2-6 AM UTC)
- No Node Management: Autopilot handles all node operations
To destroy all resources:
terraform destroyThis is a simplified, production-ready configuration suitable for:
- Development environments
- Small to medium production workloads
- Teams wanting minimal operational overhead
- Cost-conscious deployments
For advanced features like private clusters, custom networking, or enterprise security, consider extending the module configuration.