Skip to content

Commit b2f894c

Browse files
authored
use availability zones to determine subnets cidr blocks (#32)
* use availability zones to determine subnets cidr blocks * remove vpc_secondary_cidr default value * calculate newbits dynamically based on the number of availability zones * update pre-commit hooks version
1 parent 0ed5bb9 commit b2f894c

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

Diff for: .github/workflows/checks.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@ jobs:
2323
uses: actions/checkout@v3
2424

2525
- uses: actions/setup-python@v3
26+
with:
27+
python-version: "3.10"
28+
29+
- name: Setup Terraform
30+
uses: hashicorp/setup-terraform@v3
31+
with:
32+
terraform_version: "^1.5.0"
33+
terraform_wrapper: false
2634

2735
- uses: pre-commit/[email protected]

Diff for: .pre-commit-config.yaml

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.82.0
3+
rev: v1.96.3
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate
77
- id: terraform_docs
88
args:
9-
- '--args=--lockfile=false'
10-
- '--hook-config=--path-to-file=README.md'
11-
- '--hook-config=--create-file-if-not-exist=true'
9+
- "--args=--lockfile=false"
10+
- "--hook-config=--path-to-file=README.md"
11+
- "--hook-config=--create-file-if-not-exist=true"
1212
- id: terraform_tflint
1313
args:
14-
- '--args=--only=terraform_deprecated_interpolation'
15-
- '--args=--only=terraform_deprecated_index'
16-
- '--args=--only=terraform_unused_declarations'
17-
- '--args=--only=terraform_comment_syntax'
18-
- '--args=--only=terraform_documented_outputs'
19-
- '--args=--only=terraform_documented_variables'
20-
- '--args=--only=terraform_typed_variables'
21-
- '--args=--only=terraform_module_pinned_source'
22-
- '--args=--only=terraform_naming_convention'
23-
- '--args=--only=terraform_required_version'
24-
- '--args=--only=terraform_required_providers'
25-
- '--args=--only=terraform_standard_module_structure'
26-
- '--args=--only=terraform_workspace_remote'
14+
- "--args=--only=terraform_deprecated_interpolation"
15+
- "--args=--only=terraform_deprecated_index"
16+
- "--args=--only=terraform_unused_declarations"
17+
- "--args=--only=terraform_comment_syntax"
18+
- "--args=--only=terraform_documented_outputs"
19+
- "--args=--only=terraform_documented_variables"
20+
- "--args=--only=terraform_typed_variables"
21+
- "--args=--only=terraform_module_pinned_source"
22+
- "--args=--only=terraform_naming_convention"
23+
- "--args=--only=terraform_required_version"
24+
- "--args=--only=terraform_required_providers"
25+
- "--args=--only=terraform_standard_module_structure"
26+
- "--args=--only=terraform_workspace_remote"
2727
- repo: https://github.com/pre-commit/pre-commit-hooks
2828
rev: v4.4.0
2929
hooks:

Diff for: main.tf

+21-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17+
data "aws_region" "current" {}
18+
19+
locals {
20+
# Calculate newbits dynamically based on the number of availability zones
21+
# 1 or 2 AZs, newbits = 1 (2 subnets needed).
22+
# 3 or 4 AZs, newbits = 2 (4 subnets needed).
23+
# 5 to 8 AZs, newbits = 3 (8 subnets needed).
24+
# and so on...
25+
newbits = ceil(log(length(var.availability_zones), 2))
26+
}
1727

1828
# VPC
1929
resource "aws_vpc" "quortex" {
@@ -38,13 +48,22 @@ resource "aws_default_security_group" "quortex" {
3848
}
3949

4050
resource "aws_vpc_ipv4_cidr_block_association" "secondary" {
41-
for_each = var.vpc_secondary_cidrs
51+
for_each = toset([for index, az in var.availability_zones : cidrsubnet(var.vpc_secondary_cidr, local.newbits, index)])
4252
vpc_id = aws_vpc.quortex.id
4353
cidr_block = each.value
4454
}
4555

4656
resource "aws_subnet" "quortex" {
47-
for_each = var.subnets
57+
for_each = merge([
58+
for key, subnet in var.subnets : {
59+
for index, az in var.availability_zones : "${key}-${data.aws_region.current.name}${az}" => {
60+
"availability_zone" = "${data.aws_region.current.name}${az}",
61+
"cidr" = cidrsubnet(subnet.cidr, local.newbits, index),
62+
"public" = subnet.public,
63+
"tags" = subnet.tags,
64+
}
65+
}
66+
]...)
4867

4968
vpc_id = aws_vpc.quortex.id
5069
availability_zone = each.value.availability_zone

Diff for: variables.tf

+12-8
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,13 @@ 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+
}
98+
99+
variable "availability_zones" {
100+
type = list(string)
101+
description = "The availability zones to use."
102+
}

0 commit comments

Comments
 (0)