Skip to content

Commit 88f3c73

Browse files
authored
Make target_group_port optional, if type not forward (#68)
1 parent 694a7f4 commit 88f3c73

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ In order to run all checks at any point run the following command:
9393
| <a name="input_enable_s3_logs"></a> [enable\_s3\_logs](#input\_enable\_s3\_logs) | (Optional) If true, all LoadBalancer logs will be sent to S3. If true, and log\_bucket\_id is *not* provided, this module will create the bucket with other provided s3 bucket configuration options | `bool` | `true` | no |
9494
| <a name="input_http_ingress_cidr_blocks"></a> [http\_ingress\_cidr\_blocks](#input\_http\_ingress\_cidr\_blocks) | List of CIDR blocks to allowed to access the Load Balancer through HTTP | `list(string)` | <pre>[<br/> "0.0.0.0/0"<br/>]</pre> | no |
9595
| <a name="input_http_ingress_prefix_list_ids"></a> [http\_ingress\_prefix\_list\_ids](#input\_http\_ingress\_prefix\_list\_ids) | List of prefix list IDs blocks to allowed to access the Load Balancer through HTTP | `list(string)` | `[]` | no |
96-
| <a name="input_http_ports"></a> [http\_ports](#input\_http\_ports) | Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener\_port and the target\_group\_port. For `redirect` type, include listener port, host, path, port, protocol, query and status\_code. For `fixed-response`, include listener\_port, content\_type, message\_body and status\_code | <pre>map(object({<br/> type = optional(string)<br/><br/> listener_port = number<br/> target_group_port = number<br/><br/> target_group_protocol = optional(string, "HTTP")<br/> target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC<br/><br/> # Health check options, overriding default values provided as module variables<br/> target_group_health_check_enabled = optional(bool)<br/> target_group_health_check_interval = optional(number)<br/> target_group_health_check_path = optional(string)<br/> target_group_health_check_port = optional(string)<br/> target_group_health_check_protocol = optional(string, "HTTP")<br/> target_group_health_check_timeout = optional(number)<br/> target_group_health_check_healthy_threshold = optional(number)<br/> target_group_health_check_unhealthy_threshold = optional(number)<br/> target_group_health_check_matcher = optional(string)<br/><br/> host = optional(string, "#{host}")<br/> path = optional(string, "/#{path}")<br/> port = optional(string, "#{port}")<br/> protocol = optional(string, "#{protocol}")<br/> query = optional(string, "#{query}")<br/> status_code = optional(string) # Default for `type=redirect`: "HTTP_301". Default for `type=fixed-response`: "200".<br/> content_type = optional(string, "text/plain")<br/> message_body = optional(string, "Fixed response content")<br/> }))</pre> | <pre>{<br/> "default": {<br/> "listener_port": 80,<br/> "target_group_port": 80,<br/> "type": "forward"<br/> }<br/>}</pre> | no |
96+
| <a name="input_http_ports"></a> [http\_ports](#input\_http\_ports) | Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener\_port and the target\_group\_port. For `redirect` type, include listener\_port, host, path, port, protocol, query and status\_code. For `fixed-response`, include listener\_port, content\_type, message\_body and status\_code | <pre>map(object({<br/> type = optional(string)<br/><br/> listener_port = number<br/><br/> target_group_port = optional(number)<br/> target_group_protocol = optional(string, "HTTP")<br/> target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC<br/><br/> # Health check options, overriding default values provided as module variables<br/> target_group_health_check_enabled = optional(bool)<br/> target_group_health_check_interval = optional(number)<br/> target_group_health_check_path = optional(string)<br/> target_group_health_check_port = optional(string)<br/> target_group_health_check_protocol = optional(string, "HTTP")<br/> target_group_health_check_timeout = optional(number)<br/> target_group_health_check_healthy_threshold = optional(number)<br/> target_group_health_check_unhealthy_threshold = optional(number)<br/> target_group_health_check_matcher = optional(string)<br/><br/> host = optional(string, "#{host}")<br/> path = optional(string, "/#{path}")<br/> port = optional(string, "#{port}")<br/> protocol = optional(string, "#{protocol}")<br/> query = optional(string, "#{query}")<br/> status_code = optional(string) # Default for `type=redirect`: "HTTP_301". Default for `type=fixed-response`: "200".<br/> content_type = optional(string, "text/plain")<br/> message_body = optional(string, "Fixed response content")<br/> }))</pre> | <pre>{<br/> "default": {<br/> "listener_port": 80,<br/> "target_group_port": 80,<br/> "type": "forward"<br/> }<br/>}</pre> | no |
9797
| <a name="input_https_ingress_cidr_blocks"></a> [https\_ingress\_cidr\_blocks](#input\_https\_ingress\_cidr\_blocks) | List of CIDR blocks to allowed to access the Load Balancer through HTTPS | `list(string)` | <pre>[<br/> "0.0.0.0/0"<br/>]</pre> | no |
9898
| <a name="input_https_ingress_prefix_list_ids"></a> [https\_ingress\_prefix\_list\_ids](#input\_https\_ingress\_prefix\_list\_ids) | List of prefix list IDs blocks to allowed to access the Load Balancer through HTTPS | `list(string)` | `[]` | no |
99-
| <a name="input_https_ports"></a> [https\_ports](#input\_https\_ports) | Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener\_port and the target\_group\_port. For `redirect` type, include listener port, host, path, port, protocol, query and status\_code. For `fixed-response`, include listener\_port, content\_type, message\_body and status\_code | <pre>map(object({<br/> type = optional(string)<br/><br/> listener_port = number<br/> target_group_port = number<br/><br/> target_group_protocol = optional(string, "HTTP")<br/> target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC<br/><br/> # Health check options, overriding default values provided as module variables<br/> target_group_health_check_enabled = optional(bool)<br/> target_group_health_check_interval = optional(number)<br/> target_group_health_check_path = optional(string)<br/> target_group_health_check_port = optional(string)<br/> target_group_health_check_protocol = optional(string, "HTTP")<br/> target_group_health_check_timeout = optional(number)<br/> target_group_health_check_healthy_threshold = optional(number)<br/> target_group_health_check_unhealthy_threshold = optional(number)<br/> target_group_health_check_matcher = optional(string)<br/><br/> host = optional(string, "#{host}")<br/> path = optional(string, "/#{path}")<br/> port = optional(string, "#{port}")<br/> protocol = optional(string, "#{protocol}")<br/> query = optional(string, "#{query}")<br/> status_code = optional(string) # Default for `type=redirect`: "HTTP_301". Default for `type=fixed-response`: "200".<br/> content_type = optional(string, "text/plain")<br/> message_body = optional(string, "Fixed response content")<br/> }))</pre> | <pre>{<br/> "default": {<br/> "listener_port": 443,<br/> "target_group_port": 443,<br/> "type": "forward"<br/> }<br/>}</pre> | no |
99+
| <a name="input_https_ports"></a> [https\_ports](#input\_https\_ports) | Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener\_port and the target\_group\_port. For `redirect` type, include listener\_port, host, path, port, protocol, query and status\_code. For `fixed-response`, include listener\_port, content\_type, message\_body and status\_code | <pre>map(object({<br/> type = optional(string)<br/><br/> listener_port = number<br/><br/> target_group_port = optional(number)<br/> target_group_protocol = optional(string, "HTTP")<br/> target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC<br/><br/> # Health check options, overriding default values provided as module variables<br/> target_group_health_check_enabled = optional(bool)<br/> target_group_health_check_interval = optional(number)<br/> target_group_health_check_path = optional(string)<br/> target_group_health_check_port = optional(string)<br/> target_group_health_check_protocol = optional(string, "HTTP")<br/> target_group_health_check_timeout = optional(number)<br/> target_group_health_check_healthy_threshold = optional(number)<br/> target_group_health_check_unhealthy_threshold = optional(number)<br/> target_group_health_check_matcher = optional(string)<br/><br/> host = optional(string, "#{host}")<br/> path = optional(string, "/#{path}")<br/> port = optional(string, "#{port}")<br/> protocol = optional(string, "#{protocol}")<br/> query = optional(string, "#{query}")<br/> status_code = optional(string) # Default for `type=redirect`: "HTTP_301". Default for `type=fixed-response`: "200".<br/> content_type = optional(string, "text/plain")<br/> message_body = optional(string, "Fixed response content")<br/> }))</pre> | <pre>{<br/> "default": {<br/> "listener_port": 443,<br/> "target_group_port": 443,<br/> "type": "forward"<br/> }<br/>}</pre> | no |
100100
| <a name="input_idle_timeout"></a> [idle\_timeout](#input\_idle\_timeout) | (Optional) The time in seconds that the connection is allowed to be idle. Default: 60. | `number` | `60` | no |
101101
| <a name="input_internal"></a> [internal](#input\_internal) | (Optional) If true, the LB will be internal. | `bool` | `false` | no |
102102
| <a name="input_ip_address_type"></a> [ip\_address\_type](#input\_ip\_address\_type) | (Optional) The type of IP addresses used by the subnets for your load balancer. The possible values are ipv4 and dualstack. Defaults to ipv4 | `string` | `"ipv4"` | no |

main.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ resource "aws_security_group_rule" "ingress_through_https" {
118118
resource "null_resource" "lb_http_tgs_config" {
119119
for_each = {
120120
for name, config in var.http_ports : name => config
121-
if config.type == null || config.type == "forward"
121+
if(config.type == null || config.type == "forward") && config.target_group_port != null
122122
}
123123

124124
triggers = {
@@ -131,7 +131,7 @@ resource "null_resource" "lb_http_tgs_config" {
131131
resource "random_id" "lb_http_tgs_id" {
132132
for_each = {
133133
for name, config in var.http_ports : name => config
134-
if config.type == null || config.type == "forward"
134+
if(config.type == null || config.type == "forward") && config.target_group_port != null
135135
}
136136

137137
byte_length = 2
@@ -147,7 +147,7 @@ resource "random_id" "lb_http_tgs_id" {
147147
resource "aws_lb_target_group" "lb_http_tgs" {
148148
for_each = {
149149
for name, config in var.http_ports : name => config
150-
if config.type == null || config.type == "forward"
150+
if(config.type == null || config.type == "forward") && config.target_group_port != null
151151
}
152152
name = "${var.name_prefix}-http-${each.value.target_group_port}-${random_id.lb_http_tgs_id[each.key].hex}"
153153
port = each.value.target_group_port
@@ -194,7 +194,7 @@ resource "aws_lb_target_group" "lb_http_tgs" {
194194
resource "null_resource" "lb_https_tgs_config" {
195195
for_each = {
196196
for name, config in var.https_ports : name => config
197-
if config.type == null || config.type == "forward"
197+
if(config.type == null || config.type == "forward") && config.target_group_port != null
198198
}
199199

200200
triggers = {
@@ -207,7 +207,7 @@ resource "null_resource" "lb_https_tgs_config" {
207207
resource "random_id" "lb_https_tgs_id" {
208208
for_each = {
209209
for name, config in var.https_ports : name => config
210-
if config.type == null || config.type == "forward"
210+
if(config.type == null || config.type == "forward") && config.target_group_port != null
211211
}
212212

213213
byte_length = 2
@@ -223,7 +223,7 @@ resource "random_id" "lb_https_tgs_id" {
223223
resource "aws_lb_target_group" "lb_https_tgs" {
224224
for_each = {
225225
for name, config in var.https_ports : name => config
226-
if config.type == null || config.type == "forward"
226+
if(config.type == null || config.type == "forward") && config.target_group_port != null
227227
}
228228
name = "${var.name_prefix}-https-${each.value.target_group_port}-${random_id.lb_https_tgs_id[each.key].hex}"
229229
port = each.value.target_group_port

variables.tf

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ variable "waf_web_acl_arn" {
145145
# ACCESS CONTROL TO APPLICATION LOAD BALANCER
146146
#------------------------------------------------------------------------------
147147
variable "http_ports" {
148-
description = "Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener_port and the target_group_port. For `redirect` type, include listener port, host, path, port, protocol, query and status_code. For `fixed-response`, include listener_port, content_type, message_body and status_code"
148+
description = "Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener_port and the target_group_port. For `redirect` type, include listener_port, host, path, port, protocol, query and status_code. For `fixed-response`, include listener_port, content_type, message_body and status_code"
149149
type = map(object({
150150
type = optional(string)
151151

152-
listener_port = number
153-
target_group_port = number
152+
listener_port = number
154153

154+
target_group_port = optional(number)
155155
target_group_protocol = optional(string, "HTTP")
156156
target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC
157157

@@ -182,16 +182,24 @@ variable "http_ports" {
182182
target_group_port = 80
183183
}
184184
}
185+
validation {
186+
condition = alltrue([for _, v in var.http_ports : v.type != "forward" || v.target_group_port != null])
187+
error_message = "target_group_port must be set if type is forward"
188+
}
189+
validation {
190+
condition = alltrue([for _, v in var.http_ports : (v.type == "redirect" || v.type == "fixed-response") ? v.status_code != null : true])
191+
error_message = "status_code must be set if type is redirect or fixed-response"
192+
}
185193
}
186194

187195
variable "https_ports" {
188-
description = "Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener_port and the target_group_port. For `redirect` type, include listener port, host, path, port, protocol, query and status_code. For `fixed-response`, include listener_port, content_type, message_body and status_code"
196+
description = "Map containing objects to define listeners behaviour based on type field. If type field is `forward`, include listener_port and the target_group_port. For `redirect` type, include listener_port, host, path, port, protocol, query and status_code. For `fixed-response`, include listener_port, content_type, message_body and status_code"
189197
type = map(object({
190198
type = optional(string)
191199

192-
listener_port = number
193-
target_group_port = number
200+
listener_port = number
194201

202+
target_group_port = optional(number)
195203
target_group_protocol = optional(string, "HTTP")
196204
target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC
197205

@@ -222,6 +230,14 @@ variable "https_ports" {
222230
target_group_port = 443
223231
}
224232
}
233+
validation {
234+
condition = alltrue([for _, v in var.https_ports : v.type != "forward" || v.target_group_port != null])
235+
error_message = "target_group_port must be set if type is forward"
236+
}
237+
validation {
238+
condition = alltrue([for _, v in var.https_ports : (v.type == "redirect" || v.type == "fixed-response") ? v.status_code != null : true])
239+
error_message = "status_code must be set if type is redirect or fixed-response"
240+
}
225241
}
226242

227243
/*

0 commit comments

Comments
 (0)