forked from rancher/terraform-k3s-aws-cluster
-
Notifications
You must be signed in to change notification settings - Fork 12
/
infra.tf
272 lines (232 loc) · 7.49 KB
/
infra.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
#############################
### Access Control
#############################
resource "aws_security_group" "ingress" {
name = "${local.name}-ingress"
vpc_id = data.aws_vpc.default.id
}
resource "aws_security_group_rule" "ingress_http" {
type = "ingress"
from_port = 80
to_port = 80
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ingress.id
}
resource "aws_security_group_rule" "ingress_https" {
type = "ingress"
from_port = 443
to_port = 443
protocol = "TCP"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ingress.id
}
resource "aws_security_group_rule" "ingress_self" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
self = true
security_group_id = aws_security_group.ingress.id
}
resource "aws_security_group_rule" "ingress_egress_all" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.ingress.id
}
resource "aws_security_group" "self" {
name = "${local.name}-self"
vpc_id = data.aws_vpc.default.id
}
resource "aws_security_group_rule" "self_self" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
self = true
security_group_id = aws_security_group.self.id
}
resource "aws_security_group_rule" "self_k3s_server" {
type = "ingress"
from_port = 6443
to_port = 6443
protocol = "TCP"
cidr_blocks = local.private_subnets_cidr_blocks
security_group_id = aws_security_group.self.id
}
resource "aws_security_group" "database" {
name = "${local.name}-database"
vpc_id = data.aws_vpc.default.id
}
resource "aws_security_group_rule" "database_self" {
type = "ingress"
from_port = 5432
to_port = 5432
protocol = "TCP"
self = true
security_group_id = aws_security_group.database.id
}
resource "aws_security_group_rule" "database_egress_all" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = aws_security_group.database.id
}
#############################
### Create Nodes
#############################
resource "aws_launch_template" "k3s_server" {
name_prefix = "${local.name}-server"
image_id = local.server_image_id
instance_type = local.server_instance_type
user_data = data.cloudinit_config.k3s_server.rendered
block_device_mappings {
device_name = "/dev/sda1"
ebs {
encrypted = true
volume_type = local.server_volume_type
volume_size = "50"
}
}
network_interfaces {
delete_on_termination = true
security_groups = concat([aws_security_group.self.id, aws_security_group.database.id], var.extra_server_security_groups)
}
tags = {
Name = "${local.name}-server"
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "${local.name}-server"
}
}
}
resource "aws_launch_template" "k3s_agent" {
name_prefix = "${local.name}-agent"
image_id = local.agent_image_id
instance_type = local.agent_instance_type
user_data = data.cloudinit_config.k3s_agent.rendered
block_device_mappings {
device_name = "/dev/sda1"
ebs {
encrypted = true
volume_type = local.agent_volume_type
volume_size = "50"
}
}
network_interfaces {
delete_on_termination = true
security_groups = concat([aws_security_group.ingress.id, aws_security_group.self.id], var.extra_agent_security_groups)
}
tags = {
Name = "${local.name}-agent"
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "${local.name}-agent"
}
}
}
resource "aws_autoscaling_group" "k3s_server" {
name_prefix = "${local.name}-server"
desired_capacity = local.server_node_count
max_size = local.server_node_count
min_size = local.server_node_count
vpc_zone_identifier = local.private_subnets
target_group_arns = [
aws_lb_target_group.server-6443.arn
]
launch_template {
id = aws_launch_template.k3s_server.id
version = "$Latest"
}
depends_on = [aws_rds_cluster_instance.k3s]
}
resource "aws_autoscaling_group" "k3s_agent" {
name_prefix = "${local.name}-agent"
desired_capacity = local.agent_node_count
max_size = local.agent_node_count
min_size = local.agent_node_count
vpc_zone_identifier = local.private_subnets
target_group_arns = var.create_external_nlb ? [
aws_lb_target_group.agent-80.0.arn,
aws_lb_target_group.agent-443.0.arn
] : null
launch_template {
id = aws_launch_template.k3s_agent.id
version = "$Latest"
}
}
#############################
### Create Database
#############################
resource "aws_db_subnet_group" "private" {
count = local.deploy_rds
name_prefix = "${local.name}-private"
subnet_ids = local.private_subnets
}
resource "aws_rds_cluster_parameter_group" "k3s" {
count = local.deploy_rds
name_prefix = "${local.name}-"
description = "Force SSL for aurora-postgresql"
family = local.db_parameter_group_family
parameter {
name = "rds.force_ssl"
value = "1"
apply_method = "pending-reboot"
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_rds_cluster" "k3s" {
count = local.deploy_rds
cluster_identifier_prefix = "${local.name}-"
engine = "aurora-postgresql"
engine_mode = "provisioned"
engine_version = local.db_engine_version
allow_major_version_upgrade = local.db_allow_major_version_upgrade
db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.k3s.0.name
availability_zones = local.aws_azs
database_name = local.db_name
master_username = local.db_user
master_password = local.db_pass
preferred_maintenance_window = "fri:11:21-fri:11:51"
db_subnet_group_name = aws_db_subnet_group.private.0.id
vpc_security_group_ids = [aws_security_group.database.id]
storage_encrypted = true
preferred_backup_window = "11:52-19:52"
backup_retention_period = 30
copy_tags_to_snapshot = true
deletion_protection = false
skip_final_snapshot = local.skip_final_snapshot ? true : false
final_snapshot_identifier = local.skip_final_snapshot ? null : "${local.name}-final-snapshot"
}
resource "aws_rds_cluster_instance" "k3s" {
count = local.db_node_count
identifier_prefix = "${local.name}-${count.index}"
cluster_identifier = aws_rds_cluster.k3s.0.id
engine = "aurora-postgresql"
instance_class = local.db_instance_type
db_subnet_group_name = aws_db_subnet_group.private.0.id
ca_cert_identifier = local.rds_ca_cert_identifier
}
#############################
### Create Public Rancher DNS
#############################
resource "aws_route53_record" "rancher" {
count = local.use_route53
zone_id = data.aws_route53_zone.dns_zone.0.zone_id
name = "${local.subdomain}.${local.domain}"
type = "CNAME"
ttl = 30
records = [aws_lb.lb.0.dns_name]
provider = aws.r53
}