-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathmain.tf
373 lines (306 loc) · 9.48 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#####
# AWS Prodvider
#####
# Retrieve AWS credentials from env variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
provider "aws" {
region = var.aws_region
}
#####
# Generate kubeadm token
#####
module "kubeadm-token" {
source = "scholzj/kubeadm-token/random"
}
#####
# IAM roles
#####
# Master nodes
resource "aws_iam_policy" "master_policy" {
name = "${var.cluster_name}-master"
path = "/"
description = "Policy for role ${var.cluster_name}-master"
policy = file("${path.module}/template/master-policy.json.tpl")
}
resource "aws_iam_role" "master_role" {
name = "${var.cluster_name}-master"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy_attachment" "master-attach" {
name = "master-attachment"
roles = [aws_iam_role.master_role.name]
policy_arn = aws_iam_policy.master_policy.arn
}
resource "aws_iam_instance_profile" "master_profile" {
name = "${var.cluster_name}-master"
role = aws_iam_role.master_role.name
}
# Worker nodes
resource "aws_iam_policy" "node_policy" {
name = "${var.cluster_name}-node"
path = "/"
description = "Policy for role ${var.cluster_name}-node"
policy = file("${path.module}/template/node-policy.json.tpl")
}
resource "aws_iam_role" "node_role" {
name = "${var.cluster_name}-node"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_policy_attachment" "node-attach" {
name = "node-attachment"
roles = [aws_iam_role.node_role.name]
policy_arn = aws_iam_policy.node_policy.arn
}
resource "aws_iam_instance_profile" "node_profile" {
name = "${var.cluster_name}-node"
role = aws_iam_role.node_role.name
}
#####
# Security Group
#####
# Find VPC details based on Master subnet
data "aws_subnet" "cluster_subnet" {
id = var.master_subnet_id
}
resource "aws_security_group" "kubernetes" {
vpc_id = data.aws_subnet.cluster_subnet.vpc_id
name = var.cluster_name
tags = merge(
{
"Name" = var.cluster_name
format("kubernetes.io/cluster/%v", var.cluster_name) = "owned"
},
var.tags,
)
}
# Allow outgoing connectivity
resource "aws_security_group_rule" "allow_all_outbound_from_kubernetes" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.kubernetes.id
}
# Allow SSH connections only from specific CIDR (TODO)
resource "aws_security_group_rule" "allow_ssh_from_cidr" {
count = length(var.ssh_access_cidr)
type = "ingress"
from_port = 22
to_port = 22
protocol = "tcp"
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibilty in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
cidr_blocks = [var.ssh_access_cidr[count.index]]
security_group_id = aws_security_group.kubernetes.id
}
# Allow the security group members to talk with each other without restrictions
resource "aws_security_group_rule" "allow_cluster_crosstalk" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
source_security_group_id = aws_security_group.kubernetes.id
security_group_id = aws_security_group.kubernetes.id
}
# Allow API connections only from specific CIDR (TODO)
resource "aws_security_group_rule" "allow_api_from_cidr" {
count = length(var.api_access_cidr)
type = "ingress"
from_port = 6443
to_port = 6443
protocol = "tcp"
# TF-UPGRADE-TODO: In Terraform v0.10 and earlier, it was sometimes necessary to
# force an interpolation expression to be interpreted as a list by wrapping it
# in an extra set of list brackets. That form was supported for compatibilty in
# v0.11, but is no longer supported in Terraform v0.12.
#
# If the expression in the following list itself returns a list, remove the
# brackets to avoid interpretation as a list of lists. If the expression
# returns a single list item then leave it as-is and remove this TODO comment.
cidr_blocks = [var.api_access_cidr[count.index]]
security_group_id = aws_security_group.kubernetes.id
}
##########
# Bootstraping scripts
##########
data "cloudinit_config" "master_cloud_init" {
gzip = true
base64_encode = true
part {
filename = "init-aws-kubernete-master.sh"
content_type = "text/x-shellscript"
content = templatefile("${path.module}/scripts/init-aws-kubernetes-master.sh", { kubeadm_token = module.kubeadm-token.token, dns_name = "${var.cluster_name}.${var.hosted_zone}", ip_address = aws_eip.master.public_ip, cluster_name = var.cluster_name, addons = join(" ", var.addons), aws_region = var.aws_region, asg_name = "${var.cluster_name}-nodes", asg_min_nodes = var.min_worker_count, asg_max_nodes = var.max_worker_count, aws_subnets = join(" ", concat(var.worker_subnet_ids, [var.master_subnet_id])) } )
}
}
data "cloudinit_config" "node_cloud_init" {
gzip = true
base64_encode = true
part {
filename = "init-aws-kubernetes-node.sh"
content_type = "text/x-shellscript"
content = templatefile("${path.module}/scripts/init-aws-kubernetes-node.sh", { kubeadm_token = module.kubeadm-token.token, master_ip = aws_eip.master.public_ip, master_private_ip = aws_instance.master.private_ip, dns_name = "${var.cluster_name}.${var.hosted_zone}" } )
}
}
##########
# Keypair
##########
resource "aws_key_pair" "keypair" {
key_name = var.cluster_name
public_key = file(var.ssh_public_key)
}
#####
# AMI image
#####
data "aws_ami" "centos7" {
most_recent = true
owners = ["aws-marketplace"]
filter {
name = "product-code"
values = ["aw0evgkw8e5c1q413zgy5pjce", "cvugziknvmxgqna9noibqnnsy"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
#####
# Master - EC2 instance
#####
resource "aws_eip" "master" {
vpc = true
}
resource "aws_instance" "master" {
instance_type = var.master_instance_type
ami = data.aws_ami.centos7.id
key_name = aws_key_pair.keypair.key_name
subnet_id = var.master_subnet_id
associate_public_ip_address = false
vpc_security_group_ids = [
aws_security_group.kubernetes.id,
]
iam_instance_profile = aws_iam_instance_profile.master_profile.name
user_data = data.cloudinit_config.master_cloud_init.rendered
tags = merge(
{
"Name" = join("-", [var.cluster_name, "master"])
format("kubernetes.io/cluster/%v", var.cluster_name) = "owned"
},
var.tags,
)
root_block_device {
volume_type = "gp2"
volume_size = "50"
delete_on_termination = true
}
lifecycle {
ignore_changes = [
ami,
user_data,
associate_public_ip_address,
]
}
}
resource "aws_eip_association" "master_assoc" {
instance_id = aws_instance.master.id
allocation_id = aws_eip.master.id
}
#####
# Nodes
#####
resource "aws_launch_configuration" "nodes" {
name_prefix = "${var.cluster_name}-nodes-"
image_id = data.aws_ami.centos7.id
instance_type = var.worker_instance_type
key_name = aws_key_pair.keypair.key_name
iam_instance_profile = aws_iam_instance_profile.node_profile.name
security_groups = [
aws_security_group.kubernetes.id,
]
associate_public_ip_address = var.public_worker
user_data = data.cloudinit_config.node_cloud_init.rendered
root_block_device {
volume_type = "gp2"
volume_size = "50"
delete_on_termination = true
}
lifecycle {
create_before_destroy = true
ignore_changes = [user_data]
}
}
resource "aws_autoscaling_group" "nodes" {
vpc_zone_identifier = var.worker_subnet_ids
name = "${var.cluster_name}-nodes"
max_size = var.max_worker_count
min_size = var.min_worker_count
desired_capacity = var.min_worker_count
launch_configuration = aws_launch_configuration.nodes.name
tags = concat(
[{
key = "kubernetes.io/cluster/${var.cluster_name}"
value = "owned"
propagate_at_launch = true
},
{
key = "Name"
value = "${var.cluster_name}-node"
propagate_at_launch = true
}],
var.tags2,
)
lifecycle {
ignore_changes = [desired_capacity]
}
}
#####
# DNS record
#####
data "aws_route53_zone" "dns_zone" {
name = "${var.hosted_zone}."
private_zone = var.hosted_zone_private
}
resource "aws_route53_record" "master" {
zone_id = data.aws_route53_zone.dns_zone.zone_id
name = "${var.cluster_name}.${var.hosted_zone}"
type = "A"
records = [aws_eip.master.public_ip]
ttl = 300
}