cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

terraform create multiple db user

francly
New Contributor II

Hi, follow the example to create one user. It's working however I want to create multiple users, I have tried many ways but still cannot get it work, please share some idea.

https://registry.terraform.io/providers/databricks/databricks/latest/docs/resources/user

data "databricks_group" "admins" {
  display_name = "admins"
}
 
resource "databricks_user" "me" {
  user_name = "me@example.com"
}
 
resource "databricks_group_member" "i-am-admin" {
  group_id  = data.databricks_group.admins.id
  member_id = databricks_user.me.id
}

1 ACCEPTED SOLUTION

Accepted Solutions

629699
New Contributor III

as @Cedric Law Hing Ping​ mentioned, you can use `count` or `foreach` in Terraform to do this.

Here's a simple example:

```

locals {

user_emails = toset([

"user1@example.com",

"user2@example.com",

"user3@example.com"

])

}

resource "databricks_user" "engineers" {

for_each = local.user_emails

user_name = each.value

}

data "databricks_group" "admins" {

display_name = "admins"

}

resource "databricks_group_member" "engineer-admins" {

for_each = databricks_user.engineers

group_id = data.databricks_group.admins.id

member_id = each.value.id

}

```

View solution in original post

5 REPLIES 5

Hopkins696
New Contributor II

count is one of the meta-parameters available to all resources. We can specify the number of identical resources to create.

Taco Bell Survey

Cedric
Valued Contributor
Valued Contributor

You would have to create multiple "databricks_user" resources. Using count or foreach should help you with creating multiple of the same resource.

629699
New Contributor III

as @Cedric Law Hing Ping​ mentioned, you can use `count` or `foreach` in Terraform to do this.

Here's a simple example:

```

locals {

user_emails = toset([

"user1@example.com",

"user2@example.com",

"user3@example.com"

])

}

resource "databricks_user" "engineers" {

for_each = local.user_emails

user_name = each.value

}

data "databricks_group" "admins" {

display_name = "admins"

}

resource "databricks_group_member" "engineer-admins" {

for_each = databricks_user.engineers

group_id = data.databricks_group.admins.id

member_id = each.value.id

}

```

francly
New Contributor II

Thanks @Zach King​ this solution work for me.

Natlab
New Contributor II

What if I want to give User Name along with the email ID?
I used below code but its not helping(code is not failing, but not adding user name)
It seems this code line: "display_name = each.key" is not working. Pls suggest.

 

 
terraform {
required_providers {
databricks = {
source = "databricks/databricks"
}
}
}
provider "databricks" {
#alias = "workspace"
token = "dapixxxxx"
}
locals {
users = {
User1 = "user1@example.com",
User2 = "user2@example.com",
User3 = "user3@example.com"
}
}
resource "databricks_user" "engineers" {
for_each = local.users
display_name = each.key
user_name = each.value
allow_cluster_create = false
allow_instance_pool_create = false
databricks_sql_access = true
workspace_access = true
}
Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.