Skip to content

Tarique-B-DevOps/Terraform-GCP-VPC-GKE

Repository files navigation

Terraform GCP VPC & GKE

This repository provisions infrastructure on Google Cloud Platform (GCP) using Terraform. It sets up a Virtual Private Cloud (VPC) and a Google Kubernetes Engine (GKE) cluster with modular, parameterized support and CI/CD automation via Jenkins.


⚙️ Features

  • Modular design with vpc and gke Terraform modules
  • Supports multiple backend types: remote, gcs, s3, azurerm
  • Jenkinsfile for automated provisioning and destruction
  • Quick setup for repeatable infrastructure deployments on GCP

🚀 Quick Deployment Steps

  1. Clone the repository

  2. Change into the project directory

  3. Initialize Terraform with a backend config
    terraform init -backend-config=<path-to-backend-config-file>

  4. Preview infrastructure changes
    terraform plan -var-file=<path-to-var-file>

  5. Apply the configuration
    terraform apply -var-file=<path-to-var-file>


🤖 Configure with Jenkins

This project includes a Jenkinsfile with parameterized deployment options:

  • Select backend type: remote, gcs, s3, azurerm
  • Choose execution mode: local or remote (Terraform Cloud)
  • Input backend config and variable files
  • Toggle destroy mode for cleanup

Jenkins Setup Steps

  1. Create a new Jenkins pipeline job
  2. Point it to this GitHub repository
  3. Configure the following parameters:
    • BACKEND_TYPE (e.g., gcs)
    • HCP_EXEC_MODE (e.g., local)
    • BACKEND_CONFIG (e.g., gcs-staging.hcl)
    • TF_VAR_FILE (e.g., gcp.tfvars)
    • DESTROY_TERRAFORM (true/false)
  4. Trigger the pipeline to deploy or destroy GCP infrastructure

Image

Using Terraform Cloud as the Backend

  1. Create an environment variable in your Terraform Cloud workspace:

    • Name: GOOGLE_CREDENTIALS

    • Value: Paste your entire GCP JSON key file without newline characters.

      To remove newlines from your JSON file, run:

      cat CREDENTIALS.json | tr -s '\n' ' '
      
    • Mark the variable as Sensitive

  2. Save the variable.

  3. All runs in this workspace will use the GOOGLE_CREDENTIALS environment variable to authenticate with Google Cloud Platform.

Setting GKE Credentials for kubectl

  1. Install GKE Auth Plugin:

    • Ubuntu/Debian:

      sudo apt-get install google-cloud-sdk-gke-gcloud-auth-plugin -y
    • RHEL/CentOS/Amazon Linux:

      sudo yum install google-cloud-sdk-gke-gcloud-auth-plugin -y
  2. Configure kubectl:

    • Get the command from the configure_kubeconfig output

      terraform output -raw configure_kubeconfig
    • Or run the following by replacing with your actual values:

      gcloud container clusters get-credentials <CLUSTER_NAME> \
             --zone <ZONE> \
             --project <PROJECT_ID>

About

Terraform module to provision a Virtual Private Cloud (VPC) and a Google Kubernetes Engine (GKE) cluster with minimal configuration, deployable using an advanced Jenkinsfile setup.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages