Get Index Count in Terraform Dynamic Block

Tell me, please, How can I get the iteration index in a dynamic block when iterating over an object? I need to replace 0 with index to get different passwords for different users.

resource "random_password" "pwd" {
  count = length(var.users)
  length           = 18
  special          = true
  override_special = "_!%@"
}

resource "yandex_mdb_postgresql_cluster" "mdb_postgres" {

  dynamic "user" {
    for_each = var.users
    content {
      name       = user.value.name
      password   = user.value.password == "" || user.value.password == null ? random_password.pwd[0].result : user.value.password
    }
  }
}

The user is an object

variable "users" {
  type = map(object({
    name       = string
    password   = string
    conn_limit = number
    permissions = list(object({
      database_name = string
    }))
  }))
 }
3

1 Answer

Since you are using for_each in the yandex_mdb_postgresql_cluster resource, the same logic can be used in the case of the password creation. That way a random_password resource with the same key names as in the second resource will be created. Here's a working example:

resource "random_password" "pwd" {
  for_each         = var.users
  length           = 18
  special          = true
  override_special = "_!%@"
}

variable "users" {
  type = map(object({
    name       = string
    password   = string
    conn_limit = number
    permissions = list(object({
      database_name = string
    }))
  }))
}

For a terraform.tfvars file with the following values:

users = {
  "marko" = {
    conn_limit = 1
    name       = "marko"
    password   = "pass123"
    permissions = [{
      database_name = "dbname"
    }]
  },
  "valeriy" = {
    conn_limit = 1
    name       = "valeriy"
    password   = "pass456"
    permissions = [{
      database_name = "dbname1"
    }]
  }
}

it will create two elements with keys that correspond to the keys in the users variable:

Terraform will perform the following actions:

  # random_password.pwd["marko"] will be created
  + resource "random_password" "pwd" {
      + id               = (known after apply)
      + length           = 18
      + lower            = true
      + min_lower        = 0
      + min_numeric      = 0
      + min_special      = 0
      + min_upper        = 0
      + number           = true
      + override_special = "_!%@"
      + result           = (sensitive value)
      + special          = true
      + upper            = true
    }

  # random_password.pwd["valeriy"] will be created
  + resource "random_password" "pwd" {
      + id               = (known after apply)
      + length           = 18
      + lower            = true
      + min_lower        = 0
      + min_numeric      = 0
      + min_special      = 0
      + min_upper        = 0
      + number           = true
      + override_special = "_!%@"
      + result           = (sensitive value)
      + special          = true
      + upper            = true
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

random_password.pwd["valeriy"]: Creating...
random_password.pwd["marko"]: Creating...
random_password.pwd["valeriy"]: Creation complete after 0s [id=none]
random_password.pwd["marko"]: Creation complete after 0s [id=none]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Take note of the key in the square brackets, e.g.: random_password.pwd["valeriy"].

This means you can now reference the password in the second resource by using the key name from the users variable:

resource "yandex_mdb_postgresql_cluster" "mdb_postgres" {

  dynamic "user" {
    for_each = var.users
    content {
      name       = user.value.name
      password   = user.value.password == "" || user.value.password == null ? random_password.pwd[user.key].result : user.value.password
    }
  }
}

Note the way to access the value of random password now is with user.key in random_password.pwd[user.key].result.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest