1

In the following example enable_aggregate_limit is of type: bool. From what I understand it will render the parameter argument with content if its value is TRUE and it does not create the parameter if its FALSE. What does the ["yes"] and [] mean though in the ternary expression and what can I do to create the parameter when the value o enable_aggregate_limit is FALSE.

resource "aws_db_parameter_group" "main" {
  name   = local.name
  family = var.custom_parameter_group_family

  dynamic parameter {
    for_each = var.enable_aggregate_limit ? ["yes"] : []
    content {
      name         = "pga_aggregate_limit"
      value        = "0"
      apply_method = "pending-reboot"
  }
}
Hedge
  • 16,142
  • 42
  • 141
  • 246
  • 1
    This is a variant on the algorithm for a conditional block. See https://stackoverflow.com/questions/69034600/terraform-only-use-properties-if-value-is-greater-than-one/69035305#69035305. Other than that, ternaries behave the same in HCL2 as in any other language, so the first value is returned on a truthy conditional, and the second value on a falsey conditional, and either would be assigned as the value to the `for_each` meta-argument. – Matthew Schuchard Aug 03 '22 at 19:15

1 Answers1

2

Usually dynamic blocks are used to create several blocks of code with changing values inside. But in your case it is used together with ternary operator.

If for_each has empty value [] - dynamic block doesn't create any code.

If for_each receive a list as value, it iterates over this list and create several blocks. But in your situation there is only one element ["yes"], so it should iterate over yes item and create:

parameter {
  name         = "pga_aggregate_limit"
  value        = "0"
  apply_method = "pending-reboot"
}

To create a parameter when your bool value is FALSE, you should change the condition:

for_each = var.enable_aggregate_limit == false ? ["yes"] : []

or

for_each = var.enable_aggregate_limit ? [] : ["yes"]
Aleksey Kurkov
  • 323
  • 2
  • 13
  • This looks unnecessarily complicated. So that means instead of ["yes"] there could be any string value in the list? – Hedge Aug 03 '22 at 19:25
  • 1
    Yes, you are right. There could be any string for this simple case. But in more complex tasks there could be an `object` containing values, and during the iteration you set these values to your `parameter` values – Aleksey Kurkov Aug 03 '22 at 19:32