Skip to content

Commit 246cdb3

Browse files
authored
Update network.tf
1 parent fc69142 commit 246cdb3

File tree

1 file changed

+54
-38
lines changed

1 file changed

+54
-38
lines changed

infra/network.tf

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
############################ VPC ############################
22

33
# Create VPC, subnets, route tables, and IGW
4-
data "aws_availability_zones" "available" {
5-
state = "available"
6-
}
7-
8-
locals {
9-
valid_azs = [for az in data.aws_availability_zones.available.names : az if az != "us-west-1a"]
10-
}
11-
124
module "vpc" {
135
source = "terraform-aws-modules/vpc/aws"
146
version = ">= 4.0"
157
name = "${var.project_prefix}-vpc-${random_id.build_suffix.hex}"
168
cidr = var.cidr
17-
azs = local.valid_azs
18-
enable_dns_support = true
9+
azs = var.azs
10+
enable_dns_support = true
1911
enable_dns_hostnames = true
2012
tags = {
2113
resource_owner = var.resource_owner
@@ -31,43 +23,71 @@ resource "aws_internet_gateway" "igw" {
3123
}
3224

3325
module subnet_addrs {
34-
for_each = toset(local.valid_azs)
26+
for_each = toset(var.azs)
3527
source = "hashicorp/subnets/cidr"
3628
version = ">= 1.0.0"
37-
base_cidr_block = cidrsubnet(module.vpc.vpc_cidr_block,4,index(local.valid_azs,each.key))
29+
base_cidr_block = cidrsubnet(module.vpc.vpc_cidr_block,4,index(var.azs,each.key))
30+
/*
31+
VPC CIDR = 10.0.0.0/16
32+
AZ1 = 10.0.0.0/20
33+
AZ2 = 10.0.16.0/20
34+
*/
3835
networks = [
39-
{ name = "management", new_bits = 8 },
40-
{ name = "internal", new_bits = 6 },
41-
{ name = "external", new_bits = 6 },
42-
{ name = "app-cidr", new_bits = 4 }
36+
{
37+
name = "management"
38+
new_bits = 8
39+
#10.0.0.0/28
40+
#10.0.16.0/28
41+
},
42+
{
43+
name = "internal"
44+
new_bits = 6
45+
#10.0.0.64/26
46+
#10.0.16.64/26
47+
},
48+
{
49+
name = "external"
50+
new_bits = 6
51+
#10.0.0.128/26
52+
#10.0.16.128/26
53+
},
54+
{
55+
name = "app-cidr"
56+
new_bits = 4
57+
#10.0.1.0/24 EC2
58+
#10.0.17.0/24 EKS
59+
}
4360
]
4461
}
4562

4663
resource "aws_subnet" "internal" {
47-
for_each = toset(local.valid_azs)
48-
vpc_id = module.vpc.vpc_id
49-
cidr_block = module.subnet_addrs[each.key].network_cidr_blocks["internal"]
64+
for_each = toset(var.azs)
65+
vpc_id = module.vpc.vpc_id
66+
cidr_block = module.subnet_addrs[each.key].network_cidr_blocks["internal"]
5067
availability_zone = each.key
51-
tags = { Name = format("%s-int-subnet-%s", var.project_prefix, each.key) }
68+
tags = {
69+
Name = format("%s-int-subnet-%s",var.project_prefix,each.key)
70+
}
5271
}
53-
5472
resource "aws_subnet" "management" {
55-
for_each = toset(local.valid_azs)
56-
vpc_id = module.vpc.vpc_id
57-
cidr_block = module.subnet_addrs[each.key].network_cidr_blocks["management"]
73+
for_each = toset(var.azs)
74+
vpc_id = module.vpc.vpc_id
75+
cidr_block = module.subnet_addrs[each.key].network_cidr_blocks["management"]
5876
availability_zone = each.key
59-
tags = { Name = format("%s-mgmt-subnet-%s", var.project_prefix, each.key) }
77+
tags = {
78+
Name = format("%s-mgmt-subnet-%s",var.project_prefix,each.key)
79+
}
6080
}
61-
6281
resource "aws_subnet" "external" {
63-
for_each = toset(local.valid_azs)
64-
vpc_id = module.vpc.vpc_id
65-
cidr_block = module.subnet_addrs[each.key].network_cidr_blocks["external"]
82+
for_each = toset(var.azs)
83+
vpc_id = module.vpc.vpc_id
84+
cidr_block = module.subnet_addrs[each.key].network_cidr_blocks["external"]
6685
map_public_ip_on_launch = true
6786
availability_zone = each.key
68-
tags = { Name = format("%s-ext-subnet-%s", var.project_prefix, each.key) }
87+
tags = {
88+
Name = format("%s-ext-subnet-%s",var.project_prefix,each.key)
89+
}
6990
}
70-
7191
resource "aws_route_table" "main" {
7292
vpc_id = module.vpc.vpc_id
7393
route {
@@ -78,22 +98,18 @@ resource "aws_route_table" "main" {
7898
Name = "${var.project_prefix}-rt-${random_id.build_suffix.hex}"
7999
}
80100
}
81-
82101
resource "aws_route_table_association" "subnet-association-internal" {
83-
for_each = toset(local.valid_azs)
102+
for_each = toset(var.azs)
84103
subnet_id = aws_subnet.internal[each.key].id
85104
route_table_id = aws_route_table.main.id
86105
}
87-
88106
resource "aws_route_table_association" "subnet-association-management" {
89-
for_each = toset(local.valid_azs)
107+
for_each = toset(var.azs)
90108
subnet_id = aws_subnet.management[each.key].id
91109
route_table_id = aws_route_table.main.id
92110
}
93-
94111
resource "aws_route_table_association" "subnet-association-external" {
95-
for_each = toset(local.valid_azs)
112+
for_each = toset(var.azs)
96113
subnet_id = aws_subnet.external[each.key].id
97114
route_table_id = aws_route_table.main.id
98115
}
99-

0 commit comments

Comments
 (0)