Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add listener rules support for http/tcp listeners #216

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ No modules.
| [aws_lb_listener.frontend_http_tcp](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener) | resource |
| [aws_lb_listener.frontend_https](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener) | resource |
| [aws_lb_listener_certificate.https_listener](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener_certificate) | resource |
| [aws_lb_listener_rule.http_tcp_listener_rule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener_rule) | resource |
| [aws_lb_listener_rule.https_listener_rule](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener_rule) | resource |
| [aws_lb_target_group.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource |
| [aws_lb_target_group_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group_attachment) | resource |
Expand All @@ -328,8 +329,10 @@ No modules.
| <a name="input_enable_deletion_protection"></a> [enable\_deletion\_protection](#input\_enable\_deletion\_protection) | If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer. Defaults to false. | `bool` | `false` | no |
| <a name="input_enable_http2"></a> [enable\_http2](#input\_enable\_http2) | Indicates whether HTTP/2 is enabled in application load balancers. | `bool` | `true` | no |
| <a name="input_extra_ssl_certs"></a> [extra\_ssl\_certs](#input\_extra\_ssl\_certs) | A list of maps describing any extra SSL certificates to apply to the HTTPS listeners. Required key/values: certificate\_arn, https\_listener\_index (the index of the listener within https\_listeners which the cert applies toward). | `list(map(string))` | `[]` | no |
| <a name="input_http_tcp_listener_rules"></a> [http\_tcp\_listener\_rules](#input\_http\_tcp\_listener\_rules) | A list of maps describing the Listener Rules for this ALB. Required key/values: actions, conditions. Optional key/values: priority, http\_tcp\_listener\_index (default to http\_tcp\_listeners[count.index]) | `any` | `[]` | no |
| <a name="input_http_tcp_listener_rules_tags"></a> [http\_tcp\_listener\_rules\_tags](#input\_http\_tcp\_listener\_rules\_tags) | A map of tags to add to all http listener rules | `map(string)` | `{}` | no |
| <a name="input_http_tcp_listeners"></a> [http\_tcp\_listeners](#input\_http\_tcp\_listeners) | A list of maps describing the HTTP listeners or TCP ports for this ALB. Required key/values: port, protocol. Optional key/values: target\_group\_index (defaults to http\_tcp\_listeners[count.index]) | `any` | `[]` | no |
| <a name="input_http_tcp_listeners_tags"></a> [http\_tcp\_listeners\_tags](#input\_http\_tcp\_listeners\_tags) | A map of tags to add to all tcp listeners | `map(string)` | `{}` | no |
| <a name="input_http_tcp_listeners_tags"></a> [http\_tcp\_listeners\_tags](#input\_http\_tcp\_listeners\_tags) | A map of tags to add to all http listeners | `map(string)` | `{}` | no |
| <a name="input_https_listener_rules"></a> [https\_listener\_rules](#input\_https\_listener\_rules) | A list of maps describing the Listener Rules for this ALB. Required key/values: actions, conditions. Optional key/values: priority, https\_listener\_index (default to https\_listeners[count.index]) | `any` | `[]` | no |
| <a name="input_https_listener_rules_tags"></a> [https\_listener\_rules\_tags](#input\_https\_listener\_rules\_tags) | A map of tags to add to all https listener rules | `map(string)` | `{}` | no |
| <a name="input_https_listeners"></a> [https\_listeners](#input\_https\_listeners) | A list of maps describing the HTTPS listeners for this ALB. Required key/values: port, certificate\_arn. Optional key/values: ssl\_policy (defaults to ELBSecurityPolicy-2016-08), target\_group\_index (defaults to https\_listeners[count.index]) | `any` | `[]` | no |
Expand Down
39 changes: 39 additions & 0 deletions examples/complete-alb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,45 @@ module "alb" {
},
]

http_tcp_listener_rules = [
{
http_tcp_listener_index = 0
priority = 3
actions = [{
type = "fixed-response"
content_type = "text/plain"
status_code = 200
message_body = "This is a fixed response"
}]

conditions = [{
http_headers = [{
http_header_name = "x-Gimme-Fixed-Response"
values = ["yes", "please", "right now"]
}]
}]
},
{
http_tcp_listener_index = 0
priority = 5000
actions = [{
type = "redirect"
status_code = "HTTP_302"
host = "www.youtube.com"
path = "/watch"
query = "v=dQw4w9WgXcQ"
protocol = "HTTPS"
}]

conditions = [{
query_strings = [{
key = "video"
value = "random"
}]
}]
},
]

target_groups = [
{
name_prefix = "h1"
Expand Down
166 changes: 166 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,172 @@ resource "aws_lb_listener_rule" "https_listener_rule" {
)
}

resource "aws_lb_listener_rule" "http_tcp_listener_rule" {
count = var.create_lb ? length(var.http_tcp_listener_rules) : 0

listener_arn = aws_lb_listener.frontend_http_tcp[lookup(var.http_tcp_listener_rules[count.index], "http_tcp_listener_index", count.index)].arn
priority = lookup(var.http_tcp_listener_rules[count.index], "priority", null)

# redirect actions
dynamic "action" {
for_each = [
for action_rule in var.http_tcp_listener_rules[count.index].actions :
action_rule
if action_rule.type == "redirect"
]

content {
type = action.value["type"]
redirect {
host = lookup(action.value, "host", null)
path = lookup(action.value, "path", null)
port = lookup(action.value, "port", null)
protocol = lookup(action.value, "protocol", null)
query = lookup(action.value, "query", null)
status_code = action.value["status_code"]
}
}
}

# fixed-response actions
dynamic "action" {
for_each = [
for action_rule in var.http_tcp_listener_rules[count.index].actions :
action_rule
if action_rule.type == "fixed-response"
]

content {
type = action.value["type"]
fixed_response {
message_body = lookup(action.value, "message_body", null)
status_code = lookup(action.value, "status_code", null)
content_type = action.value["content_type"]
}
}
}

# forward actions
dynamic "action" {
for_each = [
for action_rule in var.http_tcp_listener_rules[count.index].actions :
action_rule
if action_rule.type == "forward"
]

content {
type = action.value["type"]
target_group_arn = aws_lb_target_group.main[lookup(action.value, "target_group_index", count.index)].id
}
}

# Path Pattern condition
dynamic "condition" {
for_each = [
for condition_rule in var.http_tcp_listener_rules[count.index].conditions :
condition_rule
if length(lookup(condition_rule, "path_patterns", [])) > 0
]

content {
path_pattern {
values = condition.value["path_patterns"]
}
}
}

# Host header condition
dynamic "condition" {
for_each = [
for condition_rule in var.http_tcp_listener_rules[count.index].conditions :
condition_rule
if length(lookup(condition_rule, "host_headers", [])) > 0
]

content {
host_header {
values = condition.value["host_headers"]
}
}
}

# Http header condition
dynamic "condition" {
for_each = [
for condition_rule in var.http_tcp_listener_rules[count.index].conditions :
condition_rule
if length(lookup(condition_rule, "http_headers", [])) > 0
]

content {
dynamic "http_header" {
for_each = condition.value["http_headers"]

content {
http_header_name = http_header.value["http_header_name"]
values = http_header.value["values"]
}
}
}
}

# Http request method condition
dynamic "condition" {
for_each = [
for condition_rule in var.http_tcp_listener_rules[count.index].conditions :
condition_rule
if length(lookup(condition_rule, "http_request_methods", [])) > 0
]

content {
http_request_method {
values = condition.value["http_request_methods"]
}
}
}

# Query string condition
dynamic "condition" {
for_each = [
for condition_rule in var.http_tcp_listener_rules[count.index].conditions :
condition_rule
if length(lookup(condition_rule, "query_strings", [])) > 0
]

content {
dynamic "query_string" {
for_each = condition.value["query_strings"]

content {
key = lookup(query_string.value, "key", null)
value = query_string.value["value"]
}
}
}
}

# Source IP address condition
dynamic "condition" {
for_each = [
for condition_rule in var.http_tcp_listener_rules[count.index].conditions :
condition_rule
if length(lookup(condition_rule, "source_ips", [])) > 0
]

content {
source_ip {
values = condition.value["source_ips"]
}
}
}

tags = merge(
var.tags,
var.http_tcp_listener_rules_tags,
lookup(var.http_tcp_listener_rules[count.index], "tags", {}),
)
}

resource "aws_lb_listener" "frontend_http_tcp" {
count = var.create_lb ? length(var.http_tcp_listeners) : 0

Expand Down
14 changes: 13 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ variable "https_listener_rules" {
default = []
}

variable "http_tcp_listener_rules" {
description = "A list of maps describing the Listener Rules for this ALB. Required key/values: actions, conditions. Optional key/values: priority, http_tcp_listener_index (default to http_tcp_listeners[count.index])"
type = any
default = []
}

variable "idle_timeout" {
description = "The time in seconds that the connection is allowed to be idle."
type = number
Expand Down Expand Up @@ -154,14 +160,20 @@ variable "https_listener_rules_tags" {
default = {}
}

variable "http_tcp_listener_rules_tags" {
description = "A map of tags to add to all http listener rules"
type = map(string)
default = {}
}

variable "https_listeners_tags" {
description = "A map of tags to add to all https listeners"
type = map(string)
default = {}
}

variable "http_tcp_listeners_tags" {
description = "A map of tags to add to all tcp listeners"
description = "A map of tags to add to all http listeners"
type = map(string)
default = {}
}
Expand Down