Looping through a map, multiple keys

Hello, I am trying to do something similar to this discussion : Looping through a map, multiple keys?

However, I’m trying to use a dynamic block to populate parameters in a aws_db_parameter_group.

My variable looks like this:

variable "params_diorad3" {
  default = {
    "param_1" = {
      name         = "SQLNET.SSL_VERSION"
      value        = "1.2"
      apply_method = "immediate"
    },
    "param_2" = {
      name         = "SQLNET.CIPHER_SUITE"
      value        = "SSL_RSA_WITH_AES_256_CBC_SHA"
      apply_method = "immediate"
    }
  }
}

There’s a lot more parameters, but I’ve just cut it down to two for this post.

My aws_db_parameter_group looks like this:

resource "aws_db_parameter_group" "diorad" {
  name   = "diorad"
  family = "oracle-ee2-19"

  dynamic "parameter" {
    for_each = var.params_diorad3

    content {
      name         = each.value.name
      value        = each.value.value
      apply_method = each.value.apply_method
    }
  }
}

However, when I run a plan I get:

Error: each.value cannot be used in this context

  on main.tf line 17, in resource "aws_db_parameter_group" "diorad":
  17:       name         = each.value.name

A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.


Error: each.value cannot be used in this context

  on main.tf line 17, in resource "aws_db_parameter_group" "diorad":
  17:       name         = each.value.name

A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.


Error: each.value cannot be used in this context

  on main.tf line 18, in resource "aws_db_parameter_group" "diorad":
  18:       value        = each.value.value

A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.


Error: each.value cannot be used in this context

  on main.tf line 18, in resource "aws_db_parameter_group" "diorad":
  18:       value        = each.value.value

A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.


Error: each.value cannot be used in this context

  on main.tf line 19, in resource "aws_db_parameter_group" "diorad":
  19:       apply_method = each.value.apply_method

A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.


Error: each.value cannot be used in this context

  on main.tf line 19, in resource "aws_db_parameter_group" "diorad":
  19:       apply_method = each.value.apply_method

A reference to "each.value" has been used in a context in which it
unavailable, such as when the configuration no longer contains the value in
its "for_each" expression. Remove this reference to each.value in your
configuration to work around this error.

Why doesn’t it work for me?

Hi @wblakecannon,

Dynamic blocks use the block name as the default iterator, so changing each to parameter will make your example work:


resource "aws_db_parameter_group" "diorad" {
  name   = "diorad"
  family = "oracle-ee2-19"

  dynamic "parameter" {
    for_each = var.params_diorad3

    content {
      name         = parameter.value.name
      value        = parameter.value.value
      apply_method = parameter.value.apply_method
    }
  }
}

Doh. Thanks very much. I knew it would be something simple that I missed.