-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
171 lines (138 loc) · 3.87 KB
/
vpc.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
locals {
workload_subnets = {
az_base = 0 * var.redundancy_factor
ip_base = 10
}
management_subnets = {
az_base = 1 * var.redundancy_factor
ip_base = 20
}
public_subnets = {
az_base = 2 * var.redundancy_factor
ip_base = 30
}
zones_count = length(data.aws_availability_zones.available.zone_ids)
}
resource "aws_vpc" "this" {
cidr_block = var.aws_vpc_cidr_block
tags = {
Name = "HomeAssignment"
}
}
resource "aws_internet_gateway" "this" {
vpc_id = aws_vpc.this.id
tags = {
Name = "GW_HomeAssignment"
}
}
resource "aws_route" "igw" {
route_table_id = aws_vpc.this.main_route_table_id
gateway_id = aws_internet_gateway.this.id
destination_cidr_block = "0.0.0.0/0"
}
resource "aws_subnet" "workload" {
count = var.redundancy_factor
vpc_id = aws_vpc.this.id
cidr_block = "10.1.${local.workload_subnets.ip_base + count.index}.0/24"
availability_zone = data.aws_availability_zones.available.names[
(local.workload_subnets.az_base + count.index) % local.zones_count
]
tags = {
Name = "Workload"
}
}
resource "aws_eip" "nat_workload" {
vpc = true
tags = {
Name = "nat_workload_ip"
}
depends_on = [aws_internet_gateway.this]
}
resource "aws_nat_gateway" "workload" {
allocation_id = aws_eip.nat_workload.id
subnet_id = aws_subnet.public[0].id
tags = {
Name = "nat_workload"
}
depends_on = [aws_internet_gateway.this]
}
resource "aws_route_table" "workload_rt" {
vpc_id = aws_vpc.this.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.workload.id
}
tags = {
Name = "workload_rt"
}
}
resource "aws_route_table_association" "workload" {
count = length(aws_subnet.workload)
subnet_id = aws_subnet.workload[count.index].id
route_table_id = aws_route_table.workload_rt.id
}
resource "aws_subnet" "management" {
count = var.redundancy_factor
vpc_id = aws_vpc.this.id
cidr_block = "10.1.${local.management_subnets.ip_base + count.index}.0/24"
availability_zone = data.aws_availability_zones.available.names[
(local.management_subnets.az_base + count.index) % local.zones_count
]
map_public_ip_on_launch = true
tags = {
Name = "Management"
}
}
resource "aws_subnet" "public" {
count = var.redundancy_factor
vpc_id = aws_vpc.this.id
cidr_block = "10.1.${local.public_subnets.ip_base + count.index}.0/24"
availability_zone = data.aws_availability_zones.available.names[
(local.public_subnets.az_base + count.index) % local.zones_count
]
map_public_ip_on_launch = true
tags = {
Name = "Public"
}
}
resource "aws_network_acl" "management" {
vpc_id = aws_vpc.this.id
subnet_ids = aws_subnet.management[*].id
}
resource "aws_network_acl_rule" "vpc_to_mgmt" {
network_acl_id = aws_network_acl.management.id
rule_number = 100
egress = false
protocol = "all"
rule_action = "allow"
cidr_block = aws_vpc.this.cidr_block
}
resource "aws_network_acl_rule" "mgmt_to_vpc" {
network_acl_id = aws_network_acl.management.id
rule_number = 100
egress = true
protocol = "all"
rule_action = "allow"
cidr_block = aws_vpc.this.cidr_block
}
resource "aws_network_acl_rule" "management_access" {
count = length(var.management_access_list)
network_acl_id = aws_network_acl.management.id
rule_number = 200 + 10 * count.index
egress = false
protocol = "all"
rule_action = "allow"
cidr_block = var.management_access_list[count.index]
}
# resource "aws_network_acl_rule" "allow_all_from_mgmt" {
# count = length(var.management_access_list)
# network_acl_id = aws_network_acl.management.id
# rule_number = 100
# egress = false
# protocol = "all"
# rule_action = "allow"
# cidr_block = "0.0.0.0/0"
# }
output "nat_gateway_workload" {
value = aws_eip.nat_workload.public_ip
}