The main idea is to create the same rules but for different listeners.
I have stuck on var.listener_rules_http_redirect.listener_arn
. How to process because of each.value.listener_arn
doesn't work.
Could anyone share fresh ideas, please?
Error:
│ Error: Incorrect attribute value type
│
│ on modules/aws/alb/lb_listener_rule/03-main.tf line 4, in resource "aws_lb_listener_rule" "www_redirect":
│ 4: listener_arn = each.value.listener_arn
│ ├────────────────
│ │ each.value.listener_arn is tuple with 1 element
│
│ Inappropriate value for attribute "listener_arn": string required.
Module variables
module "lb_listener_rule_http" {
source = "./modules/aws/alb/lb_listener_rule"
listener_arn = module.alb_lb_listener.frontend_http
tags = merge(local.tags, { Name = var.domain })
domain_name_public = var.domain
listener_rules_http_redirect = {
// redirect www to non www
1 = {
listener_arn = [
{
http = module.alb_lb_listener.frontend_http,
https = module.alb_lb_listener.frontend_https
}
],
action = [
{
"type" = "redirect",
"target_group_arn" = module.target_group.arns[local.target_group_name_wp],
}
],
redirect = [
{
host = var.domain
path = "/#{path}"
query = "#{query}"
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
]
conditions = [
{
"field" = "host-header"
"values" = ["www.${var.domain}"]
}
]
},
}
}
module code
resource "aws_lb_listener_rule" "www_redirect" {
for_each = var.listener_rules_http_redirect
listener_arn = each.value.listener_arn
priority = each.key
dynamic "action" {
for_each = each.value["action"]
content {
type = action.value.type
dynamic "redirect" {
for_each = each.value["redirect"]
content {
host = redirect.value.host
path = redirect.value.path
query = redirect.value.query
port = redirect.value.port
protocol = redirect.value.protocol
status_code = redirect.value.status_code
}
}
}
}
dynamic "condition" {
for_each = each.value["conditions"]
content {
dynamic "host_header" {
for_each = condition.value.field == "host-header" ? [condition.value.field] : []
content {
values = condition.value.values
}
}
dynamic "path_pattern" {
for_each = condition.value.field == "path-pattern" ? [condition.value.field] : []
content {
values = condition.value.values
}
}
dynamic "source_ip" {
for_each = condition.value.field == "source-ip" ? [condition.value.field] : []
content {
values = condition.value.values
}
}
}
}
}
Vars
variable "listener_rules_http_redirect" {
type = map(any)
default = {}
}