Skip to content

Commit e5bf550

Browse files
committed
feat(variables): replace lookup with direct attribute access
Simplify attribute access in `main.tf` by replacing `lookup` calls with direct references to `each.value` properties. Updated `variables.tf` to define explicit object types, improving type safety and clarity for HTTP/HTTPS port variable definitions.
1 parent d0b65a1 commit e5bf550

File tree

3 files changed

+58
-30
lines changed

3 files changed

+58
-30
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 | `map(any)` | <pre>{<br/> "default": {<br/> "listener_port": 80,<br/> "target_group_port": 80,<br/> "target_group_protocol": "HTTP",<br/> "target_group_protocol_version": "HTTP1",<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/> 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/> 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 | `map(any)` | <pre>{<br/> "default": {<br/> "listener_port": 443,<br/> "target_group_port": 443,<br/> "target_group_protocol": "HTTP",<br/> "target_group_protocol_version": "HTTP1",<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/> 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/> 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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ resource "aws_lb_listener" "lb_http_listeners" {
282282
type = "redirect"
283283

284284
redirect {
285-
host = lookup(each.value, "host", "#{host}")
286-
path = lookup(each.value, "path", "/#{path}")
287-
port = lookup(each.value, "port", "#{port}")
288-
protocol = lookup(each.value, "protocol", "#{protocol}")
289-
query = lookup(each.value, "query", "#{query}")
285+
host = each.value.host
286+
path = each.value.path
287+
port = each.value.port
288+
protocol = each.value.protocol
289+
query = each.value.query
290290
status_code = lookup(each.value, "status_code", "HTTP_301")
291291
}
292292
}
@@ -298,8 +298,8 @@ resource "aws_lb_listener" "lb_http_listeners" {
298298
type = "fixed-response"
299299

300300
fixed_response {
301-
content_type = lookup(each.value, "content_type", "text/plain")
302-
message_body = lookup(each.value, "message_body", "Fixed response content")
301+
content_type = each.value.content_type
302+
message_body = each.value.message_body
303303
status_code = lookup(each.value, "status_code", "200")
304304
}
305305
}
@@ -337,11 +337,11 @@ resource "aws_lb_listener" "lb_https_listeners" {
337337
type = "redirect"
338338

339339
redirect {
340-
host = lookup(each.value, "host", "#{host}")
341-
path = lookup(each.value, "path", "/#{path}")
342-
port = lookup(each.value, "port", "#{port}")
343-
protocol = lookup(each.value, "protocol", "#{protocol}")
344-
query = lookup(each.value, "query", "#{query}")
340+
host = each.value.host
341+
path = each.value.path
342+
port = each.value.port
343+
protocol = each.value.protocol
344+
query = each.value.query
345345
status_code = lookup(each.value, "status_code", "HTTP_301")
346346
}
347347
}
@@ -353,8 +353,8 @@ resource "aws_lb_listener" "lb_https_listeners" {
353353
type = "fixed-response"
354354

355355
fixed_response {
356-
content_type = lookup(each.value, "content_type", "text/plain")
357-
message_body = lookup(each.value, "message_body", "Fixed response content")
356+
content_type = each.value.content_type
357+
message_body = each.value.message_body
358358
status_code = lookup(each.value, "status_code", "200")
359359
}
360360
}

variables.tf

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,58 @@ variable "waf_web_acl_arn" {
146146
#------------------------------------------------------------------------------
147147
variable "http_ports" {
148148
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"
149-
type = map(any)
149+
type = map(object({
150+
type = optional(string)
151+
152+
listener_port = number
153+
target_group_port = number
154+
155+
target_group_protocol = optional(string, "HTTP")
156+
target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC
157+
158+
host = optional(string, "#{host}")
159+
path = optional(string, "/#{path}")
160+
port = optional(string, "#{port}")
161+
protocol = optional(string, "#{protocol}")
162+
query = optional(string, "#{query}")
163+
status_code = optional(string) # Default for `type=redirect`: "HTTP_301". Default for `type=fixed-response`: "200".
164+
content_type = optional(string, "text/plain")
165+
message_body = optional(string, "Fixed response content")
166+
}))
150167
default = {
151168
default = {
152-
type = "forward"
153-
listener_port = 80
154-
target_group_port = 80
155-
target_group_protocol = "HTTP"
156-
# HTTP1, HTTP2 or GRPC
157-
target_group_protocol_version = "HTTP1"
169+
type = "forward"
170+
listener_port = 80
171+
target_group_port = 80
158172
}
159173
}
160174
}
161175

162176
variable "https_ports" {
163177
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"
164-
type = map(any)
178+
type = map(object({
179+
type = optional(string)
180+
181+
listener_port = number
182+
target_group_port = number
183+
184+
target_group_protocol = optional(string, "HTTP")
185+
target_group_protocol_version = optional(string, "HTTP1") # HTTP1, HTTP2 or GRPC
186+
187+
host = optional(string, "#{host}")
188+
path = optional(string, "/#{path}")
189+
port = optional(string, "#{port}")
190+
protocol = optional(string, "#{protocol}")
191+
query = optional(string, "#{query}")
192+
status_code = optional(string) # Default for `type=redirect`: "HTTP_301". Default for `type=fixed-response`: "200".
193+
content_type = optional(string, "text/plain")
194+
message_body = optional(string, "Fixed response content")
195+
}))
165196
default = {
166197
default = {
167-
type = "forward"
168-
listener_port = 443
169-
target_group_port = 443
170-
target_group_protocol = "HTTP"
171-
# HTTP1, HTTP2 or GRPC
172-
target_group_protocol_version = "HTTP1"
198+
type = "forward"
199+
listener_port = 443
200+
target_group_port = 443
173201
}
174202
}
175203
}

0 commit comments

Comments
 (0)