Eduardo Gimenez
Eduardo Gimenez

Reputation: 85

terraform aws_lb_listener_rule condition

I try to migrate from v0.11 to v0.12 of terraform and I have some troubles with the condition tag.

This it's my resource:

resource "aws_lb_listener_rule" "static" {
  listener_arn = var.alb_int_arn
  priority     = index(var.priority_load, count.index)

  action {
    type             = "forward"
    target_group_arn = aws_alb_target_group.alb_target_group.arn
  }

  count = var.count_path

  condition {
    host_header {
      values = index(var.path_to_service, count.index)
    }
  }
}

var.path_to_service it's a list of string and var.priority_load it's a list of numbers.

My problem it's when I try to apply my terraform files always say me the command the same error:

Inappropriate value for attribute "values": set of string required.

I try to put a string directly and I have the same error.

My IDE (IntelliJ) said me that I have an error into the condition tag when it inspected the code, the error said:

Report blocks with unknown type(first literal)

I maked a test and I changed the condition tag for a block, like this:

condition = {
    host_header {
      values = index(var.path_to_service, count.index)
    }
  }

To terraform this it's a syntactic error, but for my IDE it's a right configuration... obviously not work

Any idea for me?

Upvotes: 1

Views: 3809

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74694

This error is saying that this values argument is expecting a set of strings, but apparently the elements of var.path_to_service are not of that type.

You didn't share the definition of variable "path_to_service", but in order for it to work with this configuration as written it would need to be declared something like this:

variable "path_to_service" {
  type = list(set(string))
}

...and the value assigned to it by the calling module would need to be nested like this:

  path_to_service = [
    ["a", "b"],
    ["c"],
  ]

Since your variable has a singular name, I guess it is more likely that it's a list of individual strings, one per "service":

variable "path_to_service" {
  type = list(string)
}
path_to_service = [
  "a",
  "b",
  "c",
]

If that is true, then you'd need to wrap those individual strings in a one-element set by wrapping the expression in brackets [ ]:

  values = [index(var.path_to_service, count.index)]

While you're updating this for Terraform 0.12 anyway, you could also switch to the new list indexing syntax, which should achieve the same result:

  values = [var.path_to_service[count.index]]

Adding those list brackets with the example variable values I gave above would cause Terraform to understand this as if you had written the following, using "a" just as an example:

  condition {
    host_header {
      values = ["a"]
    }
  }

...which seems to match the type that is expected by that values argument.

Upvotes: 4

Related Questions