-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
main.tf
459 lines (395 loc) · 15.8 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
provider "aws" {
region = local.region
}
provider "kubernetes" {
host = module.eks_blueprints.eks_cluster_endpoint
cluster_ca_certificate = base64decode(module.eks_blueprints.eks_cluster_certificate_authority_data)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
# This requires the awscli to be installed locally where Terraform is executed
args = ["eks", "get-token", "--cluster-name", module.eks_blueprints.eks_cluster_id]
}
}
provider "helm" {
kubernetes {
host = module.eks_blueprints.eks_cluster_endpoint
cluster_ca_certificate = base64decode(module.eks_blueprints.eks_cluster_certificate_authority_data)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
# This requires the awscli to be installed locally where Terraform is executed
args = ["eks", "get-token", "--cluster-name", module.eks_blueprints.eks_cluster_id]
}
}
}
data "aws_availability_zones" "available" {}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
locals {
name = var.name
region = var.region
vpc_cidr = var.vpc_cidr
azs = slice(data.aws_availability_zones.available.names, 0, 3)
tags = merge(var.tags, {
Blueprint = local.name
GithubRepo = "github.com/aws-ia/terraform-aws-eks-blueprints"
})
}
#---------------------------------------------------------------
# EKS Blueprints
#---------------------------------------------------------------
module "eks_blueprints" {
source = "../../.."
cluster_name = local.name
cluster_version = var.eks_cluster_version
vpc_id = module.vpc.vpc_id
private_subnet_ids = module.vpc.private_subnets
cluster_endpoint_private_access = true # if true, Kubernetes API requests within your cluster's VPC (such as node to control plane communication) use the private VPC endpoint
cluster_endpoint_public_access = true # if true, Your cluster API server is accessible from the internet. You can, optionally, limit the CIDR blocks that can access the public endpoint.
#---------------------------------------
# Note: This can further restricted to specific required for each Add-on and your application
#---------------------------------------
node_security_group_additional_rules = {
# Extend node-to-node security group rules. Recommended and required for the Add-ons
ingress_self_all = {
description = "Node to node all ports/protocols"
protocol = "-1"
from_port = 0
to_port = 0
type = "ingress"
self = true
}
# Recommended outbound traffic for Node groups
egress_all = {
description = "Node all egress"
protocol = "-1"
from_port = 0
to_port = 0
type = "egress"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
# Allows Control Plane Nodes to talk to Worker nodes on all ports. Added this to simplify the example and further avoid issues with Add-ons communication with Control plane.
# This can be restricted further to specific port based on the requirement for each Add-on e.g., metrics-server 4443, spark-operator 8080, karpenter 8443 etc.
# Change this according to your security requirements if needed
ingress_cluster_to_node_all_traffic = {
description = "Cluster API to Nodegroup all traffic"
protocol = "-1"
from_port = 0
to_port = 0
type = "ingress"
source_cluster_security_group = true
}
}
managed_node_groups = {
# Core node group for deploying all the critical add-ons
mng1 = {
node_group_name = "core-node-grp"
subnet_ids = module.vpc.private_subnets
instance_types = ["m5.xlarge"]
ami_type = "AL2_x86_64"
capacity_type = "ON_DEMAND"
disk_size = 100
disk_type = "gp3"
max_size = 9
min_size = 3
desired_size = 3
create_launch_template = true
launch_template_os = "amazonlinux2eks"
update_config = [{
max_unavailable_percentage = 50
}]
k8s_labels = {
Environment = "preprod"
Zone = "test"
WorkerType = "ON_DEMAND"
NodeGroupType = "core"
}
additional_tags = {
Name = "core-node-grp"
subnet_type = "private"
"k8s.io/cluster-autoscaler/node-template/label/arch" = "x86"
"k8s.io/cluster-autoscaler/node-template/label/kubernetes.io/os" = "linux"
"k8s.io/cluster-autoscaler/node-template/label/noderole" = "core"
"k8s.io/cluster-autoscaler/node-template/label/node-lifecycle" = "on-demand"
"k8s.io/cluster-autoscaler/experiments" = "owned"
"k8s.io/cluster-autoscaler/enabled" = "true"
}
},
#---------------------------------------
# Note: This example only uses ON_DEMAND node group for both Spark Driver and Executors.
# If you want to leverage SPOT nodes for Spark executors then create ON_DEMAND node group for placing your driver pods and SPOT nodegroup for executors.
# Use NodeSelectors to place your driver/executor pods with the help of Pod Templates.
#---------------------------------------
mng2 = {
node_group_name = "spark-node-grp"
subnet_ids = module.vpc.private_subnets
instance_types = ["r5d.large"]
ami_type = "AL2_x86_64"
capacity_type = "ON_DEMAND"
format_mount_nvme_disk = true # Mounts NVMe disks to /local1, /local2 etc. for multiple NVMe disks
# RAID0 configuration is recommended for better performance when you use larger instances with multiple NVMe disks e.g., r5d.24xlarge
# Permissions can further be restricted to the mounted folder for only Spark execution user
post_userdata = <<-EOT
#!/bin/bash
set -ex
/usr/bin/chmod 777 /local1
EOT
disk_size = 100
disk_type = "gp3"
max_size = 9 # Managed node group soft limit is 450; request AWS for limit increase
min_size = 3
desired_size = 3
create_launch_template = true
launch_template_os = "amazonlinux2eks"
update_config = [{
max_unavailable_percentage = 50
}]
additional_iam_policies = []
k8s_taints = []
k8s_labels = {
Environment = "preprod"
Zone = "test"
WorkerType = "ON_DEMAND"
NodeGroupType = "spark"
}
additional_tags = {
Name = "spark-node-grp"
subnet_type = "private"
"k8s.io/cluster-autoscaler/node-template/label/arch" = "x86"
"k8s.io/cluster-autoscaler/node-template/label/kubernetes.io/os" = "linux"
"k8s.io/cluster-autoscaler/node-template/label/noderole" = "spark"
"k8s.io/cluster-autoscaler/node-template/label/disk" = "nvme"
"k8s.io/cluster-autoscaler/node-template/label/node-lifecycle" = "on-demand"
"k8s.io/cluster-autoscaler/experiments" = "owned"
"k8s.io/cluster-autoscaler/enabled" = "true"
}
},
}
#---------------------------------------
# ENABLE EMR ON EKS
# 1. Creates namespace
# 2. k8s role and role binding(emr-containers user) for the above namespace
# 3. IAM role for the team execution role
# 4. Update AWS_AUTH config map with emr-containers user and AWSServiceRoleForAmazonEMRContainers role
# 5. Create a trust relationship between the job execution role and the identity of the EMR managed service account
#---------------------------------------
enable_emr_on_eks = true
emr_on_eks_teams = {
emr-data-team-a = {
namespace = "emr-data-team-a"
job_execution_role = "emr-eks-data-team-a"
additional_iam_policies = [aws_iam_policy.emr_on_eks.arn]
}
emr-data-team-b = {
namespace = "emr-data-team-b"
job_execution_role = "emr-eks-data-team-b"
additional_iam_policies = [aws_iam_policy.emr_on_eks.arn]
}
}
tags = local.tags
}
module "eks_blueprints_kubernetes_addons" {
source = "../../../modules/kubernetes-addons"
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
#---------------------------------------
enable_amazon_eks_vpc_cni = true
enable_amazon_eks_coredns = true
enable_amazon_eks_kube_proxy = true
#---------------------------------------------------------
# CoreDNS Autoscaler helps to scale for large EKS Clusters
# Further tuning for CoreDNS is to leverage NodeLocal DNSCache -> https://kubernetes.io/docs/tasks/administer-cluster/nodelocaldns/
#---------------------------------------------------------
enable_coredns_autoscaler = true
coredns_autoscaler_helm_config = {
name = "cluster-proportional-autoscaler"
chart = "cluster-proportional-autoscaler"
repository = "https://kubernetes-sigs.github.io/cluster-proportional-autoscaler"
version = "1.0.0"
namespace = "kube-system"
timeout = "300"
values = [templatefile("${path.module}/helm-values/coredns-autoscaler-values.yaml", {
operating_system = "linux"
target = "deployment/coredns"
})]
description = "Cluster Proportional Autoscaler for CoreDNS Service"
}
#---------------------------------------
# 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"
})]
}
#---------------------------------------
# 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"
})]
}
#---------------------------------------
# Amazon Managed Prometheus
#---------------------------------------
enable_amazon_prometheus = true
amazon_prometheus_workspace_endpoint = aws_prometheus_workspace.amp.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.10.1"
namespace = "prometheus"
timeout = "300"
values = [templatefile("${path.module}/helm-values/prometheus-values.yaml", {
operating_system = "linux"
})]
}
#---------------------------------------
# Vertical Pod Autoscaling
#---------------------------------------
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"
})]
}
tags = local.tags
}
#---------------------------------------------------------------
# VPC and Subnets
#---------------------------------------------------------------
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 3.0"
name = local.name
cidr = local.vpc_cidr
azs = local.azs
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 10)]
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
# Manage so we can name
manage_default_network_acl = true
default_network_acl_tags = { Name = "${local.name}-default" }
manage_default_route_table = true
default_route_table_tags = { Name = "${local.name}-default" }
manage_default_security_group = true
default_security_group_tags = { Name = "${local.name}-default" }
public_subnet_tags = {
"kubernetes.io/cluster/${local.name}" = "shared"
"kubernetes.io/role/elb" = 1
}
private_subnet_tags = {
"kubernetes.io/cluster/${local.name}" = "shared"
"kubernetes.io/role/internal-elb" = 1
}
default_security_group_name = "${local.name}-endpoint-secgrp"
default_security_group_ingress = [
{
protocol = -1
from_port = 0
to_port = 0
cidr_blocks = local.vpc_cidr
}]
default_security_group_egress = [
{
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = "0.0.0.0/0"
}]
tags = local.tags
}
#---------------------------------------------------------------
# Example IAM policies for EMR job execution
#---------------------------------------------------------------
data "aws_iam_policy_document" "emr_on_eks" {
statement {
sid = ""
effect = "Allow"
resources = ["arn:${data.aws_partition.current.partition}:s3:::*"]
actions = [
"s3:DeleteObject",
"s3:DeleteObjectVersion",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
]
}
statement {
sid = ""
effect = "Allow"
resources = ["arn:${data.aws_partition.current.partition}:logs:${data.aws_region.current.id}:${data.aws_caller_identity.current.account_id}:log-group:*"]
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutLogEvents",
]
}
}
resource "aws_iam_policy" "emr_on_eks" {
name = format("%s-%s", local.name, "emr-job-iam-policies")
description = "IAM policy for EMR on EKS Job execution"
path = "/"
policy = data.aws_iam_policy_document.emr_on_eks.json
}
#---------------------------------------------------------------
# Amazon Prometheus Workspace
#---------------------------------------------------------------
resource "aws_prometheus_workspace" "amp" {
alias = format("%s-%s", "amp-ws", local.name)
tags = local.tags
}
#---------------------------------------------------------------
# Create EMR on EKS Virtual Cluster
#---------------------------------------------------------------
resource "aws_emrcontainers_virtual_cluster" "this" {
name = format("%s-%s", module.eks_blueprints.eks_cluster_id, "emr-data-team-a")
container_provider {
id = module.eks_blueprints.eks_cluster_id
type = "EKS"
info {
eks_info {
namespace = "emr-data-team-a"
}
}
}
}