Managing cloud resources manually can be tedious, error-prone, and time-consuming. Infrastructure-as-Code (IaC) tools like Terraform make it easier to define, provision, and manage cloud infrastructure. In this guide, we'll use Terraform to deploy a Virtual Private Cloud (VPC), a Subnet, a Firewall rule, and a Compute Instance on Google Cloud Platform (GCP).
Prerequisites
Before we start, ensure you have:
Google Cloud Account with project setup.
Terraform installed on your local machine. Download Terraform.
GCP Service Account JSON Key with appropriate permissions (e.g., Owner or specific permissions for the resources).
Google Cloud SDK (gcloud) installed for authentication.
Step 1: Authenticate Terraform with GCP
Download the Service Account JSON file from GCP.
Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the JSON file path:
Step 2: Create the Terraform Configuration File
Create a directory for your Terraform project:
-Run the command "terraform init" to initialize the directory
Step 3: Create VPC Network Module
Create a vpc tf file
-Run the command terraform validate to make sure every vpc network configuration is correct
- Run the command "terraform plan"
- Run the command "terraform deploy --auto-approve"
Step 4: Create the Subnet Module
Create a subnet tf file:
- Run the command "terraform deploy --auto-approve"
Step 4: Create the Firewall Module
Create a firewall tf file:
- Run the command "terraform deploy --auto-approve"
Step 4: Create the Compute Instance Module
Create a compute tf file:
-Run the command terraform validate to make sure every computer instance configuration is correct
- Run the command "terraform plan"
- Run the command "terraform deploy --auto-approve"
Step 5: Verify the Resources
Log in to the Google Cloud Console.
Navigate to VPC networks, Compute Engine, and Firewall rules to confirm the resources are created.
Step 6: Clean Up Resources
When you're done, destroy the resources to avoid unnecessary charges:
- Run the command "terraform destroy --auto-approve
Code Explanation
VPC: Creates an isolated network (google_compute_network) for your resources.
Subnet: Reserves an IP range within the VPC using a CIDR block (google_compute_subnetwork).
Firewall: Opens port 22 to allow SSH access (google_compute_firewall).
Compute Instance: Deploys a virtual machine (google_compute_instance) with an external IP for access.
Why Terraform?
Terraform is declarative, meaning you define what you want to achieve, and Terraform figures out how to make it happen. It supports multi-cloud environments and tracks changes with state files, making infrastructure management simple and efficient.
Benefits of Using Modules
Reusability: Write once and reuse across multiple projects.
Clarity: Keep your root configuration clean and organized.
Scalability: Manage complex deployments with modular components.
This modular setup is production-ready and ensures scalability for future infrastructure growth. Let me know if you'd like help refining the documentation further! 🚀
Top comments (0)