I’m trying to create a module to create several listener rules in a balancer, the balancer is already created, and it works correctly, but when creating the module I get an error regarding the RedirectConfig, but I don’t understand why it asks for this value
Terraform v1.3.7
on windows_amd64
+ provider registry.terraform.io/hashicorp/aws v4.36.0
+ provider registry.terraform.io/hashicorp/local v2.2.3
+ provider registry.terraform.io/hashicorp/random v3.4.3
+ provider registry.terraform.io/hashicorp/template v2.2.0
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.lb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "invalid route"
status_code = 503
}
}
}
resource "aws_alb_listener_rule" "auth_rule" {
listener_arn = aws_lb_listener.front_end.arn
priority = 101
for_each = var.listener_rules
dynamic "action" {
for_each = each.value.action
content {
type = lookup(action.value, "type", {})
dynamic "redirect" {
for_each = lookup(action.value, "redirect", {})
content {
port = lookup(redirect.value.action, "port")
protocol = lookup(redirect.value.action, "protocol")
status_code = lookup(redirect.value.action, "status_code")
}
}
}
}
condition {
path_pattern {
values = ["/api/*"]
}
}
condition {
host_header {
values = ["domain.com"]
}
}
}
variable "listener_rules" {
type = map(object({
priority = number
action = map(object({
type = string
status_code = string
protocol = string
port = string
}))
}))
}
test.tfvars
listener_rules = {
"auth" = {
priority = 101
action = {
rules = ({
type = "redirect"
status_code = "503"
port = "80"
protocol = "HTTP"
})
}
},
"core" = {
priority = 101
action = {
rules = ({
type = "redirect"
status_code = "503"
port = "80"
protocol = "HTTP"
})
}
}
}
$ terraform validate
Success! The configuration is valid.
terraform plan -var-file=test.tfvars
Terraform will perform the following actions:
# module.lb.aws_alb_listener_rule.auth_rule["auth"] will be created
+ resource "aws_alb_listener_rule" "auth_rule" {
+ arn = (known after apply)
+ id = (known after apply)
+ listener_arn = "arn:aws:elasticloadbalancing:us-east-1:xxxxxxx:listener/app/xxxxxxxx"
+ priority = 101
+ tags_all = (known after apply)
+ action {
+ order = (known after apply)
+ type = "redirect"
}
+ condition {
+ host_header {
+ values = [
+ "domain.com",
]
}
}
+ condition {
+ path_pattern {
+ values = [
+ "/api/*",
]
}
}
}
# module.lb.aws_alb_listener_rule.auth_rule["core"] will be created
+ resource "aws_alb_listener_rule" "auth_rule" {
+ arn = (known after apply)
+ id = (known after apply)
+ listener_arn = "arn:aws:elasticloadbalancing:us-east-1:xxxxx:listener/app/xxxxx/xxxxxxx"
+ priority = 101
+ tags_all = (known after apply)
+ action {
+ order = (known after apply)
+ type = "redirect"
}
+ condition {
+ host_header {
+ values = [
+ "domain",
]
}
}
+ condition {
+ path_pattern {
+ values = [
+ "/api/*",
]
}
}
}
terraform apply -var-file=test.tfvars
│ Error: Error creating LB Listener Rule: InvalidLoadBalancerAction: For actions of type 'redirect', you must specify
the following fields: 'RedirectConfig'
│ status code: 400, request id: 66d433f7-a431-4d18-9020-310f6f871911
│
│ with module.lb.aws_alb_listener_rule.auth_rule["core"],
│ on modules\lb\main.tf line 138, in resource "aws_alb_listener_rule" "auth_rule":
│ 138: resource "aws_alb_listener_rule" "auth_rule" {
│
╵
╷
│ Error: Error creating LB Listener Rule: InvalidLoadBalancerAction: For actions of type 'redirect', you must specify
the following fields: 'RedirectConfig'
│ status code: 400, request id: 4da7ee97-e945-434b-abdf-02efd56edc7e
│
│ with module.lb.aws_alb_listener_rule.auth_rule["auth"],
│ on modules\lb\main.tf line 138, in resource "aws_alb_listener_rule" "auth_rule":
│ 138: resource "aws_alb_listener_rule" "auth_rule" {
│
I have created the resource with hardcoded code and I do not get errors regarding the RedirectConfig…