Skip to content

Commit 95e6dc2

Browse files
authored
Fix IPv6 ingress rules (#186)
1 parent dcd2064 commit 95e6dc2

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ Available targets:
234234
| <a name="input_health_check_unhealthy_threshold"></a> [health\_check\_unhealthy\_threshold](#input\_health\_check\_unhealthy\_threshold) | The number of consecutive health check failures required before considering the target unhealthy | `number` | `2` | no |
235235
| <a name="input_http2_enabled"></a> [http2\_enabled](#input\_http2\_enabled) | A boolean flag to enable/disable HTTP/2 | `bool` | `true` | no |
236236
| <a name="input_http_enabled"></a> [http\_enabled](#input\_http\_enabled) | A boolean flag to enable/disable HTTP listener | `bool` | `true` | no |
237-
| <a name="input_http_ingress_cidr_blocks"></a> [http\_ingress\_cidr\_blocks](#input\_http\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTP security group | `list(string)` | <pre>[<br/> "0.0.0.0/0"<br/>]</pre> | no |
237+
| <a name="input_http_ingress_cidr_blocks"></a> [http\_ingress\_cidr\_blocks](#input\_http\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTP security group | `list(string)` | <pre>[<br/> "0.0.0.0/0",<br/> "::/0"<br/>]</pre> | no |
238238
| <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 for allowing access to HTTP ingress security group | `list(string)` | `[]` | no |
239239
| <a name="input_http_port"></a> [http\_port](#input\_http\_port) | The port for the HTTP listener | `number` | `80` | no |
240240
| <a name="input_http_redirect"></a> [http\_redirect](#input\_http\_redirect) | A boolean flag to enable/disable HTTP redirect to HTTPS | `bool` | `false` | no |
241241
| <a name="input_https_enabled"></a> [https\_enabled](#input\_https\_enabled) | A boolean flag to enable/disable HTTPS listener | `bool` | `false` | no |
242-
| <a name="input_https_ingress_cidr_blocks"></a> [https\_ingress\_cidr\_blocks](#input\_https\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTPS security group | `list(string)` | <pre>[<br/> "0.0.0.0/0"<br/>]</pre> | no |
242+
| <a name="input_https_ingress_cidr_blocks"></a> [https\_ingress\_cidr\_blocks](#input\_https\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTPS security group | `list(string)` | <pre>[<br/> "0.0.0.0/0",<br/> "::/0"<br/>]</pre> | no |
243243
| <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 for allowing access to HTTPS ingress security group | `list(string)` | `[]` | no |
244244
| <a name="input_https_port"></a> [https\_port](#input\_https\_port) | The port for the HTTPS listener | `number` | `443` | no |
245245
| <a name="input_https_ssl_policy"></a> [https\_ssl\_policy](#input\_https\_ssl\_policy) | The name of the SSL Policy for the listener | `string` | `"ELBSecurityPolicy-2015-05"` | no |

docs/terraform.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@
7272
| <a name="input_health_check_unhealthy_threshold"></a> [health\_check\_unhealthy\_threshold](#input\_health\_check\_unhealthy\_threshold) | The number of consecutive health check failures required before considering the target unhealthy | `number` | `2` | no |
7373
| <a name="input_http2_enabled"></a> [http2\_enabled](#input\_http2\_enabled) | A boolean flag to enable/disable HTTP/2 | `bool` | `true` | no |
7474
| <a name="input_http_enabled"></a> [http\_enabled](#input\_http\_enabled) | A boolean flag to enable/disable HTTP listener | `bool` | `true` | no |
75-
| <a name="input_http_ingress_cidr_blocks"></a> [http\_ingress\_cidr\_blocks](#input\_http\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTP security group | `list(string)` | <pre>[<br/> "0.0.0.0/0"<br/>]</pre> | no |
75+
| <a name="input_http_ingress_cidr_blocks"></a> [http\_ingress\_cidr\_blocks](#input\_http\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTP security group | `list(string)` | <pre>[<br/> "0.0.0.0/0",<br/> "::/0"<br/>]</pre> | no |
7676
| <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 for allowing access to HTTP ingress security group | `list(string)` | `[]` | no |
7777
| <a name="input_http_port"></a> [http\_port](#input\_http\_port) | The port for the HTTP listener | `number` | `80` | no |
7878
| <a name="input_http_redirect"></a> [http\_redirect](#input\_http\_redirect) | A boolean flag to enable/disable HTTP redirect to HTTPS | `bool` | `false` | no |
7979
| <a name="input_https_enabled"></a> [https\_enabled](#input\_https\_enabled) | A boolean flag to enable/disable HTTPS listener | `bool` | `false` | no |
80-
| <a name="input_https_ingress_cidr_blocks"></a> [https\_ingress\_cidr\_blocks](#input\_https\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTPS security group | `list(string)` | <pre>[<br/> "0.0.0.0/0"<br/>]</pre> | no |
80+
| <a name="input_https_ingress_cidr_blocks"></a> [https\_ingress\_cidr\_blocks](#input\_https\_ingress\_cidr\_blocks) | List of CIDR blocks to allow in HTTPS security group | `list(string)` | <pre>[<br/> "0.0.0.0/0",<br/> "::/0"<br/>]</pre> | no |
8181
| <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 for allowing access to HTTPS ingress security group | `list(string)` | `[]` | no |
8282
| <a name="input_https_port"></a> [https\_port](#input\_https\_port) | The port for the HTTPS listener | `number` | `443` | no |
8383
| <a name="input_https_ssl_policy"></a> [https\_ssl\_policy](#input\_https\_ssl\_policy) | The name of the SSL Policy for the listener | `string` | `"ELBSecurityPolicy-2015-05"` | no |
@@ -94,7 +94,7 @@
9494
| <a name="input_listener_additional_tags"></a> [listener\_additional\_tags](#input\_listener\_additional\_tags) | The additional tags to apply to all listeners | `map(string)` | `{}` | no |
9595
| <a name="input_listener_http_fixed_response"></a> [listener\_http\_fixed\_response](#input\_listener\_http\_fixed\_response) | Have the HTTP listener return a fixed response for the default action. | <pre>object({<br/> content_type = string<br/> message_body = string<br/> status_code = string<br/> })</pre> | `null` | no |
9696
| <a name="input_listener_https_fixed_response"></a> [listener\_https\_fixed\_response](#input\_listener\_https\_fixed\_response) | Have the HTTPS listener return a fixed response for the default action. | <pre>object({<br/> content_type = string<br/> message_body = string<br/> status_code = string<br/> })</pre> | `null` | no |
97-
| <a name="input_listener_https_redirect"></a> [listener\_https\_redirect](#input\_listener\_https\_redirect) | Have the HTTPS listener return a redirect response for the default action. | <pre>object({<br/> host = optional(string)<br/> path = optional(string)<br/> port = optional(string)<br/> protocol = optional(string)<br/> query = optional(string)<br/> status_code = optional(string)<br/> })</pre> | `null` | no |
97+
| <a name="input_listener_https_redirect"></a> [listener\_https\_redirect](#input\_listener\_https\_redirect) | Have the HTTPS listener return a redirect response for the default action. | <pre>object({<br/> host = optional(string)<br/> path = optional(string)<br/> port = optional(string)<br/> protocol = optional(string)<br/> query = optional(string)<br/> status_code = string<br/> })</pre> | <pre>{<br/> "host": null,<br/> "path": null,<br/> "port": null,<br/> "protocol": null,<br/> "query": null,<br/> "status_code": "HTTP_301"<br/>}</pre> | no |
9898
| <a name="input_load_balancer_name"></a> [load\_balancer\_name](#input\_load\_balancer\_name) | The name for the default load balancer, uses a module label name if left empty | `string` | `""` | no |
9999
| <a name="input_load_balancer_name_max_length"></a> [load\_balancer\_name\_max\_length](#input\_load\_balancer\_name\_max\_length) | The max length of characters for the load balancer. | `number` | `32` | no |
100100
| <a name="input_load_balancing_algorithm_type"></a> [load\_balancing\_algorithm\_type](#input\_load\_balancing\_algorithm\_type) | Determines how the load balancer selects targets when routing requests. Only applicable for Application Load Balancer Target Groups | `string` | `"round_robin"` | no |

main.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
locals {
2+
# cidrnetmask returns an error for IPv6 addresses
3+
# cidrhost works with both IPv4 and IPv6, and returns an error if the argument is not a valid IPv4/IPv6 CIDR prefix
4+
http_ingress_cidr_blocks_v4 = [for cidr in var.http_ingress_cidr_blocks : cidr if can(cidrnetmask(cidr))]
5+
http_ingress_cidr_blocks_v6 = var.ip_address_type == "dualstack" ? [for cidr in var.http_ingress_cidr_blocks : cidr if !can(cidrnetmask(cidr)) && can(cidrhost(cidr, 0))] : []
6+
https_ingress_cidr_blocks_v4 = [for cidr in var.https_ingress_cidr_blocks : cidr if can(cidrnetmask(cidr))]
7+
https_ingress_cidr_blocks_v6 = var.ip_address_type == "dualstack" ? [for cidr in var.https_ingress_cidr_blocks : cidr if !can(cidrnetmask(cidr)) && can(cidrhost(cidr, 0))] : []
8+
}
9+
10+
111
resource "aws_security_group" "default" {
212
count = module.this.enabled && var.security_group_enabled ? 1 : 0
313
description = "Controls access to the ALB (HTTP/HTTPS)"
@@ -22,7 +32,8 @@ resource "aws_security_group_rule" "http_ingress" {
2232
from_port = var.http_port
2333
to_port = var.http_port
2434
protocol = "tcp"
25-
cidr_blocks = var.http_ingress_cidr_blocks
35+
cidr_blocks = local.http_ingress_cidr_blocks_v4
36+
ipv6_cidr_blocks = local.http_ingress_cidr_blocks_v6
2637
prefix_list_ids = var.http_ingress_prefix_list_ids
2738
security_group_id = one(aws_security_group.default[*].id)
2839
}
@@ -33,7 +44,8 @@ resource "aws_security_group_rule" "https_ingress" {
3344
from_port = var.https_port
3445
to_port = var.https_port
3546
protocol = "tcp"
36-
cidr_blocks = var.https_ingress_cidr_blocks
47+
cidr_blocks = local.https_ingress_cidr_blocks_v4
48+
ipv6_cidr_blocks = local.https_ingress_cidr_blocks_v6
3749
prefix_list_ids = var.https_ingress_prefix_list_ids
3850
security_group_id = one(aws_security_group.default[*].id)
3951
}

variables.tf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@ variable "http_redirect" {
4040

4141
variable "http_ingress_cidr_blocks" {
4242
type = list(string)
43-
default = ["0.0.0.0/0"]
43+
default = ["0.0.0.0/0", "::/0"]
4444
description = "List of CIDR blocks to allow in HTTP security group"
45+
46+
validation {
47+
condition = alltrue([for cidr in var.http_ingress_cidr_blocks : can(cidrhost(cidr, 0))])
48+
error_message = "Each entry in http_ingress_cidr_blocks must be a valid CIDR block."
49+
}
4550
}
4651

4752
variable "http_ingress_prefix_list_ids" {
@@ -70,8 +75,13 @@ variable "https_enabled" {
7075

7176
variable "https_ingress_cidr_blocks" {
7277
type = list(string)
73-
default = ["0.0.0.0/0"]
78+
default = ["0.0.0.0/0", "::/0"]
7479
description = "List of CIDR blocks to allow in HTTPS security group"
80+
81+
validation {
82+
condition = alltrue([for cidr in var.https_ingress_cidr_blocks : can(cidrhost(cidr, 0))])
83+
error_message = "Each entry in https_ingress_cidr_blocks must be a valid CIDR block."
84+
}
7585
}
7686

7787
variable "https_ingress_prefix_list_ids" {
@@ -132,6 +142,11 @@ variable "ip_address_type" {
132142
type = string
133143
default = "ipv4"
134144
description = "The type of IP addresses used by the subnets for your load balancer. The possible values are `ipv4` and `dualstack`."
145+
146+
validation {
147+
condition = contains(["ipv4", "dualstack"], var.ip_address_type)
148+
error_message = "ip_address_type must be either `ipv4` or `dualstack`."
149+
}
135150
}
136151

137152
variable "deletion_protection_enabled" {

0 commit comments

Comments
 (0)