-
Notifications
You must be signed in to change notification settings - Fork 10
/
010_vpc.tf
229 lines (186 loc) · 5.62 KB
/
010_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// create vpc
//Base VPC Networking
//EKS requires the usage of Virtual Private Cloud to provide the base for its networking configuration.
resource "aws_vpc" "cluster" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
tags = merge(
local.common_tags,
{
"Name" = "${var.CLUSTER_NAME}-eks-vpc"
},
)
}
//The below will create a ${var.public_subnet_cidr} VPC,
//two ${var.public_subnet_cidr} public subnets,
//two ${var.private_subnet_cidr} private subnets with nat instances,
//an internet gateway,
//and setup the subnet routing to route external traffic through the internet gateway:
// public subnets
resource "aws_subnet" "eks-public" {
vpc_id = aws_vpc.cluster.id
cidr_block = var.public_subnet_cidr
availability_zone = local.availabilityzone
tags = merge(
local.common_tags,
{
"Name" = "${var.CLUSTER_NAME}-eks-public"
},
)
}
resource "aws_subnet" "eks-public-2" {
vpc_id = aws_vpc.cluster.id
cidr_block = var.public_subnet_cidr2
availability_zone = local.availabilityzone2
tags = merge(
local.common_tags,
{
"Name" = "${var.CLUSTER_NAME}-eks-public-2"
},
)
}
// private subnet
resource "aws_subnet" "eks-private" {
vpc_id = aws_vpc.cluster.id
cidr_block = var.private_subnet_cidr
availability_zone = local.availabilityzone
map_public_ip_on_launch = var.use_simple_vpc
tags = merge(
local.common_tags,
{
"Name" = "${var.CLUSTER_NAME}-eks-private"
},
)
}
resource "aws_subnet" "eks-private-2" {
vpc_id = aws_vpc.cluster.id
cidr_block = var.private_subnet_cidr2
availability_zone = local.availabilityzone2
map_public_ip_on_launch = var.use_simple_vpc
tags = merge(
local.common_tags,
{
"Name" = "${var.CLUSTER_NAME}-eks-private-2"
},
)
}
// internet gateway, note: creation takes a while
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.cluster.id
tags = {
Environment = var.CLUSTER_NAME
}
}
// reserve elastic ip for nat gateway
resource "aws_eip" "nat_eip" {
count = var.use_simple_vpc ? 0 : 1
vpc = true
tags = {
Environment = var.CLUSTER_NAME
}
}
resource "aws_eip" "nat_eip_2" {
count = var.use_simple_vpc ? 0 : 1
vpc = true
tags = {
Environment = var.CLUSTER_NAME
}
}
// create nat once internet gateway created
resource "aws_nat_gateway" "nat_gateway" {
count = var.use_simple_vpc ? 0 : 1
allocation_id = aws_eip.nat_eip.0.id
subnet_id = aws_subnet.eks-public.id
depends_on = [aws_internet_gateway.igw]
tags = {
Environment = var.CLUSTER_NAME
}
}
resource "aws_nat_gateway" "nat_gateway_2" {
count = var.use_simple_vpc ? 0 : 1
allocation_id = aws_eip.nat_eip_2.0.id
subnet_id = aws_subnet.eks-public-2.id
depends_on = [aws_internet_gateway.igw]
tags = {
Environment = var.CLUSTER_NAME
}
}
//Create private route table and the route to the internet
//This will allow all traffics from the private subnets to the internet through the NAT Gateway (Network Address Translation)
resource "aws_route_table" "private_route_table" {
vpc_id = aws_vpc.cluster.id
tags = {
Environment = var.CLUSTER_NAME
Name = "${var.CLUSTER_NAME}-private-route-table"
}
}
resource "aws_route_table" "private_route_table_2" {
vpc_id = aws_vpc.cluster.id
tags = {
Environment = var.CLUSTER_NAME
Name = "${var.CLUSTER_NAME}-private-route-table-2"
}
}
resource "aws_route" "private_route_nat" {
count = var.use_simple_vpc ? 0 : 1
route_table_id = aws_route_table.private_route_table.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gateway.0.id
}
resource "aws_route" "private_route_2_nat" {
count = var.use_simple_vpc ? 0 : 1
route_table_id = aws_route_table.private_route_table_2.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gateway_2.0.id
}
resource "aws_route" "private_route_igw" {
count = var.use_simple_vpc ? 1 : 0
route_table_id = aws_route_table.private_route_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
resource "aws_route" "private_route_2_igw" {
count = var.use_simple_vpc ? 1 : 0
route_table_id = aws_route_table.private_route_table_2.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
resource "aws_route_table" "eks-public" {
vpc_id = aws_vpc.cluster.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Environment = var.CLUSTER_NAME
Name = "${var.CLUSTER_NAME}-eks-public"
}
}
resource "aws_route_table" "eks-public-2" {
vpc_id = aws_vpc.cluster.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Environment = var.CLUSTER_NAME
Name = "${var.CLUSTER_NAME}-eks-public-2"
}
}
// associate route tables
resource "aws_route_table_association" "eks-public" {
subnet_id = aws_subnet.eks-public.id
route_table_id = aws_route_table.eks-public.id
}
resource "aws_route_table_association" "eks-public-2" {
subnet_id = aws_subnet.eks-public-2.id
route_table_id = aws_route_table.eks-public-2.id
}
resource "aws_route_table_association" "eks-private" {
subnet_id = aws_subnet.eks-private.id
route_table_id = aws_route_table.private_route_table.id
}
resource "aws_route_table_association" "eks-private-2" {
subnet_id = aws_subnet.eks-private-2.id
route_table_id = aws_route_table.private_route_table_2.id
}