|
| 1 | +locals { |
| 2 | + spark_conf_single_node = var.cloud_name == "azure" ? { |
| 3 | + "spark.master" = "local[*]", |
| 4 | + "spark.databricks.cluster.profile" = "singleNode" |
| 5 | + } : {} |
| 6 | + |
| 7 | + default_node_type_ids = { |
| 8 | + azure_node_type_id = "Standard_D4ds_v5" |
| 9 | + aws_node_type_id = "m5d.large" |
| 10 | + # gcp_node_type_id = "gcp-default-node-type-id" |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +resource "databricks_cluster" "this" { |
| 15 | + for_each = { for cluster in var.clusters : cluster.cluster_name => cluster } |
| 16 | + |
| 17 | + cluster_name = each.value.cluster_name |
| 18 | + spark_version = each.value.spark_version |
| 19 | + node_type_id = coalesce(each.value.node_type_id, local.default_node_type_ids["${var.cloud_name}_node_type_id"]) |
| 20 | + autotermination_minutes = each.value.autotermination_minutes |
| 21 | + data_security_mode = each.value.data_security_mode |
| 22 | + custom_tags = var.cloud_name == "azure" && each.value.single_node_enable ? merge({ "ResourceClass" = "SingleNode" }, each.value.custom_tags) : each.value.custom_tags |
| 23 | + |
| 24 | + # Azure conditional configuration for Spark Conf |
| 25 | + spark_conf = var.cloud_name == "azure" ? merge( |
| 26 | + each.value.single_node_enable == true ? local.spark_conf_single_node : {}, |
| 27 | + each.value.spark_conf |
| 28 | + ) : each.value.spark_conf |
| 29 | + |
| 30 | + # Autoscaling block for AWS |
| 31 | + dynamic "autoscale" { |
| 32 | + for_each = var.cloud_name == "aws" || !each.value.single_node_enable ? [1] : [] |
| 33 | + content { |
| 34 | + min_workers = each.value.min_workers |
| 35 | + max_workers = each.value.max_workers |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + # Specific attributes for AWS |
| 40 | + dynamic "aws_attributes" { |
| 41 | + for_each = var.cloud_name == "aws" ? [each.value] : [] |
| 42 | + content { |
| 43 | + availability = each.value.aws_attributes.availability |
| 44 | + zone_id = each.value.aws_attributes.zone_id |
| 45 | + first_on_demand = each.value.aws_attributes.first_on_demand |
| 46 | + spot_bid_price_percent = each.value.aws_attributes.spot_bid_price_percent |
| 47 | + ebs_volume_count = each.value.aws_attributes.ebs_volume_count |
| 48 | + ebs_volume_size = each.value.aws_attributes.ebs_volume_size |
| 49 | + ebs_volume_type = each.value.aws_attributes.ebs_volume_type |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + # Specific attributes for Azure |
| 54 | + dynamic "azure_attributes" { |
| 55 | + for_each = var.cloud_name == "azure" ? [each.value] : [] |
| 56 | + content { |
| 57 | + availability = each.value.azure_attributes.availability |
| 58 | + first_on_demand = each.value.azure_attributes.first_on_demand |
| 59 | + spot_bid_max_price = each.value.azure_attributes.spot_bid_max_price |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + # Specific configurations |
| 64 | + dynamic "cluster_log_conf" { |
| 65 | + for_each = var.cloud_name == "azure" && each.value.cluster_log_conf_destination != null ? [each.value.cluster_log_conf_destination] : [] |
| 66 | + content { |
| 67 | + dynamic "dbfs" { |
| 68 | + for_each = var.cloud_name == "azure" ? [1] : [] |
| 69 | + content { |
| 70 | + destination = cluster_log_conf.value |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + # TODO |
| 75 | + # dynamic "s3" { |
| 76 | + # for_each = var.cloud_name == "aws" ? [1] : [] |
| 77 | + # content { |
| 78 | + # destination = "s3://acmecorp-main/cluster-logs" |
| 79 | + # region = var.region |
| 80 | + # } |
| 81 | + # } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + dynamic "init_scripts" { |
| 86 | + for_each = each.value.init_scripts_workspace != null ? each.value.init_scripts_workspace : [] |
| 87 | + content { |
| 88 | + workspace { |
| 89 | + destination = init_scripts.value |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + dynamic "init_scripts" { |
| 95 | + for_each = each.value.init_scripts_volumes != null ? each.value.init_scripts_volumes : [] |
| 96 | + content { |
| 97 | + volumes { |
| 98 | + destination = init_scripts.value |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + dynamic "init_scripts" { |
| 104 | + for_each = var.cloud_name == "azure" && each.value.init_scripts_dbfs != null ? each.value.init_scripts_dbfs : [] |
| 105 | + content { |
| 106 | + dbfs { |
| 107 | + destination = init_scripts.value |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + dynamic "init_scripts" { |
| 113 | + for_each = var.cloud_name == "azure" && each.value.init_scripts_abfss != null ? each.value.init_scripts_abfss : [] |
| 114 | + content { |
| 115 | + abfss { |
| 116 | + destination = init_scripts.value |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + # Library configurations |
| 122 | + dynamic "library" { |
| 123 | + for_each = each.value.pypi_library_repository != null ? each.value.pypi_library_repository : [] |
| 124 | + content { |
| 125 | + pypi { |
| 126 | + package = library.value |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + dynamic "library" { |
| 132 | + for_each = each.value.maven_library_repository != null ? each.value.maven_library_repository : [] |
| 133 | + content { |
| 134 | + maven { |
| 135 | + coordinates = library.value.coordinates |
| 136 | + exclusions = library.value.exclusions |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +resource "databricks_cluster_policy" "this" { |
| 143 | + for_each = { for param in var.custom_cluster_policies : (param.name) => param.definition |
| 144 | + if param.definition != null |
| 145 | + } |
| 146 | + |
| 147 | + name = each.key |
| 148 | + definition = jsonencode(each.value) |
| 149 | +} |
| 150 | + |
| 151 | +resource "databricks_cluster_policy" "overrides" { |
| 152 | + for_each = { for param in var.default_cluster_policies_override : (param.name) => param |
| 153 | + if param.definition != null |
| 154 | + } |
| 155 | + |
| 156 | + policy_family_id = each.value.family_id |
| 157 | + policy_family_definition_overrides = jsonencode(each.value.definition) |
| 158 | + name = each.key |
| 159 | +} |
| 160 | + |
| 161 | +resource "databricks_permissions" "policy" { |
| 162 | + for_each = { for param in var.custom_cluster_policies : param.name => param.can_use |
| 163 | + if param.can_use != null |
| 164 | + } |
| 165 | + |
| 166 | + cluster_policy_id = databricks_cluster_policy.this[each.key].id |
| 167 | + |
| 168 | + dynamic "access_control" { |
| 169 | + for_each = each.value |
| 170 | + content { |
| 171 | + group_name = access_control.value |
| 172 | + permission_level = "CAN_USE" |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +resource "databricks_permissions" "clusters" { |
| 178 | + for_each = { |
| 179 | + for v in var.clusters : (v.cluster_name) => v |
| 180 | + if length(v.permissions) != 0 |
| 181 | + } |
| 182 | + |
| 183 | + cluster_id = databricks_cluster.this[each.key].id |
| 184 | + |
| 185 | + dynamic "access_control" { |
| 186 | + for_each = each.value.permissions |
| 187 | + content { |
| 188 | + group_name = access_control.value.group_name |
| 189 | + permission_level = access_control.value.permission_level |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments