1+ variable "account_id" {
2+ type = string
3+ description = " Account Id that could be found in the bottom left corner of https://accounts.cloud.databricks.com/"
4+ }
5+
6+ variable "username" {
7+ type = string
8+ description = " Username to access https://accounts.cloud.databricks.com/"
9+ }
10+
11+ variable "password" {
12+ type = string
13+ description = " Password to access https://accounts.cloud.databricks.com/"
14+ }
15+
16+ variable "credentials_id" {
17+ type = string
18+ description = " credentials_id from databricks_mws_credentials"
19+ }
20+
21+ variable "storage_configuration_id" {
22+ type = string
23+ description = " storage_configuration_id from databricks_mws_storage_configurations"
24+ }
25+
26+ variable "vpc_id" {
27+ type = string
28+ description = " AWS VPC id"
29+ }
30+
31+ variable "aws_security_group_ids" {
32+ description = " AWS VPC SG ids"
33+ }
34+
35+ variable "region" {
36+ type = string
37+ description = " AWS region name"
38+ }
39+
40+ variable "deployment_name" {
41+ type = string
42+ description = " Name of the workspace"
43+ }
44+
45+ provider "databricks" {
46+ host = " https://accounts.cloud.databricks.com/"
47+ username = var. username
48+ password = var. password
49+ }
50+
51+ provider "aws" {
52+ region = var. region
53+ }
54+
55+ locals {
56+ availability_zones = toset ([for s in data . aws_subnet . other : s . availability_zone ])
57+ availability_range = range (0 , length (local. availability_zones ))
58+ cidr_newbits = 3
59+ private_subnets = zipmap (sort (local. availability_zones ),
60+ [for i in range (0 , length (local. availability_zones )) :
61+ cidrsubnet (data. aws_vpc . this . cidr_block , local. cidr_newbits ,
62+ length (local. availability_zones ) + 1 + i)
63+ ])
64+ route_tables_with_nat = [for rt in data . aws_route_table . vpc : rt . id if anytrue ([
65+ for r in rt . routes : r . nat_gateway_id != " "
66+ ])]
67+ rtb_assoc = { for x in flatten ([for rt in local . route_tables_with_nat :
68+ [for subnet in aws_subnet . private : {
69+ route_table_id = rt
70+ subnet_id = subnet.id
71+ }]]) : " ${ x . route_table_id } -${ x . subnet_id } " => x }
72+ }
73+
74+ data "aws_vpc" "this" {
75+ id = var. vpc_id
76+ }
77+
78+ data "aws_subnets" "other" {
79+ filter {
80+ name = " vpc-id"
81+ values = [data . aws_vpc . this . id ]
82+ }
83+ }
84+
85+ data "aws_subnet" "other" {
86+ for_each = toset (data. aws_subnets . other . ids )
87+ id = each. value
88+ }
89+
90+ data "aws_route_tables" "vpc" {
91+ vpc_id = data. aws_vpc . this . id
92+ }
93+
94+ data "aws_route_table" "vpc" {
95+ for_each = data. aws_route_tables . vpc . ids
96+ route_table_id = each. value
97+ }
98+
99+ resource "aws_subnet" "private" {
100+ for_each = local. private_subnets
101+ cidr_block = each. value
102+ availability_zone = each. key
103+ vpc_id = data. aws_vpc . this . id
104+ tags = merge (data. aws_vpc . this . tags , {
105+ Name = " ${ var . deployment_name } -private-${ each . key } "
106+ })
107+ }
108+
109+ resource "aws_route_table_association" "private" {
110+ for_each = local. rtb_assoc
111+ subnet_id = each. value . subnet_id
112+ route_table_id = each. value . route_table_id
113+ }
114+
115+ resource "databricks_mws_networks" "this" {
116+ account_id = var. account_id
117+ network_name = " ${ var . deployment_name } -network"
118+ vpc_id = data. aws_vpc . this . id
119+ subnet_ids = [for s in aws_subnet . private : s . id ]
120+ security_group_ids = var. aws_security_group_ids
121+ }
122+
123+ resource "databricks_mws_workspaces" "this" {
124+ account_id = var. account_id
125+ aws_region = var. region
126+ workspace_name = var. deployment_name
127+ deployment_name = var. deployment_name
128+
129+ credentials_id = var. credentials_id
130+ storage_configuration_id = var. storage_configuration_id
131+ network_id = databricks_mws_networks. this . network_id
132+
133+ token {
134+ }
135+ }
136+
137+ output "host" {
138+ value = databricks_mws_workspaces. this . workspace_url
139+ }
140+
141+ output "token" {
142+ value = databricks_mws_workspaces. this . token [0 ]. token_value
143+ sensitive = true
144+ }
0 commit comments