-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.tf
390 lines (346 loc) · 10.7 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/**
* # Why
*
* To spin up complete eks with all necessary components.
* Those include:
* - vpc (NOTE: the vpc submodule moved into separate repo https://github.com/dasmeta/terraform-aws-vpc)
* - eks cluster
* - alb ingress controller
* - fluentbit
* - external secrets
* - metrics to cloudwatch
*
* ## How to run
* ```hcl
*data "aws_availability_zones" "available" {}
*
*locals {
* cluster_endpoint_public_access = true
* cluster_enabled_log_types = ["audit"]
* vpc = {
* create = {
* name = "dev"
* availability_zones = data.aws_availability_zones.available.names
* private_subnets = ["172.16.1.0/24", "172.16.2.0/24", "172.16.3.0/24"]
* public_subnets = ["172.16.4.0/24", "172.16.5.0/24", "172.16.6.0/24"]
* cidr = "172.16.0.0/16"
* public_subnet_tags = {
* "kubernetes.io/cluster/dev" = "shared"
* "kubernetes.io/role/elb" = "1"
* }
* private_subnet_tags = {
* "kubernetes.io/cluster/dev" = "shared"
* "kubernetes.io/role/internal-elb" = "1"
* }
* }
* }
* cluster_name = "your-cluster-name-goes-here"
* alb_log_bucket_name = "your-log-bucket-name-goes-here"
*
* fluent_bit_name = "fluent-bit"
* log_group_name = "fluent-bit-cloudwatch-env"
*}
*
*
*#(Basic usage with example of using already created VPC)
*data "aws_availability_zones" "available" {}
*
*locals {
* cluster_endpoint_public_access = true
* cluster_enabled_log_types = ["audit"]
*
* vpc = {
* link = {
* id = "vpc-1234"
* private_subnet_ids = ["subnet-1", "subnet-2"]
* }
* }
* cluster_name = "your-cluster-name-goes-here"
* alb_log_bucket_name = "your-log-bucket-name-goes-here"
*
* fluent_bit_name = "fluent-bit"
* log_group_name = "fluent-bit-cloudwatch-env"
*}
*
*# Minimum
*
*module "cluster_min" {
* source = "dasmeta/eks/aws"
* version = "0.1.1"
*
* cluster_name = local.cluster_name
* users = local.users
*
* vpc = {
* link = {
* id = "vpc-1234"
* private_subnet_ids = ["subnet-1", "subnet-2"]
* }
* }
*
*}
*
*# Max @TODO: the max param passing setup needs to be checked/fixed
*
* module "cluster_max" {
* source = "dasmeta/eks/aws"
* version = "0.1.1"
*
* ### VPC
* vpc = {
* create = {
* name = "dev"
* availability_zones = data.aws_availability_zones.available.names
* private_subnets = ["172.16.1.0/24", "172.16.2.0/24", "172.16.3.0/24"]
* public_subnets = ["172.16.4.0/24", "172.16.5.0/24", "172.16.6.0/24"]
* cidr = "172.16.0.0/16"
* public_subnet_tags = {
* "kubernetes.io/cluster/dev" = "shared"
* "kubernetes.io/role/elb" = "1"
* }
* private_subnet_tags = {
* "kubernetes.io/cluster/dev" = "shared"
* "kubernetes.io/role/internal-elb" = "1"
* }
* }
* }
*
* cluster_enabled_log_types = local.cluster_enabled_log_types
* cluster_endpoint_public_access = local.cluster_endpoint_public_access
*
* ### EKS
* cluster_name = local.cluster_name
* manage_aws_auth = true
*
* # IAM users username and group. By default value is ["system:masters"]
* user = [
* {
* username = "devops1"
* group = ["system:masters"]
* },
* {
* username = "devops2"
* group = ["system:kube-scheduler"]
* },
* {
* username = "devops3"
* }
* ]
*
* # You can create node use node_group when you create node in specific subnet zone.(Note. This Case Ec2 Instance havn't specific name).
* # Other case you can use worker_group variable.
*
* node_groups = {
* example = {
* name = "nodegroup"
* name-prefix = "nodegroup"
* additional_tags = {
* "Name" = "node"
* "ExtraTag" = "ExtraTag"
* }
*
* instance_type = "t3.xlarge"
* max_capacity = 1
* disk_size = 50
* create_launch_template = false
* subnet = ["subnet_id"]
* }
* }
*
* node_groups_default = {
* disk_size = 50
* instance_types = ["t3.medium"]
* }
*
* worker_groups = {
* default = {
* name = "nodes"
* instance_type = "t3.xlarge"
* asg_max_size = 3
* root_volume_size = 50
* }
* }
*
* workers_group_defaults = {
* launch_template_use_name_prefix = true
* launch_template_name = "default"
* root_volume_type = "gp2"
* root_volume_size = 50
* }
*
* ### ALB-INGRESS-CONTROLLER
* alb_log_bucket_name = local.alb_log_bucket_name
*
* ### FLUENT-BIT
* fluent_bit_name = local.fluent_bit_name
* log_group_name = local.log_group_name
*
* # Should be refactored to install from cluster: for prod it has done from metrics-server.tf
* ### METRICS-SERVER
* # enable_metrics_server = false
* metrics_server_name = "metrics-server"
}
* ```
**/
module "vpc" {
source = "dasmeta/vpc/aws"
version = "1.0.1"
count = try(var.vpc.create.name) != null ? 1 : 0
name = var.vpc.create.name
availability_zones = var.vpc.create.availability_zones
cidr = var.vpc.create.cidr
private_subnets = var.vpc.create.private_subnets
public_subnets = var.vpc.create.public_subnets
public_subnet_tags = merge(
{
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
"kubernetes.io/role/elb" = 1
},
var.vpc.create.public_subnet_tags
)
private_subnet_tags = merge(
{
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
"kubernetes.io/role/internal-elb" = 1
},
var.vpc.create.private_subnet_tags
)
}
module "eks-cluster" {
source = "./modules/eks"
count = var.create ? 1 : 0
region = local.region
cluster_name = var.cluster_name
vpc_id = var.vpc.create.name != null ? module.vpc[0].id : var.vpc.link.id
subnets = var.vpc.create.name != null ? module.vpc[0].private_subnets : var.vpc.link.private_subnet_ids
users = var.users
node_groups = var.node_groups
node_groups_default = var.node_groups_default
worker_groups = var.worker_groups
workers_group_defaults = var.workers_group_defaults
cluster_endpoint_public_access = var.cluster_endpoint_public_access
cluster_enabled_log_types = var.cluster_enabled_log_types
cluster_version = var.cluster_version
map_roles = var.map_roles
node_security_group_additional_rules = var.node_security_group_additional_rules
}
module "cloudwatch-metrics" {
source = "./modules/cloudwatch-metrics"
count = var.metrics_exporter == "cloudwatch" ? 1 : 0
account_id = local.account_id
region = local.region
eks_oidc_root_ca_thumbprint = local.eks_oidc_root_ca_thumbprint
oidc_provider_arn = module.eks-cluster[0].oidc_provider_arn
cluster_name = module.eks-cluster[0].cluster_id
}
module "metrics-server" {
source = "./modules/metrics-server"
count = var.create ? 1 : 0
name = var.metrics_server_name != "" ? var.metrics_server_name : "${module.eks-cluster[0].cluster_name}-metrics-server"
}
module "external-secrets" {
source = "./modules/external-secrets"
count = var.create ? 1 : 0
namespace = var.external_secrets_namespace
depends_on = [
module.eks-cluster
]
}
module "sso-rbac" {
source = "./modules/sso-rbac"
count = var.enable_sso_rbac && var.create ? 1 : 0
account_id = local.account_id
roles = var.roles
bindings = var.bindings
eks_module = module.eks-cluster[0].eks_module
depends_on = [
module.eks-cluster
]
}
module "efs-csi-driver" {
source = "./modules/efs-csi"
count = var.enable_efs_driver ? 1 : 0
cluster_name = var.cluster_name
efs_id = var.efs_id
cluster_oidc_arn = module.eks-cluster[0].oidc_provider_arn
storage_classes = var.efs_storage_classes
}
resource "helm_release" "cert-manager" {
count = var.create_cert_manager ? 1 : var.metrics_exporter == "adot" ? 1 : 0
namespace = "cert-manager"
create_namespace = true
name = "cert-manager"
chart = "cert-manager"
repository = "https://charts.jetstack.io"
atomic = true
version = "v1.13.1"
set {
name = "installCRDs"
value = "true"
}
depends_on = [
module.eks-cluster
]
}
resource "helm_release" "kube-state-metrics" {
count = var.enable_kube_state_metrics ? 1 : 0
name = "kube-state-metrics"
repository = "https://prometheus-community.github.io/helm-charts"
chart = "kube-state-metrics"
namespace = "kube-system"
version = "4.22.3"
create_namespace = false
atomic = true
set_list {
name = "metricAllowlist"
value = concat(var.prometheus_metrics, [
"kube_deployment_spec_replicas",
"kube_deployment_status_replicas_available"
])
}
}
module "autoscaler" {
source = "./modules/autoscaler"
count = var.autoscaling ? 1 : 0
cluster_name = var.cluster_name
cluster_oidc_arn = module.eks-cluster[0].oidc_provider_arn
eks_version = var.cluster_version
autoscaler_image_patch = var.autoscaler_image_patch
scale_down_unneeded_time = var.scale_down_unneeded_time
requests = var.autoscaler_requests
limits = var.autoscaler_limits
}
module "ebs-csi" {
source = "./modules/ebs-csi"
count = var.enable_ebs_driver ? 1 : 0
cluster_name = var.cluster_name
cluster_oidc_arn = module.eks-cluster[0].oidc_provider_arn
addon_version = var.ebs_csi_version
}
module "api-gw-controller" {
source = "./modules/api-gw"
count = var.enable_api_gw_controller ? 1 : 0
cluster_name = var.cluster_name
cluster_oidc_arn = module.eks-cluster[0].oidc_provider_arn
deploy_region = var.api_gw_deploy_region
api_gateway_resources = var.api_gateway_resources
vpc_id = var.api_gateway_resources[0].vpc_links != null ? module.vpc[0].id : null
subnet_ids = var.api_gateway_resources[0].vpc_links != null ? (var.vpc.create.private_subnets != {} ? module.vpc[0].private_subnets : var.vpc.link.private_subnet_ids) : null
}
module "portainer" {
count = var.enable_portainer ? 1 : 0
source = "./modules/portainer"
host = var.portainer_config.host
enable_ingress = var.portainer_config.enable_ingress
}
module "external-dns" {
count = var.create && var.external_dns.enabled ? 1 : 0
source = "./modules/external-dns"
cluster_name = var.cluster_name
oidc_provider_arn = module.eks-cluster[0].oidc_provider_arn
region = local.region
configs = var.external_dns.configs
depends_on = [
module.eks-cluster
]
}