-
Notifications
You must be signed in to change notification settings - Fork 217
/
addons.tf
443 lines (393 loc) · 15.5 KB
/
addons.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
module "eks_blueprints_kubernetes_addons" {
source = "github.com/aws-ia/terraform-aws-eks-blueprints//modules/kubernetes-addons?ref=v4.25.0"
eks_cluster_id = module.eks_blueprints.eks_cluster_id
eks_cluster_endpoint = module.eks_blueprints.eks_cluster_endpoint
eks_oidc_provider = module.eks_blueprints.oidc_provider
eks_cluster_version = module.eks_blueprints.eks_cluster_version
#---------------------------------------------------------------
# Amazon EKS Managed Add-ons
#---------------------------------------------------------------
# EKS Addons
enable_amazon_eks_vpc_cni = true
enable_amazon_eks_coredns = true
enable_amazon_eks_kube_proxy = true
enable_amazon_eks_aws_ebs_csi_driver = true
enable_aws_load_balancer_controller = true
aws_load_balancer_controller_helm_config = {
name = "aws-load-balancer-controller"
chart = "aws-load-balancer-controller"
repository = "https://aws.github.io/eks-charts"
version = "1.4.7"
namespace = "kube-system"
description = "aws-load-balancer-controller Helm Chart for ingress resources"
}
enable_ingress_nginx = true
ingress_nginx_helm_config = {
name = "ingress-nginx"
chart = "ingress-nginx"
repository = "https://kubernetes.github.io/ingress-nginx"
version = "4.5.2"
description = "The NGINX HelmChart Ingress Controller deployment configuration"
values = [templatefile("${path.module}/helm-values/nginx-values.yaml", {})]
}
#---------------------------------------------------------------
# Metrics Server
#---------------------------------------------------------------
enable_metrics_server = true
metrics_server_helm_config = {
name = "metrics-server"
repository = "https://kubernetes-sigs.github.io/metrics-server/" # (Optional) Repository URL where to locate the requested chart.
chart = "metrics-server"
version = "3.8.2"
namespace = "kube-system"
timeout = "300"
values = [templatefile("${path.module}/helm-values/metrics-server-values.yaml", {
operating_system = "linux"
node_group_type = "core"
})]
}
#---------------------------------------------------------------
# Cluster Autoscaler
#---------------------------------------------------------------
enable_cluster_autoscaler = true
cluster_autoscaler_helm_config = {
name = "cluster-autoscaler"
repository = "https://kubernetes.github.io/autoscaler" # (Optional) Repository URL where to locate the requested chart.
chart = "cluster-autoscaler"
version = "9.15.0"
namespace = "kube-system"
timeout = "300"
values = [templatefile("${path.module}/helm-values/cluster-autoscaler-values.yaml", {
aws_region = var.region,
eks_cluster_id = local.name,
operating_system = "linux"
node_group_type = "core"
})]
}
#---------------------------------------------------------------
# Spark Operator Add-on
#---------------------------------------------------------------
enable_spark_k8s_operator = true
spark_k8s_operator_helm_config = {
name = "spark-operator"
chart = "spark-operator"
repository = "https://googlecloudplatform.github.io/spark-on-k8s-operator"
version = "1.1.26"
namespace = "spark-operator"
timeout = "300"
create_namespace = true
values = [templatefile("${path.module}/helm-values/spark-k8s-operator-values.yaml", {
operating_system = "linux"
node_group_type = "core"
})]
}
#---------------------------------------------------------------
# Apache YuniKorn Add-on
#---------------------------------------------------------------
enable_yunikorn = true
yunikorn_helm_config = {
name = "yunikorn"
repository = "https://apache.github.io/yunikorn-release"
chart = "yunikorn"
version = "1.1.0"
timeout = "300"
values = [
templatefile("${path.module}/helm-values/yunikorn-values.yaml", {
image_version = "1.1.0"
operating_system = "linux"
node_group_type = "core"
})
]
timeout = "300"
}
#---------------------------------------------------------------
# Spark History Server Addon
#---------------------------------------------------------------
enable_spark_history_server = true
# This example is using a managed s3 readonly policy. It' recommended to create your own IAM Policy
spark_history_server_irsa_policies = ["arn:${data.aws_partition.current.id}:iam::aws:policy/AmazonS3ReadOnlyAccess"]
spark_history_server_helm_config = {
name = "spark-history-server"
chart = "spark-history-server"
repository = "https://hyper-mesh.github.io/spark-history-server"
version = "1.0.0"
namespace = "spark-history-server"
timeout = "300"
values = [
<<-EOT
serviceAccount:
create: false
sparkHistoryOpts: "-Dspark.history.fs.logDirectory=s3a://${aws_s3_bucket.this.id}/${aws_s3_object.this.key}"
# Update spark conf according to your needs
sparkConf: |-
spark.hadoop.fs.s3a.aws.credentials.provider=com.amazonaws.auth.WebIdentityTokenCredentialsProvider
spark.history.fs.eventLog.rolling.maxFilesToRetain=5
spark.hadoop.fs.s3a.impl=org.apache.hadoop.fs.s3a.S3AFileSystem
spark.eventLog.enabled=true
spark.history.ui.port=18080
resources:
limits:
cpu: 200m
memory: 2G
requests:
cpu: 100m
memory: 1G
EOT
]
}
#---------------------------------------------------------------
# Logging with FluentBit
#---------------------------------------------------------------
enable_aws_for_fluentbit = true
aws_for_fluentbit_cw_log_group_name = "/${var.name}/fluentbit-logs" # Optional
aws_for_fluentbit_cw_log_group_retention = 30
aws_for_fluentbit_irsa_policies = [aws_iam_policy.fluentbit.arn]
aws_for_fluentbit_helm_config = {
name = "aws-for-fluent-bit"
chart = "aws-for-fluent-bit"
repository = "https://aws.github.io/eks-charts"
version = "0.1.22"
namespace = "aws-for-fluent-bit"
timeout = "300"
values = [templatefile("${path.module}/helm-values/aws-for-fluentbit-values.yaml", {
region = data.aws_region.current.id
cloudwatch_log_group = "/${var.name}/fluentbit-logs"
s3_bucket_name = aws_s3_bucket.this.id
cluster_name = module.eks_blueprints.eks_cluster_id
})]
}
#---------------------------------------------------------------
# Amazon Managed Prometheus
#---------------------------------------------------------------
enable_amazon_prometheus = true
amazon_prometheus_workspace_endpoint = module.managed_prometheus.workspace_prometheus_endpoint
#---------------------------------------------------------------
# Prometheus Server Add-on
#---------------------------------------------------------------
enable_prometheus = true
prometheus_helm_config = {
name = "prometheus"
repository = "https://prometheus-community.github.io/helm-charts"
chart = "prometheus"
version = "15.16.1"
namespace = "prometheus"
timeout = "300"
values = [templatefile("${path.module}/helm-values/prometheus-values.yaml", {
operating_system = "linux"
node_group_type = "core"
})]
}
#---------------------------------------------------------------
# Vertical Pod Autoscaling for Prometheus Server
#---------------------------------------------------------------
enable_vpa = true
vpa_helm_config = {
name = "vpa"
repository = "https://charts.fairwinds.com/stable" # (Optional) Repository URL where to locate the requested chart.
chart = "vpa"
version = "1.4.0"
namespace = "vpa"
timeout = "300"
values = [templatefile("${path.module}/helm-values/vpa-values.yaml", {
operating_system = "linux"
node_group_type = "core"
})]
}
#---------------------------------------
# CloudWatch metrics for EKS
#---------------------------------------
enable_aws_cloudwatch_metrics = true
aws_cloudwatch_metrics_helm_config = {
name = "aws-cloudwatch-metrics"
chart = "aws-cloudwatch-metrics"
repository = "https://aws.github.io/eks-charts"
version = "0.0.7"
namespace = "amazon-cloudwatch"
timeout = "300"
values = [
templatefile("${path.module}/helm-values/aws-cloudwatch-metrics-valyes.yaml", {
eks_cluster_id = var.name
})
]
}
#---------------------------------------------------------------
# Open Source Grafana Add-on
#---------------------------------------------------------------
enable_grafana = true
# This example shows how to set default password for grafana using SecretsManager and Helm Chart set_sensitive values.
grafana_helm_config = {
set_sensitive = [
{
name = "adminPassword"
value = data.aws_secretsmanager_secret_version.admin_password_version.secret_string
}
]
}
tags = local.tags
# This is required when using terraform apply with target option
depends_on = [
aws_s3_bucket_acl.this,
aws_s3_bucket_public_access_block.this,
aws_s3_bucket_server_side_encryption_configuration.this,
aws_s3_object.this
]
}
#---------------------------------------------------------------
# Grafana Admin credentials resources
# Login to AWS secrets manager with the same role as Terraform to extract the Grafana admin password with the secret name as "grafana"
#---------------------------------------------------------------
resource "random_password" "grafana" {
length = 16
special = true
override_special = "!#$%&*()-_=+[]{}<>:?"
}
#tfsec:ignore:aws-ssm-secret-use-customer-key
resource "aws_secretsmanager_secret" "grafana" {
name = "grafana"
recovery_window_in_days = 0 # Set to zero for this example to force delete during Terraform destroy
}
resource "aws_secretsmanager_secret_version" "grafana" {
secret_id = aws_secretsmanager_secret.grafana.id
secret_string = random_password.grafana.result
}
module "managed_prometheus" {
source = "terraform-aws-modules/managed-service-prometheus/aws"
version = "~> 2.1"
workspace_alias = local.name
tags = local.tags
}
#---------------------------------------------------------------
# S3 Bucket for SparkHistoryServer logs and FluentBit logs
#---------------------------------------------------------------
#tfsec:ignore:aws-s3-enable-bucket-logging tfsec:ignore:aws-s3-enable-versioning
resource "aws_s3_bucket" "this" {
bucket_prefix = format("%s-%s", "spark", data.aws_caller_identity.current.account_id)
tags = local.tags
}
resource "aws_s3_bucket_acl" "this" {
bucket = aws_s3_bucket.this.id
acl = "private"
}
#tfsec:ignore:aws-s3-encryption-customer-key
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
bucket = aws_s3_bucket.this.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
restrict_public_buckets = true
ignore_public_acls = true
}
# Creating an s3 bucket prefix. Ensure you copy Spark History event logs under this path to visualize the dags
resource "aws_s3_object" "this" {
bucket = aws_s3_bucket.this.id
acl = "private"
key = "${module.eks_blueprints.eks_cluster_id}/event-history-logs/"
content_type = "application/x-directory"
depends_on = [
aws_s3_bucket_acl.this,
aws_s3_bucket_public_access_block.this,
aws_s3_bucket_server_side_encryption_configuration.this
]
}
#---------------------------------------------------------------
# IAM Policy for FluentBit Add-on
#---------------------------------------------------------------
resource "aws_iam_policy" "fluentbit" {
description = "IAM policy policy for FluentBit"
name = "${local.name}-fluentbit-additional"
policy = data.aws_iam_policy_document.fluent_bit.json
}
#---------------------------------------------------------------
# IRSA for Spark driver/executor pods for "spark-team-a"
#---------------------------------------------------------------
module "irsa" {
source = "github.com/aws-ia/terraform-aws-eks-blueprints//modules/irsa?ref=v4.15.0"
eks_cluster_id = local.name
eks_oidc_provider_arn = module.eks_blueprints.eks_oidc_provider_arn
irsa_iam_policies = [aws_iam_policy.spark.arn]
kubernetes_namespace = local.spark_team
kubernetes_service_account = local.spark_team
}
#---------------------------------------------------------------
# Creates IAM policy for IRSA. Provides IAM permissions for Spark driver/executor pods
#---------------------------------------------------------------
resource "aws_iam_policy" "spark" {
description = "IAM role policy for Spark Job execution"
name = "${local.name}-spark-irsa"
policy = data.aws_iam_policy_document.spark_operator.json
}
#---------------------------------------------------------------
# Kubernetes Cluster role for service Account analytics-k8s-data-team-a
#---------------------------------------------------------------
resource "kubernetes_cluster_role" "spark_role" {
metadata {
name = "spark-cluster-role"
}
rule {
verbs = ["get", "list", "watch"]
api_groups = [""]
resources = ["namespaces", "nodes", "persistentvolumes"]
}
rule {
verbs = ["list", "watch"]
api_groups = ["storage.k8s.io"]
resources = ["storageclasses"]
}
rule {
verbs = ["get", "list", "watch", "describe", "create", "edit", "delete", "deletecollection", "annotate", "patch", "label"]
api_groups = [""]
resources = ["serviceaccounts", "services", "configmaps", "events", "pods", "pods/log"]
}
rule {
verbs = ["create", "patch", "delete", "watch"]
api_groups = [""]
resources = ["secrets"]
}
rule {
verbs = ["get", "list", "watch", "describe", "create", "edit", "delete", "annotate", "patch", "label"]
api_groups = ["apps"]
resources = ["statefulsets", "deployments"]
}
rule {
verbs = ["get", "list", "watch", "describe", "create", "edit", "delete", "annotate", "patch", "label"]
api_groups = ["batch"]
resources = ["jobs"]
}
rule {
verbs = ["get", "list", "watch", "describe", "create", "edit", "delete", "annotate", "patch", "label"]
api_groups = ["extensions"]
resources = ["ingresses"]
}
rule {
verbs = ["get", "list", "watch", "describe", "create", "edit", "delete", "deletecollection", "annotate", "patch", "label"]
api_groups = ["rbac.authorization.k8s.io"]
resources = ["roles", "rolebindings"]
}
depends_on = [module.irsa]
}
#---------------------------------------------------------------
# Kubernetes Cluster Role binding role for service Account analytics-k8s-data-team-a
#---------------------------------------------------------------
resource "kubernetes_cluster_role_binding" "spark_role_binding" {
metadata {
name = "spark-cluster-role-bind"
}
subject {
kind = "ServiceAccount"
name = local.spark_team
namespace = local.spark_team
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = kubernetes_cluster_role.spark_role.id
}
depends_on = [module.irsa]
}