cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
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

zcking
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
Databricks Employee
Databricks Employee

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

zcking
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
}

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group