A comprehensive Infrastructure as Code (IaC) project demonstrating multi-environment deployment on Google Cloud Platform using Terraform. This project showcases best practices for managing cloud infrastructure across development, staging, and production environments.
This project implements a modular Terraform infrastructure with the following components:
- Networking: VPC and subnets for network isolation
- Compute: Virtual machine instances for application hosting
- Storage: Google Cloud Storage buckets for data persistence
- IAM: Identity and Access Management for secure resource access
- CI/CD: Cloud Build integration for automated deployments
Detailed architecture diagram showing the complete infrastructure setup across all environments
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Google Cloud Platform β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Dev Env β β Staging Env β β Prod Env β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β VPC β β Compute β β Storage β β
β β Subnets β β Instances β β Buckets β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β IAM β β Cloud Build β β Terraform β β
β β Roles β β Pipeline β β Backend β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
This repository uses a multi-branch strategy for environment-specific deployments:
- Purpose: Project documentation and showcase
- Contents: README, architecture diagrams, screenshots
- Use Case: Project overview and documentation
dev
β Development environment infrastructurestaging
β Staging environment infrastructureprod
β Production environment infrastructure
iac-with-terraform-gcp/
βββ π architecture/ # Architecture diagrams and documentation
βββ π screenshots/ # Project documentation screenshots
βββ README.md # Project documentation (this file)
βββ π [Environment Branches] # Infrastructure code per environment
βββ π modules/ # Reusable Terraform modules
β βββ π networking/ # VPC and subnet configuration
β βββ π compute/ # VM instance management
β βββ π storage/ # GCS bucket configuration
β βββ π iam/ # IAM roles and permissions
βββ π envs/ # Environment-specific configurations
β βββ π dev/ # Development environment
β βββ π staging/ # Staging environment
β βββ π prod/ # Production environment
βββ main.tf # Main Terraform configuration
βββ variables.tf # Variable definitions
βββ outputs.tf # Output definitions
βββ providers.tf # Provider configuration
βββ backend.tf # Backend configuration
βββ cloudbuild.yaml # CI/CD pipeline configuration
βββ terraform.tfvars # Default variable values
To work with the actual infrastructure code, switch to the appropriate environment branch:
# For Development Environment
git checkout dev
# For Staging Environment
git checkout staging
# For Production Environment
git checkout prod
-
Clone the repository:
git clone <your-repository-url> cd iac-with-terraform-gcp
-
Switch to your target environment:
git checkout dev # or staging, or prod
-
Follow the environment-specific README in each branch for detailed setup instructions.
Before you begin, ensure you have the following installed:
- Terraform (>= 1.8.0)
- Google Cloud SDK
- Git
Your Google Cloud account needs the following roles:
- Project Owner or Editor
- Cloud Build Service Account
- Storage Admin (for backend configuration)
The infrastructure is organized into reusable modules:
resource "google_compute_network" "vpc" {
name = "vpc-${var.environment}"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnet" {
name = "vpc-subnet-${var.environment}"
ip_cidr_range = "10.10.0.0/24"
region = var.region
network = google_compute_network.vpc.id
}
resource "google_compute_instance" "vm_instance" {
name = var.instance_name
machine_type = "e2-medium"
zone = var.zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-12"
}
}
network_interface {
network = "default"
access_config {}
}
}
resource "google_storage_bucket" "bucket" {
name = var.bucket_name
location = "ASIA"
force_destroy = true
}
resource "google_project_iam_member" "viewer" {
project = var.project_id
role = "roles/viewer"
member = "serviceAccount:${var.project_id}@appspot.gserviceaccount.com"
}
Automated deployment using Google Cloud Build:
steps:
- name: 'hashicorp/terraform:1.8.0'
entrypoint: sh
args:
- -c
- |
terraform init -backend-config="envs/${_ENV}/backend.config" -reconfigure
terraform workspace select ${_ENV} || terraform workspace new ${_ENV}
terraform apply \
-lock-timeout=60s \
-auto-approve \
-var-file="envs/${_ENV}/terraform.tfvars"
Each environment has its own configuration:
- Region: asia-south1
- Zone: asia-south1-a
- Instance: dev-instance
- Bucket: dev-system-state
- Region: asia-south1
- Zone: asia-south1-b
- Instance: staging-instance
- Bucket: staging-system-state
- Region: asia-south1
- Zone: asia-south1-c
- Instance: prod-instance
- Bucket: prod-system-state
This project demonstrates a complete Infrastructure as Code workflow with comprehensive documentation. Below are detailed screenshots showcasing each step of the implementation process:
- What it shows: Setting up authentication between GitHub and Google Cloud Platform
- Key Learning: Proper authentication setup is crucial for secure CI/CD pipelines
- Methodology: Demonstrates the importance of service account configuration and OAuth setup
- What it shows: Configuration of environment-specific variables and backend settings
- Key Learning: Environment isolation and configuration management best practices
- Methodology: Shows how to structure Terraform configurations for multiple environments with proper separation of concerns
- What it shows: Initializing Terraform with backend configuration and workspace setup
- Key Learning: Remote state management and workspace isolation techniques
- Methodology: Demonstrates proper Terraform initialization workflow with backend configuration
- What it shows: Development environment deployment process
- Key Learning: Environment-specific deployment strategies and resource provisioning
- Methodology: Shows the development environment setup with proper resource naming and configuration
- What it shows: Terraform plan and apply process for infrastructure deployment
- Key Learning: Infrastructure provisioning workflow and change management
- Methodology: Demonstrates the complete IaC deployment process with proper planning and execution
- What it shows: Verification of VPC and subnet creation in Google Cloud Console
- Key Learning: Network infrastructure validation and monitoring
- Methodology: Shows how to verify that networking components are properly deployed and configured
- What it shows: IAM role verification and permission management
- Key Learning: Security best practices and access control validation
- Methodology: Demonstrates proper IAM configuration verification and security auditing
- What it shows: Storage bucket creation and configuration verification
- Key Learning: Storage infrastructure validation and data persistence setup
- Methodology: Shows how to verify storage components and ensure proper data management
- What it shows: Complete infrastructure deployment status and resource overview
- Key Learning: End-to-end infrastructure validation and resource management
- Methodology: Demonstrates comprehensive infrastructure verification and deployment success
- What it shows: CI/CD pipeline monitoring and build status tracking
- Key Learning: Automated deployment monitoring and pipeline management
- Methodology: Shows how to monitor and manage automated deployment pipelines
After deployment, verify your infrastructure:
gcloud compute networks list
gcloud compute networks subnets list
gcloud compute instances list
gsutil ls
gcloud projects get-iam-policy YOUR_PROJECT_ID
To destroy the infrastructure:
# For specific environment
terraform workspace select <environment>
terraform destroy -var-file="envs/<environment>/terraform.tfvars"
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the Issues page
- Review the documentation
- Create a new issue with detailed information
- Terraform Documentation
- Google Cloud Terraform Provider
- Google Cloud Build Documentation
- Terraform Best Practices
Note: This main branch contains project documentation and showcase materials. For actual infrastructure deployment, please switch to the appropriate environment branch (dev, staging, or prod).