Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I skip optional arguments in a terraform resource based on a condition

I am using Terraform modules to maintain multiple environments and for one of the resources in modules, there is an optional argument that I only want to use for one environment and not the rest of them.

For example, in the resource below I only want the argument node_shape_config to be applied for one environment and not the rest of them. Is there a way to do this in terraform?

resource "oci_containerengine_node_pool" "nodepools" {
  cluster_id     = oci_containerengine_cluster.k8s_cluster.id
  compartment_id = var.oci_vcn.compartment_id
  depends_on     = ["oci_containerengine_cluster.k8s_cluster"]

  kubernetes_version = var.oke_cluster.kubernetes_version
  name               = "${var.oci_vcn.label_prefix}-np-${count.index + 1}"
  subnet_ids         = [var.oke_cluster.worker_subnet_id_1, var.oke_cluster.worker_subnet_id_2, var.oke_cluster.worker_subnet_id_3]

  node_shape_config {
    #Optional
    ocpus = 4
  }

  node_source_details {
    image_id      = var.node_pools.create_custom_image == true ? var.node_pools.custom_image_id : var.node_pools.node_pool_node_image_id
    source_type   = "IMAGE"
  }

  ...
  ...
  ...

  count = var.node_pools.node_pool_count
}
like image 587
sanjeev kaneria Avatar asked Nov 18 '25 23:11

sanjeev kaneria


1 Answers

What you are looking for is dynamic blocks. You need to make node_shape_config block dynamic and you can do it like this:

# assuming you have variable for environment
dynamic "node_shape_config" {
    for_each = var.environment === "development" ? toset([1]) : toset([])
    content {
      ocpus = 4
    }
  }

It will set node_shape_config only for development environment

like image 187
rkm Avatar answered Nov 21 '25 09:11

rkm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!