Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Terraform GCP VM Module

This repository provisions a VM instance on Google Cloud Platform using Terraform.

📁 Structure

terraform-gcp-vm/
├── main.tf
├── variables.tf
├── modules/
│   └── gcp_vm/
│       ├── main.tf
│       └── variables.tf

✅ Features

  • Creates a GCP Compute Engine instance
  • Accepts parameters: instance type, zone, image
  • Installs nginx via startup script
  • Outputs the public IP of the VM

🔧 Requirements

  • Terraform v1.3+
  • GCP credentials configured via gcloud auth application-default login

🚀 How to Use

  1. Clone the repo:
git clone <this-repo>
cd terraform-gcp-vm
  1. Initialize Terraform:
terraform init
  1. Set variables and apply:
terraform apply -var="project_id=your-gcp-project-id" -var="region=us-central1"
  1. After ~30 seconds, you’ll see:
Outputs:
vm_public_ip = "X.X.X.X"
  1. Visit the VM IP in your browser:
http://X.X.X.X → Should show NGINX welcome page

🧪 Testing

To test:

  • Use terraform plan to verify
  • Use terraform destroy to clean up after testing

📝 Notes

  • Default network is used; ensure "Allow HTTP traffic" is enabled in firewall
  • Add SSH access or use IAP as needed

🧪 Terratest (Integration Testing)

To run the integration test using Terratest:

🔧 Requirements

  • Go 1.21+
  • Terraform installed
  • GCP credentials set up (gcloud auth application-default login)

▶️ Run Test

cd test
go mod tidy
go test -v

This will be expected to provision a VM, verify NGINX is running, then destroy the VM.

🚀 Further Improvements

  • Use Service Account for Authentication

    • Replace reliance on gcloud auth application-default login with a dedicated service account key to improve portability and CI/CD support.
  • Test Enhancements

    • Improve test stability by:
      • Adding exponential backoff and retry logic
      • Making NGINX content check more flexible (use regex or header check)
      • Validating instance metadata via gcloud or GCP APIs
  • Expose More Config Options

    • Add optional inputs for:
      • Tags, labels, metadata
      • Disk size/type
      • Custom startup script
  • Support Multiple Instances

    • Extend the module to provision a group of VMs using count or for_each.
  • Add Outputs for Internal IP, Status, and SSH Command

    • Provide more useful output for users to interact with the VM.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages