Skip to content

Commit 19c9733

Browse files
committed
use availability zones to determine subnets cidr blocks
1 parent 0ed5bb9 commit 19c9733

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

main.tf

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
data "aws_region" "current" {}
1718

1819
# VPC
1920
resource "aws_vpc" "quortex" {
@@ -38,13 +39,22 @@ resource "aws_default_security_group" "quortex" {
3839
}
3940

4041
resource "aws_vpc_ipv4_cidr_block_association" "secondary" {
41-
for_each = var.vpc_secondary_cidrs
42+
for_each = toset([for index, az in var.availability_zones : cidrsubnet(var.vpc_secondary_cidr, 2, index)])
4243
vpc_id = aws_vpc.quortex.id
4344
cidr_block = each.value
4445
}
4546

4647
resource "aws_subnet" "quortex" {
47-
for_each = var.subnets
48+
for_each = merge([
49+
for key, subnet in var.subnets : {
50+
for index, az in var.availability_zones : "${key}-${data.aws_region.current.name}${az}" => {
51+
"availability_zone" = "${data.aws_region.current.name}${az}",
52+
"cidr" = cidrsubnet(subnet.cidr, 2, index),
53+
"public" = subnet.public,
54+
"tags" = subnet.tags,
55+
}
56+
}
57+
]...)
4858

4959
vpc_id = aws_vpc.quortex.id
5060
availability_zone = each.value.availability_zone

variables.tf

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ variable "vpc_name" {
2020
default = "quortex"
2121
}
2222

23-
variable "vpc_secondary_cidrs" {
24-
type = set(string)
25-
description = "IPv4 secondary CIDRs to add to the VPC."
26-
default = []
27-
}
28-
2923
variable "cluster_name" {
3024
type = string
3125
description = "The name of the EKS cluster. Will be used to set the kubernetes.io/cluster/<cluster-name> tag on the VPC and subnets. It is required for Kubernetes to discover them."
@@ -71,10 +65,10 @@ variable "vpc_cidr_block" {
7165
}
7266

7367
variable "subnets" {
74-
type = map(object({ availability_zone = string, cidr = string, public = bool, tags = optional(map(string), {}) }))
68+
type = map(object({ cidr = string, public = bool, tags = optional(map(string), {}) }))
7569
description = <<EOT
7670
A map representing the subnets that need to be created. Each item should
77-
specify the subnet's Availability Zone, cidr block, whether the subnet
71+
specify the subnet's cidr block, whether the subnet
7872
should be public or not and optionally extra tags to add.
7973
EOT
8074
}
@@ -96,3 +90,14 @@ variable "tags" {
9690
description = "The tags (a map of key/value pairs) to be applied to created resources."
9791
default = {}
9892
}
93+
94+
variable "vpc_secondary_cidr" {
95+
type = string
96+
description = "IPv4 secondary CIDR to add to the VPC."
97+
default = "100.64.0.0/16"
98+
}
99+
100+
variable "availability_zones" {
101+
type = list(string)
102+
description = "The availability zones to use."
103+
}

0 commit comments

Comments
 (0)