This repository demonstrates how to use Terraform to automate the deployment of Google Cloud infrastructure. The project deploys an auto mode network with a firewall rule and two VM instances.
This project creates:
- One auto mode VPC network
- A firewall rule to allow HTTP, SSH, RDP, and ICMP traffic
- Two VM instances in different zones connected to the network
- Google Cloud Platform account
- Terraform (v1.5.7 or later)
- Google Cloud SDK
.
├── README.md
├── provider.tf # Google Cloud provider configuration
├── mynetwork.tf # Network and VM instance configurations
└── instance/ # VM instance module
├── main.tf # VM instance resource definition
└── variables.tf # Input variables for the module
git clone https://github.com/yourusername/terraform-gcp-infrastructure.git
cd terraform-gcp-infrastructure
terraform init
terraform plan
terraform apply
Confirm by typing yes
when prompted.
- In the Google Cloud console, navigate to VPC Network > VPC Networks to verify network creation
- Navigate to VPC Network > Firewall to verify firewall rule creation
- Navigate to Compute Engine > VM Instances to verify VM instances creation
- Test connectivity between instances using SSH and ping commands
To destroy all resources created by Terraform:
terraform destroy
Confirm by typing yes
when prompted.
The configuration creates an auto mode VPC network, which automatically creates a subnet in each region. The firewall rule allows HTTP (port 80), SSH (port 22), RDP (port 3389), and ICMP traffic.
The instance module is used to create VM instances with consistent configuration. The module takes the following input variables:
instance_name
: Name of the VM instanceinstance_zone
: Zone where the VM instance is createdinstance_type
: Machine type (defaults to e2-micro)instance_network
: Network to which the VM instance is connected
- Infrastructure as Code: Define infrastructure using code for improved consistency and reusability
- Modules: Reusable Terraform configurations for creating similar resources
- Resource Dependencies: Using references like
self_link
to create resources in the proper order
This project is licensed under the MIT License - see the LICENSE file for details.