terraformconfluent-cloud

How to parameterise cluster type (basic/standard/dedicated) on confluent cloud


I am trying to automate cluster creation process of confluent cloud using terraform. I would like to spawn different clusters for each environment using the same code through parameters.

 resource "confluent_kafka_cluster" "standard" {
  display_name = "standard_kafka_cluster"
  availability = "SINGLE_ZONE"
  cloud        = "AZURE"
  region       = "centralus"
  standard {}

  environment {
    id = confluent_environment.development.id
  }

  lifecycle {
    prevent_destroy = true
  }
}

I would like to parametrize standard/basic/dedicated so that I can have basic in dev/staging and standard/dedicated on uat/prod.

I have tried to do it using dynamic block. Haven't got any success yet. Any help would be really appreciated.


Solution

  • You can do this with dynamic blocks and for_each. However, it seems a bit like tricking Terraform.

    resource "confluent_kafka_cluster" "example" {
      display_name = var.display_name
      availability = var.availability
      cloud        = var.cloud
      region       = var.region
    
      dynamic "basic" {
        for_each = [for value in [var.cluster_type] : value if value == "BASIC"]
        content {
        }
      }
    
      dynamic "standard" {
        for_each = [for value in [var.cluster_type] : value if value == "STANDARD"]
        content {
        }
      }
    
      dynamic "dedicated" {
        for_each = [for value in [var.cluster_type] : value if value == "DEDICATED"]
        content {
          cku = var.cku
        }
      }
    
      environment {
        id = var.environment_id
      }
    
      dynamic "network" {
        for_each = [for value in [var.network_id] : value if value != null]
        content {
          id = network.value
        }
      }
    }