Dynamic block based on greater than integer

Hi,

I am trying to create a dynamic block based on whether an integer is greater than another number. This is to control the creation of ebs ephemeral_storage in the aws_ecs_task_definition resource.

  dynamic "ephemeral_storage" {
      for_each = var.volume_size > 20 ? [1] : []
      content {
          size_in_gib = var.volume_size
      }
  }
variable "volume_size"{
  type = number
  default = 25
  }

However, when running the plan I see no changes to be made. But in my mind the variable is greater than 20 so should it not create the block?

Hi @fraserc182,

Your for_each expression returns a sequence with one element if the size is greater than 20. That seems to be the opposite of the intention you stated in the following paragraph.

I think the most direct way to get the effect you want is to swap the two result arms of the conditional expression, like this:

  dynamic "ephemeral_storage" {
    for_each = var.volume_size > 20 ? [] : [1]
    content {
      size_in_gib = var.volume_size
    }
  }