This repository demonstrates how to manage Google Cloud Platform (GCP) Compute Engine instances using Terraform. It provides a structured approach for setting up a GCP project, configuring Terraform, and deploying infrastructure resources efficiently.
- β Prerequisites
- π Project Structure
- βοΈ Setting Up GCP Project
- π§ Setting Up Terraform
- π οΈ Terraform Configuration
- π Running Terraform
- ποΈ Cleaning Up
- π Additional Resources
Before you begin, ensure that you have the following prerequisites:
- π₯οΈ Google Cloud Platform (GCP) Account: You will need an active Google Cloud account.
- π¦ Terraform Installed: Install Terraform on your local machine. If not already installed, follow this guide to set up Terraform.
- π οΈ gcloud CLI Installed: The
gcloud
command-line interface is required for authentication. You can install it following this guide. - βοΈ A Google Cloud Project: Create a GCP project if you don't have one. You can do so in the Google Cloud Console.
This repository follows a modular approach to organizing Terraform configurations:
.terraform
βββ modules
β βββ compute
β β βββ main.tf
β β βββ outputs.tf
β β βββ variables.tf
βββ .gitignore
βββ .terraform.lock.hcl
βββ gcp_creds.json
βββ main.tf
βββ outputs.tf
βββ providers.tf
βββ README.md
βββ terraform.tfstate
βββ terraform.tfstate.backup
βββ terraform.tfvars
βββ variables.tf
- π modules/compute/: Contains modular Terraform configurations for Compute Engine instances.
- π main.tf: The primary Terraform configuration file.
- π§ variables.tf: Defines reusable Terraform variables.
- π€ outputs.tf: Defines the Terraform output values.
- π οΈ providers.tf: Configures the provider settings for Terraform.
- π terraform.tfvars: Stores variable values for Terraform execution.
- π gcp_creds.json: The service account key file for GCP authentication (not to be committed).
- π Create a Google Cloud Project:
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Note your project ID for Terraform configuration.
- π Enable Compute Engine API:
- Navigate to APIs & Services > Library.
- Search for Compute Engine API and click Enable.
- π€ Create a Service Account:
- Navigate to IAM & Admin > Service Accounts.
- Click Create Service Account, assign a role (
Compute Admin
orProject Owner
). - Generate a JSON key and download it securely.
-
π Set Environment Variables:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-keyfile.json"
Replace
/path/to/your-keyfile.json
with the actual path.
-
β¬οΈ Install Terraform: Follow the Terraform installation guide.
-
π οΈ Initialize Terraform:
terraform init
Below shows some example ways of configuring , for in detail approach refer the source code of this repo
-
π Define Provider Configuration (
providers.tf
):provider "google" { project = var.project_id region = var.region credentials = file(var.credentials_file) }
-
βοΈ Define Compute Engine Resource (
main.tf
):resource "google_compute_instance" "vm_instance" { name = "terraform-vm" machine_type = "e2-micro" zone = "us-central1-a" boot_disk { initialize_params { image = "debian-cloud/debian-11" } } network_interface { network = "default" access_config {} } }
-
π Define Variables (
variables.tf
):variable "project_id" { description = "GCP Project ID" type = string } variable "region" { description = "GCP Region" type = string default = "us-central1" } variable "credentials_file" { description = "Path to service account JSON key file" type = string }
-
π€ Define Outputs (
outputs.tf
):output "instance_name" { value = google_compute_instance.vm_instance.name }
-
π Plan the Deployment:
terraform plan
-
β‘ Apply the Configuration:
terraform apply
Type
yes
when prompted. -
π Verify the Deployment:
- Navigate to Compute Engine > VM instances in the GCP Console.
To remove all resources:
terraform destroy
Confirm by typing yes
.
- π Terraform Docs
- ποΈ Google Cloud Terraform Provider
- π Google Cloud Console
This project provides an efficient way to manage GCP Compute Engine instances using Terraform, following best practices for modular configuration and automation. π