gke3

Create a main.tf

resource "google_container_cluster" "primary" {
  name               = var.name
  location           = var.location
  network            = var.network
  subnetwork         = var.subnetwork
  node_locations     = var.node_locations
  default_max_pods_per_node = var.default_max_pods_per_node
  min_master_version = var.master_version
  node_version = var.node_version
  
  resource_labels = {
      cloud    = var.labels["cloud"]
      cluster  = var.labels["cluster"]
      region   = var.labels["region"]
  }
  network_policy {
      enabled = true
  }
   addons_config {
        cloudrun_config {
            disabled = true
        }

        horizontal_pod_autoscaling {
            disabled = true
        }

        http_load_balancing {
            disabled = true
        }

        network_policy_config {
            disabled = true
        }
    }
  cluster_autoscaling {
        enabled = true

        resource_limits {
            maximum       = 2
            minimum       = 1
            resource_type = "memory"
        }
        resource_limits {
            maximum       = 2
            minimum       = 1
            resource_type = "cpu"
        }
    }

  ip_allocation_policy {
    cluster_ipv4_cidr_block  = "/16"
    services_ipv4_cidr_block = "/22"
  }

  master_auth {
    username = ""
    password = ""

    client_certificate_config {
      issue_client_certificate = false
    }
  }

  node_pool {
    initial_node_count = var.initial_node_count
    node_config {
        oauth_scopes = var.oauth_scopes
        machine_type = var.machine_type
        image_type   = var.image_type
        disk_type    = var.disk_type
        disk_size_gb = var.disk_size_gb

        metadata = {
        disable-legacy-endpoints = "true"
        }

        labels = {
            cloud    = var.labels["cloud"]
            cluster  = var.labels["cluster"]
            region   = var.labels["region"]
        }

        tags = var.tags
    }
    autoscaling {
        max_node_count = var.max_node_count
        min_node_count = var.min_node_count
    }

    }

}


provider.tf provider specs like google, aws azure etc

provider "google" {
  credentials        = file("./sa.json")
  project            = var.project
  region             = var.region
}

state.tf for storing the state in Google Cloud Storage

terraform {
  backend "gcs" {
    bucket  = "terraform_state_sodium"
    prefix  = "gke/state"
  }
}

variable.tf initializing the variables

variable "name" {
    type = string
    default = "test"
}
variable "network" {
    type = string
    default = "default"
}
variable "subnetwork" {
    type = string
    default = "default"
}
variable "master_ipv4_cidr_block" {
    type = string
    default = ""
}

variable "location" {
    type = string
    default = "us-central1-a"
}
variable "region" {
    type = string
    default = "us-central1"
}
variable "master_version" {
    type = string
    default = "1.15.12-gke.16"
}
variable "node_version" {
    type = string
    default = "1.15.12-gke.16"
}
variable "initial_node_count" {
    type = number
    default = 1
}
variable "default_max_pods_per_node" {
    type = number
    default = 10
}
variable "max_node_count" {
    type = number
    default = 5
}
variable "min_node_count" {
    type = number
    default = 1
}
variable "machine_type" {
    type = string
    default = "n1-standard-1"
}
variable "image_type" {
    type = string
    default = "COS"
}
variable "disk_type" {
    type = string
    default = "pd-standard"
}
variable "disk_size_gb" {
    type = string
    default = "50"
}

variable "labels" {
    type = map
    default = {
        "cloud"   = "gcp"
        "cluster"  = "gke"
        "region"  = "us-central1"
    }
}

variable "project" {
    type = string
    default = "totemic-formula-279901"
}

variable "tags"{
    type = list
    default = ["allow-ping", "allow-public"]
}
variable "node_locations"{
    type = list
    default = ["us-central1-b","us-central1-c"]
}

variable "oauth_scopes"{
    type = list
    default = ["https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring"]
}