Skip to content

This Terraform GCP Compute Engine guide πŸš€ provides a structured approach to deploying and managing infrastructure on Google Cloud using Terraform. It includes setup instructions, modular configuration, execution steps, and cleanup processes πŸ› οΈ. Follow this guide for seamless automation, error-free deployment, and efficient resource management

Notifications You must be signed in to change notification settings

moshclouds/Managing-GCP-Compute-Engine-With-Terraform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Managing GCP Compute Engine with Terraform

Image

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.

πŸ“‘ Table of Contents

  1. βœ… Prerequisites
  2. πŸ“‚ Project Structure
  3. βš™οΈ Setting Up GCP Project
  4. πŸ”§ Setting Up Terraform
  5. πŸ› οΈ Terraform Configuration
  6. πŸš€ Running Terraform
  7. πŸ—‘οΈ Cleaning Up
  8. πŸ“Œ Additional Resources

βœ… Prerequisites

Before you begin, ensure that you have the following prerequisites:

  1. πŸ–₯️ Google Cloud Platform (GCP) Account: You will need an active Google Cloud account.
  2. πŸ“¦ Terraform Installed: Install Terraform on your local machine. If not already installed, follow this guide to set up Terraform.
  3. πŸ› οΈ gcloud CLI Installed: The gcloud command-line interface is required for authentication. You can install it following this guide.
  4. ☁️ A Google Cloud Project: Create a GCP project if you don't have one. You can do so in the Google Cloud Console.

πŸ“‚ Project Structure

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).

βš™οΈ Setting Up GCP Project

  1. 🌍 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.

Image

Image

  1. πŸ”Œ Enable Compute Engine API:
    • Navigate to APIs & Services > Library.
    • Search for Compute Engine API and click Enable.

Image

Image

Image

Image

Image

  1. πŸ‘€ Create a Service Account:
    • Navigate to IAM & Admin > Service Accounts.
    • Click Create Service Account, assign a role (Compute Admin or Project Owner).
    • Generate a JSON key and download it securely.

Image

Image

  1. 🌍 Set Environment Variables:

    export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your-keyfile.json"

    Replace /path/to/your-keyfile.json with the actual path.

πŸ”§ Setting Up Terraform

  1. ⬇️ Install Terraform: Follow the Terraform installation guide.

  2. πŸ› οΈ Initialize Terraform:

    terraform init

    Image

πŸ› οΈ Terraform Configuration

Below shows some example ways of configuring , for in detail approach refer the source code of this repo

  1. πŸ”— Define Provider Configuration (providers.tf):

    provider "google" {
      project     = var.project_id
      region      = var.region
      credentials = file(var.credentials_file)
    }
  2. βš™οΈ 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 {}
      }
    }
  3. πŸ“ 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
    }
  4. πŸ“€ Define Outputs (outputs.tf):

    output "instance_name" {
      value = google_compute_instance.vm_instance.name
    }

πŸš€ Running Terraform

  1. πŸ“ Plan the Deployment:

    terraform plan
  2. ⚑ Apply the Configuration:

    terraform apply

    Type yes when prompted.

    Image

  3. πŸ“Œ Verify the Deployment:

    • Navigate to Compute Engine > VM instances in the GCP Console.

πŸ—‘οΈ Cleaning Up

To remove all resources:

terraform destroy

Confirm by typing yes.

Image

Image

πŸ“Œ Additional Resources


This project provides an efficient way to manage GCP Compute Engine instances using Terraform, following best practices for modular configuration and automation. 🌟

About

This Terraform GCP Compute Engine guide πŸš€ provides a structured approach to deploying and managing infrastructure on Google Cloud using Terraform. It includes setup instructions, modular configuration, execution steps, and cleanup processes πŸ› οΈ. Follow this guide for seamless automation, error-free deployment, and efficient resource management

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages