|
| 1 | +################################################################################ |
| 2 | +# Databricks Workspace |
| 3 | +################################################################################ |
| 4 | +resource "databricks_mws_workspaces" "this" { |
| 5 | + account_id = var.account_id |
| 6 | + aws_region = var.region |
| 7 | + workspace_name = var.label |
| 8 | + credentials_id = databricks_mws_credentials.this.credentials_id |
| 9 | + storage_configuration_id = databricks_mws_storage_configurations.this.storage_configuration_id |
| 10 | + network_id = databricks_mws_networks.this.network_id |
| 11 | + private_access_settings_id = try(databricks_mws_private_access_settings.this[0].private_access_settings_id, null) |
| 12 | + |
| 13 | + dynamic "token" { |
| 14 | + for_each = var.workspace_creator_token_enabled ? [1] : [] |
| 15 | + content { |
| 16 | + comment = "Workspace creator token managed by Terraform" |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + lifecycle { |
| 21 | + replace_triggered_by = [databricks_mws_credentials.this] |
| 22 | + } |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | +resource "databricks_mws_private_access_settings" "this" { |
| 27 | + count = var.private_access_settings_enabled ? 1 : 0 |
| 28 | + |
| 29 | + private_access_settings_name = coalesce(var.private_access_settings_config.name, var.label) |
| 30 | + region = var.region |
| 31 | + public_access_enabled = var.private_access_settings_config.public_access_enabled |
| 32 | + allowed_vpc_endpoint_ids = coalesce(var.private_access_settings_config.allowed_vpc_endpoint_ids, [var.privatelink_rest_vpce_id]) |
| 33 | + private_access_level = "ENDPOINT" |
| 34 | +} |
| 35 | + |
| 36 | +################################################################################ |
| 37 | +# Network |
| 38 | +################################################################################ |
| 39 | +resource "databricks_mws_networks" "this" { |
| 40 | + account_id = var.account_id |
| 41 | + network_name = var.label |
| 42 | + security_group_ids = var.security_group_ids |
| 43 | + subnet_ids = var.subnet_ids |
| 44 | + vpc_id = var.vpc_id |
| 45 | + |
| 46 | + dynamic "vpc_endpoints" { |
| 47 | + for_each = var.privatelink_enabled ? [1] : [] |
| 48 | + content { |
| 49 | + dataplane_relay = [coalesce(try(module.privatelink_vpce.relay_vpce_id, null), var.privatelink_relay_vpce_id)] |
| 50 | + rest_api = [coalesce(try(module.privatelink_vpce.rest_vpce_id, null), var.privatelink_rest_vpce_id)] |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +################################################################################ |
| 56 | +# Privatelink dedicated VPC Endpoints (REST/Relay) |
| 57 | +################################################################################ |
| 58 | +module "privatelink_vpce" { |
| 59 | + count = var.privatelink_dedicated_vpce_enabled ? 1 : 0 |
| 60 | + source = "./modules/privatelink/" |
| 61 | + |
| 62 | + account_id = var.account_id |
| 63 | + region = var.region |
| 64 | + relay_vpc_endpoint_name = var.privatelink_dedicated_vpce_config.relay_vpc_endpoint_name |
| 65 | + relay_aws_vpc_endpoint_id = var.privatelink_dedicated_vpce_config.relay_aws_vpc_endpoint_id |
| 66 | + rest_vpc_endpoint_name = var.privatelink_dedicated_vpce_config.rest_vpc_endpoint_name |
| 67 | + rest_aws_vpc_endpoint_id = var.privatelink_dedicated_vpce_config.rest_aws_vpc_endpoint_id |
| 68 | +} |
| 69 | + |
| 70 | +################################################################################ |
| 71 | +# IAM |
| 72 | +################################################################################ |
| 73 | +data "databricks_aws_assume_role_policy" "this" { |
| 74 | + external_id = var.account_id |
| 75 | +} |
| 76 | + |
| 77 | +data "databricks_aws_crossaccount_policy" "this" {} |
| 78 | + |
| 79 | +module "iam_cross_account_workspace_policy" { |
| 80 | + source = "terraform-aws-modules/iam/aws//modules/iam-policy" |
| 81 | + version = "5.41.0" |
| 82 | + |
| 83 | + name = coalesce(var.iam_cross_account_workspace_role_config.policy_name, "${var.label}-dbx-crossaccount-policy") |
| 84 | + policy = data.databricks_aws_crossaccount_policy.this.json |
| 85 | +} |
| 86 | + |
| 87 | +module "iam_cross_account_workspace_role" { |
| 88 | + count = var.iam_cross_account_workspace_role_enabled ? 1 : 0 |
| 89 | + source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role" |
| 90 | + version = "5.41.0" |
| 91 | + |
| 92 | + role_name = coalesce(var.iam_cross_account_workspace_role_config.role_name, "${var.label}-dbx-cross-account") |
| 93 | + create_role = var.iam_cross_account_workspace_role_enabled |
| 94 | + create_custom_role_trust_policy = true |
| 95 | + custom_role_trust_policy = data.databricks_aws_assume_role_policy.this.json |
| 96 | + role_permissions_boundary_arn = var.iam_cross_account_workspace_role_config.permission_boundary_arn |
| 97 | + role_description = var.iam_cross_account_workspace_role_config.role_description |
| 98 | + custom_role_policy_arns = [module.iam_cross_account_workspace_policy.arn] |
| 99 | + tags = var.tags |
| 100 | +} |
| 101 | + |
| 102 | +# It is required to wait up to 30 seconds after role creation so Databricks would successfuly reference it |
| 103 | +resource "time_sleep" "wait_30_seconds" { |
| 104 | + depends_on = [module.iam_cross_account_workspace_role] |
| 105 | + |
| 106 | + create_duration = "30s" |
| 107 | +} |
| 108 | + |
| 109 | +resource "databricks_mws_credentials" "this" { |
| 110 | + account_id = var.account_id |
| 111 | + credentials_name = "${var.label}-credentials" |
| 112 | + role_arn = module.iam_cross_account_workspace_role[0].iam_role_arn |
| 113 | + |
| 114 | + depends_on = [time_sleep.wait_30_seconds] |
| 115 | +} |
| 116 | + |
| 117 | +################################################################################ |
| 118 | +# Storage Configuration |
| 119 | +################################################################################ |
| 120 | +data "databricks_aws_bucket_policy" "this" { |
| 121 | + bucket = module.storage_configuration_dbfs_bucket[0].s3_bucket_id |
| 122 | +} |
| 123 | + |
| 124 | +module "storage_configuration_dbfs_bucket" { |
| 125 | + count = var.storage_dbfs_enabled ? 1 : 0 |
| 126 | + source = "terraform-aws-modules/s3-bucket/aws" |
| 127 | + version = "4.1.2" |
| 128 | + |
| 129 | + bucket_prefix = coalesce(var.storage_dbfs_config.bucket_name, "${var.label}-dbfs-") |
| 130 | + acl = "private" |
| 131 | + |
| 132 | + force_destroy = true |
| 133 | + |
| 134 | + control_object_ownership = true |
| 135 | + object_ownership = "BucketOwnerPreferred" |
| 136 | + |
| 137 | + server_side_encryption_configuration = { |
| 138 | + rule = { |
| 139 | + apply_server_side_encryption_by_default = { |
| 140 | + sse_algorithm = "AES256" |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + versioning = { |
| 146 | + status = "Disabled" |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +resource "aws_s3_bucket_policy" "databricks_aws_bucket_policy" { |
| 152 | + bucket = module.storage_configuration_dbfs_bucket[0].s3_bucket_id |
| 153 | + policy = data.databricks_aws_bucket_policy.this.json |
| 154 | +} |
| 155 | + |
| 156 | +resource "databricks_mws_storage_configurations" "this" { |
| 157 | + account_id = var.account_id |
| 158 | + storage_configuration_name = var.label |
| 159 | + bucket_name = module.storage_configuration_dbfs_bucket[0].s3_bucket_id |
| 160 | +} |
0 commit comments