This template creates an infrastructure on Google Cloud Platform (GCP) including a custom VPC, subnets, and firewall rules.
The template creates:
- Custom VPC: A new VPC network with custom subnets
- Two Subnets:
dev-subnet-1: Created in the custom VPCdev-subnet-2: Created in the default VPC
- Firewall Rules:
- Internal traffic rules for the custom VPC
- SSH access rule for instances with
ssh-allowedtag
- Google Cloud SDK: Install and configure the Google Cloud SDK
- Terraform: Install Terraform version >= 1.12.0
- GCP Project: Have a GCP project with billing enabled
- Authentication: Set up authentication using one of these methods:
- Service Account Key
- Application Default Credentials (ADC)
- gcloud auth application-default login
# Navigate to your project directory
cd your-terraform-project
# Update the terraform.tfvars file with your project IDEdit terraform.tfvars to match your requirements:
project_id = "your-actual-gcp-project-id"# Initialize Terraform
terraform init
# Review the planned changes
terraform plan
# Apply the configuration
terraform applyWhen prompted, type yes to confirm the deployment.
| Variable | Type | Default | Description |
|---|---|---|---|
project_id |
string | Required | Your GCP project ID |
region |
string | europe-west3 |
GCP region for resources |
zone |
string | europe-west3-a |
GCP zone for resources |
vpc_cidr_block |
string | 10.0.0.0/16 |
CIDR block for the custom VPC |
environment |
string | dev |
Environment name (used in resource naming) |
subnet_configs |
list(object) | See below | Subnet configurations |
subnet_configs = [
{
cidr_block = "10.0.1.0/24"
name = "dev-subnet-1"
region = "europe-west3"
},
{
cidr_block = "10.0.2.0/24"
name = "dev-subnet-2"
region = "europe-west3"
}
]Create a custom terraform.tfvars file or override variables:
project_id = "my-project-123"
region = "us-central1"
zone = "us-central1-a"
environment = "production"
subnet_configs = [
{
cidr_block = "10.1.1.0/24"
name = "prod-subnet-1"
region = "us-central1"
},
{
cidr_block = "10.1.2.0/24"
name = "prod-subnet-2"
region = "us-central1"
}
]After deployment, the following outputs will be available:
dev_vpc_id: ID of the created custom VPCdev_vpc_name: Name of the created custom VPCdev_subnet_1_id: ID of the first subnet (in custom VPC)dev_subnet_1_name: Name of the first subnetdev_subnet_2_id: ID of the second subnet (in default VPC)default_vpc_id: ID of the default VPC
View outputs with:
terraform outputThe template creates these firewall rules:
- Internal Traffic: Allows all internal communication within the custom VPC (
10.0.0.0/16) - SSH Access: Allows SSH (port 22) from anywhere (
0.0.0.0/0) to instances tagged withssh-allowed
- Restricting source ranges to specific IP addresses or ranges
- Using IAP (Identity-Aware Proxy) for secure access
- Implementing additional network security measures
For production deployments, consider modifying:
# Restrict SSH access to specific IP ranges
source_ranges = ["YOUR_OFFICE_IP/32", "YOUR_VPN_RANGE/24"]
# Or use IAP
source_ranges = ["35.235.240.0/20"] # IAP IP range.
├── provider.tf # Terraform and provider configuration
├── variables.tf # Variable definitions
├── terraform.tfvars # Variable values (customize this)
├── resources.tf # Main infrastructure resources
├── outputs.tf # Output definitions
└── README.md # This file
terraform show# After making changes to .tf files
terraform plan
terraform applyterraform destroyterraform fmtterraform validate-
Authentication Errors
# Set up application default credentials gcloud auth application-default login -
Project Not Found
- Verify your project ID in
terraform.tfvars - Ensure you have the necessary permissions
- Verify your project ID in
-
API Not Enabled
# Enable required APIs gcloud services enable compute.googleapis.com
-
Quota Exceeded
- Check your project quotas in the GCP Console
- Request quota increases if needed
- Check the Terraform GCP Provider Documentation
- Review GCP VPC Documentation
- Use
terraform planto preview changes before applying
After deploying this template, you might want to:
- Add Compute Instances: Create VM instances in the subnets
- Set up Load Balancers: Add load balancing for your applications
- Configure DNS: Set up Cloud DNS for name resolution
- Add Monitoring: Implement logging and monitoring solutions
- Enhance Security: Add more specific firewall rules and security policies
When modifying this template:
- Run
terraform fmtto format code - Run
terraform validateto check syntax - Test changes in a development environment first
- Update this README if adding new features or variables