Matheus Schaly
Matheus Schaly

Reputation: 303

Managing GCP Composer Kubernetes cluster using Terraform

I've created a GCP Composer environment using Terraform:

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "3.5.0"
    }
  }
}

provider "google" {
  credentials = file("my_key.json")
  project = "my_project_id"
  region  = "us-east1"
  zone    = "us-east1-b"
}

resource "google_composer_environment" "my_composer_id" {
  name   = "my_composer_name"
  region = "us-east1"
  config {
    node_count = 3
    node_config {
      zone         = "us-east1-b"
      machine_type = "n1-standard-1"
    }
  }
}

Composer also automatically creates a Kubernetes Engine cluster. Such cluster has a single node pool called default-pool. I'd like to create a new node pool inside the cluster created by Composer. Something like this:

resource "google_container_node_pool" "my_node_pool_id" {
  name       = "my_node_pool_name"
  location   = "us-east1"
  cluster    = ????
  node_count = 0
  node_config {
    preemptible  = true
    machine_type = "n1-standard-1"
  }
  autoscaling  {
    min_node_count = 0
    max_node_count = 3
  }
}

However, as I didn't create the cluster in the Terraform file (as it was automatically created by Composer), I don't have the reference to it.

Upvotes: 0

Views: 1075

Answers (1)

norbjd
norbjd

Reputation: 11237

Cluster name can be accessed via the key gke_cluster available in the config section of your Cloud Composer environment :

resource "google_container_node_pool" "my_node_pool_id" {
  name       = "my_node_pool_name"
  location   = "us-east1-b"

  cluster    = element(
      split("/",
        lookup(
          google_composer_environment.my_composer_id.config[0],
          "gke_cluster"
        )
      ),
      5
    )

  // ...
}

The 5th element corresponds to the name of the GKE cluster.

Upvotes: 2

Related Questions