-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
189 lines (156 loc) · 4.42 KB
/
main.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
# Use the AWS Provider and specify an AWS Region
provider "aws" {
region = var.aws_region
}
# Set two local variables
locals {
vpc_name = "${var.env_name} ${var.vpc_name}"
cluster_name = "${var.cluster_name}-${var.env_name}"
}
# AWS VPC definition
resource "aws_vpc" "main" {
cidr_block = var.main_vpc_cidr
tags = {
"Name" = local.vpc_name,
"kubernetes.io/cluster/${local.cluster_name}" = "shared",
}
}
# Subnet definition
data "aws_availability_zones" "available" {
state = "available"
}
# Define public subnet a
resource "aws_subnet" "public-subnet-a" {
vpc_id = aws_vpc.main.id
cidr_block = var.public_subnet_a_cidr
availability_zone = data.aws_availability_zones.available.names[0]
tags = {
"Name" = (
"${local.vpc_name}-public-subnet-a"
)
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/elb" = "1"
}
}
# Define public subnet b
resource "aws_subnet" "public-subnet-b" {
vpc_id = aws_vpc.main.id
cidr_block = var.public_subnet_b_cidr
availability_zone = data.aws_availability_zones.available.names[1]
tags = {
"Name" = (
"${local.vpc_name}-public-subnet-b"
)
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/elb" = "1"
}
}
# Define private subnet a
resource "aws_subnet" "private-subnet-a" {
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnet_a_cidr
availability_zone = data.aws_availability_zones.available.names[0]
tags = {
"Name" = (
"${local.vpc_name}-private-subnet-a"
)
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = "1"
}
}
# Define private subnet b
resource "aws_subnet" "private-subnet-b" {
vpc_id = aws_vpc.main.id
cidr_block = var.private_subnet_b_cidr
availability_zone = data.aws_availability_zones.available.names[1]
tags = {
"Name" = (
"${local.vpc_name}-private-subnet-b"
)
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = "1"
}
}
# Internet gateway and routing tables for public subnets
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id
tags = {
Name = "${local.vpc_name}-igw"
}
}
resource "aws_route_table" "public-route" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
"Name" = "${local.vpc_name}-public-route"
}
}
resource "aws_route_table_association" "public-a-association" {
subnet_id = aws_subnet.public-subnet-a.id
route_table_id = aws_route_table.public-route.id
}
resource "aws_route_table_association" "public-b-association" {
subnet_id = aws_subnet.public-subnet-b.id
route_table_id = aws_route_table.public-route.id
}
# Implement NAT gateway
resource "aws_eip" "nat-a" {
domain = "vpc"
tags = {
"Name" = "${local.vpc_name}-NAT-a"
}
}
resource "aws_eip" "nat-b" {
domain = "vpc"
tags = {
"Name" = "${local.vpc_name}-NAT-b"
}
}
resource "aws_nat_gateway" "nat-gw-a" {
allocation_id = aws_eip.nat-a.id
subnet_id = aws_subnet.public-subnet-a.id
depends_on = [aws_internet_gateway.igw]
tags = {
"Name" = "${local.vpc_name}-NAT-gw-a"
}
}
resource "aws_nat_gateway" "nat-gw-b" {
allocation_id = aws_eip.nat-b.id
subnet_id = aws_subnet.public-subnet-b.id
depends_on = [aws_internet_gateway.igw]
tags = {
"Name" = "${local.vpc_name}-NAT-gw-b"
}
}
# Define Private Subnet Routes
resource "aws_route_table" "private-route-a" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat-gw-a.id
}
tags = {
"Name" = "${local.vpc_name}-private-route-a"
}
}
resource "aws_route_table" "private-route-b" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat-gw-b.id
}
tags = {
"Name" = "${local.vpc_name}-private-route-b"
}
}
resource "aws_route_table_association" "private-a-association" {
subnet_id = aws_subnet.private-subnet-a.id
route_table_id = aws_route_table.private-route-a.id
}
resource "aws_route_table_association" "private-b-association" {
subnet_id = aws_subnet.private-subnet-b.id
route_table_id = aws_route_table.private-route-b.id
}