Skip to content

Commit b1ec393

Browse files
authored
Update network.tf
1 parent e341183 commit b1ec393

File tree

1 file changed

+38
-54
lines changed

1 file changed

+38
-54
lines changed

infra/network.tf

+38-54
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
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+
412
module "vpc" {
513
source = "terraform-aws-modules/vpc/aws"
614
version = ">= 4.0"
715
name = "${var.project_prefix}-vpc-${random_id.build_suffix.hex}"
816
cidr = var.cidr
9-
azs = var.azs
10-
enable_dns_support = true
17+
azs = local.valid_azs
18+
enable_dns_support = true
1119
enable_dns_hostnames = true
1220
tags = {
1321
resource_owner = var.resource_owner
@@ -23,71 +31,43 @@ resource "aws_internet_gateway" "igw" {
2331
}
2432

2533
module subnet_addrs {
26-
for_each = toset(var.azs)
34+
for_each = toset(local.valid_azs)
2735
source = "hashicorp/subnets/cidr"
2836
version = ">= 1.0.0"
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-
*/
37+
base_cidr_block = cidrsubnet(module.vpc.vpc_cidr_block,4,index(local.valid_azs,each.key))
3538
networks = [
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-
}
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 }
6043
]
6144
}
6245

6346
resource "aws_subnet" "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"]
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"]
6750
availability_zone = each.key
68-
tags = {
69-
Name = format("%s-int-subnet-%s",var.project_prefix,each.key)
70-
}
51+
tags = { Name = format("%s-int-subnet-%s", var.project_prefix, each.key) }
7152
}
53+
7254
resource "aws_subnet" "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"]
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"]
7658
availability_zone = each.key
77-
tags = {
78-
Name = format("%s-mgmt-subnet-%s",var.project_prefix,each.key)
79-
}
59+
tags = { Name = format("%s-mgmt-subnet-%s", var.project_prefix, each.key) }
8060
}
61+
8162
resource "aws_subnet" "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"]
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"]
8566
map_public_ip_on_launch = true
8667
availability_zone = each.key
87-
tags = {
88-
Name = format("%s-ext-subnet-%s",var.project_prefix,each.key)
89-
}
68+
tags = { Name = format("%s-ext-subnet-%s", var.project_prefix, each.key) }
9069
}
70+
9171
resource "aws_route_table" "main" {
9272
vpc_id = module.vpc.vpc_id
9373
route {
@@ -98,18 +78,22 @@ resource "aws_route_table" "main" {
9878
Name = "${var.project_prefix}-rt-${random_id.build_suffix.hex}"
9979
}
10080
}
81+
10182
resource "aws_route_table_association" "subnet-association-internal" {
102-
for_each = toset(var.azs)
83+
for_each = toset(local.valid_azs)
10384
subnet_id = aws_subnet.internal[each.key].id
10485
route_table_id = aws_route_table.main.id
10586
}
87+
10688
resource "aws_route_table_association" "subnet-association-management" {
107-
for_each = toset(var.azs)
89+
for_each = toset(local.valid_azs)
10890
subnet_id = aws_subnet.management[each.key].id
10991
route_table_id = aws_route_table.main.id
11092
}
93+
11194
resource "aws_route_table_association" "subnet-association-external" {
112-
for_each = toset(var.azs)
95+
for_each = toset(local.valid_azs)
11396
subnet_id = aws_subnet.external[each.key].id
11497
route_table_id = aws_route_table.main.id
11598
}
99+

0 commit comments

Comments
 (0)