-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
202 lines (170 loc) · 4.19 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
provider "aws" {
region = var.region
access_key = var.access_key
secret_key = var.secret_key
}
#****** VPC Start ******#
resource "aws_vpc" "my_custom_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "K8S VPC"
}
}
resource "aws_subnet" "my_public_subnet" {
vpc_id = aws_vpc.my_custom_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "eu-central-1a"
tags = {
Name = "K8S Subnet"
}
}
resource "aws_internet_gateway" "my_ig" {
vpc_id = aws_vpc.my_custom_vpc.id
tags = {
Name = "K8S Internet Gateway"
}
}
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.my_custom_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_ig.id
}
route {
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.my_ig.id
}
tags = {
Name = "Public Route Table"
}
}
resource "aws_route_table_association" "public_1_rt_a" {
subnet_id = aws_subnet.my_public_subnet.id
route_table_id = aws_route_table.public_rt.id
}
resource "aws_security_group" "k8s_sg" {
name = "K8S Ports"
vpc_id = aws_vpc.my_custom_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 6443
to_port = 6443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 2379
to_port = 2380
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 10250
to_port = 10250
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 30000
to_port = 32767
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
}
#****** VPC END ******#
resource "random_string" "s3name" {
length = 9
special = false
upper = false
lower = true
}
resource "aws_s3_bucket_acl" "s3_bucket_acl" {
bucket = aws_s3_bucket.s3buckit.id
acl = "private"
depends_on = [aws_s3_bucket_ownership_controls.s3_bucket_acl_ownership]
}
resource "aws_s3_bucket_ownership_controls" "s3_bucket_acl_ownership" {
bucket = aws_s3_bucket.s3buckit.id
rule {
object_ownership = "ObjectWriter"
}
}
resource "aws_s3_bucket" "s3buckit" {
bucket = "k8s-${random_string.s3name.result}"
force_destroy = true
depends_on = [
random_string.s3name
]
}
resource "aws_instance" "ec2_instance_msr" {
ami = var.ami_id
subnet_id = aws_subnet.my_public_subnet.id
instance_type = var.instance_type
key_name = var.ami_key_pair_name
associate_public_ip_address = true
security_groups = [ aws_security_group.k8s_sg.id ]
root_block_device {
volume_type = "gp2"
volume_size = "16"
delete_on_termination = true
}
tags = {
Name = "k8s_msr_1"
}
user_data_base64 = base64encode("${templatefile("install_k8s_control-plane.sh", {
access_key = var.access_key
private_key = var.secret_key
region = var.region
s3buckit_name = "k8s-${random_string.s3name.result}"
})}")
depends_on = [
aws_s3_bucket.s3buckit,
random_string.s3name
]
}
resource "aws_instance" "ec2_instance_worker-node" {
ami = var.ami_id
count = var.number_of_worker
subnet_id = aws_subnet.my_public_subnet.id
instance_type = var.instance_type
key_name = var.ami_key_pair_name
associate_public_ip_address = true
security_groups = [ aws_security_group.k8s_sg.id ]
root_block_device {
volume_type = "gp2"
volume_size = "16"
delete_on_termination = true
}
tags = {
Name = "k8s_worker-node_${count.index + 1}"
}
user_data_base64 = base64encode("${templatefile("install_k8s_worker-node.sh", {
access_key = var.access_key
private_key = var.secret_key
region = var.region
s3buckit_name = "k8s-${random_string.s3name.result}"
worker_number = "${count.index + 1}"
})}")
depends_on = [
aws_s3_bucket.s3buckit,
random_string.s3name,
aws_instance.ec2_instance_control-plane
]
}