user_01_02
user_01_02

Reputation: 763

Terraform interpolation for lists?

I am trying to use a terraform gitlab-provider . And i want pass maps in the form of lists and call them in module.

How can i achieve this? Is there any interpolation syntax that can used here?

# names and can_create_groups
variable "names" {
  type    = "list"
  default = [
    {
     "name"             = "test"
     "username"         = "test"
     "email"            = "[email protected]"
     "project_limit"    = "100"
     "can_create_groups"= "false"
     "is_admin"         = "false"
    },
    {
     "name"             = "test2"
     "username"         = "tetst.2"
     "email"            = "[email protected]"
     "project_limit"    = "100"
     "can_create_groups"= "true"
     "is_admin"         = "false"
    }
  ]
}

GIT - Users

resource "gitlab_user" "user" {
  name             = "${element(var.names,count.index)}"
  username         = "${element(var.names,count.index)}"
  password         = "dummypassword"
  email            = "${element(var.names,count.index)}"
  is_admin         = "${element(var.names,count.index)}"
  projects_limit   = "${element(var.names,count.index)}"
  can_create_group = "${element(var.names,count.index)}"
  count            = 2
}

error: Error: gitlab_user.user: 2 error(s) occurred:

${element(var.names,count.index)} * gitlab_user.user[1]: element: element() may only be used with flat lists, this list contains elements of type map in:

${element(var.names,count.index)}

Upvotes: 0

Views: 2216

Answers (2)

user_01_02
user_01_02

Reputation: 763

resource "gitlab_user" "user" {
  name             = "${lookup(var.gitlab_users[count.index], "name")}"
  username         = "${lookup(var.gitlab_users[count.index], "username")}"
  password         = "dummypassword"
  email            = "${lookup(var.gitlab_users[count.index], "email")}"
  is_admin         = "${lookup(var.gitlab_users[count.index], "is_admin")}"
  projects_limit   = "${lookup(var.gitlab_users[count.index], "projects_limit")}"
  can_create_group = "${lookup(var.gitlab_users[count.index], "can_create_groups")}"
  count            = "${length(var.gitlab_users)}"
}

Upvotes: 0

rwisch45
rwisch45

Reputation: 3702

You can use some of the built in Terraform functions to make it work

resource "gitlab_user" "user" {
  name             = "${lookup(var.names, element(keys(var.names), count.index))}"
  password         = "dummypassword"

  etc, etc, etc

  count            =  "${length(keys(var.names))}"
}

Upvotes: 1

Related Questions