generated from aws-ia/terraform-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathmain.tf
382 lines (317 loc) · 10.2 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
provider "aws" {
region = local.region
}
# Required for public ECR where Karpenter artifacts are hosted
provider "aws" {
region = "us-east-1"
alias = "virginia"
}
provider "kubernetes" {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.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.cluster_name]
}
}
provider "helm" {
kubernetes {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.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.cluster_name]
}
}
}
data "aws_availability_zones" "available" {}
data "aws_ecrpublic_authorization_token" "token" {
provider = aws.virginia
}
locals {
name = basename(path.cwd)
region = "us-west-2"
vpc_cidr = "10.0.0.0/16"
azs = slice(data.aws_availability_zones.available.names, 0, 3)
tags = {
Blueprint = local.name
GithubRepo = "github.com/aws-ia/terraform-aws-eks-blueprints-addons"
}
}
################################################################################
# Blueprints Addons
################################################################################
module "eks_blueprints_addons" {
source = "../../"
cluster_name = module.eks.cluster_name
cluster_endpoint = module.eks.cluster_endpoint
cluster_version = module.eks.cluster_version
oidc_provider_arn = module.eks.oidc_provider_arn
eks_addons = {
aws-ebs-csi-driver = {
most_recent = true
service_account_role_arn = module.ebs_csi_driver_irsa.iam_role_arn
}
coredns = {
most_recent = true
timeouts = {
create = "25m"
delete = "10m"
}
}
vpc-cni = {
most_recent = true
}
kube-proxy = {}
adot = {
most_recent = true
service_account_role_arn = module.adot_irsa.iam_role_arn
}
}
enable_aws_efs_csi_driver = true
enable_aws_fsx_csi_driver = true
enable_argocd = true
enable_argo_rollouts = true
enable_argo_workflows = true
enable_aws_cloudwatch_metrics = true
enable_aws_privateca_issuer = true
enable_cluster_autoscaler = true
enable_secrets_store_csi_driver = true
enable_secrets_store_csi_driver_provider_aws = true
enable_kube_prometheus_stack = true
enable_external_dns = true
external_dns_route53_zone_arns = [
"arn:aws:route53:::hostedzone/*",
]
enable_external_secrets = true
enable_gatekeeper = true
enable_ingress_nginx = true
# Wait for all Cert-manager related resources to be ready
enable_cert_manager = true
cert_manager = {
wait = true
}
# Turn off mutation webhook for services to avoid ordering issue
enable_aws_load_balancer_controller = true
aws_load_balancer_controller = {
set = [{
name = "enableServiceMutatorWebhook"
value = "false"
}]
}
enable_metrics_server = true
enable_vpa = true
enable_fargate_fluentbit = true
enable_aws_for_fluentbit = true
aws_for_fluentbit_cw_log_group = {
create = true
use_name_prefix = true # Set this to true to enable name prefix
name_prefix = "eks-cluster-logs-"
retention = 7
}
aws_for_fluentbit = {
enable_containerinsights = true
kubelet_monitoring = true
chart_version = "0.1.28"
set = [{
name = "cloudWatchLogs.autoCreateGroup"
value = true
},
{
name = "hostNetwork"
value = true
},
{
name = "dnsPolicy"
value = "ClusterFirstWithHostNet"
}
]
s3_bucket_arns = [
module.velero_backup_s3_bucket.s3_bucket_arn,
"${module.velero_backup_s3_bucket.s3_bucket_arn}/logs/*"
]
}
enable_aws_node_termination_handler = true
aws_node_termination_handler_asg_arns = [for asg in module.eks.self_managed_node_groups : asg.autoscaling_group_arn]
enable_karpenter = true
karpenter_enable_instance_profile_creation = true
# ECR login required
karpenter = {
repository_username = data.aws_ecrpublic_authorization_token.token.user_name
repository_password = data.aws_ecrpublic_authorization_token.token.password
}
enable_velero = true
## An S3 Bucket ARN is required. This can be declared with or without a Prefix.
velero = {
s3_backup_location = "${module.velero_backup_s3_bucket.s3_bucket_arn}/backups"
values = [
# https://github.com/vmware-tanzu/helm-charts/issues/550#issuecomment-1959933230
<<-EOT
kubectl:
image:
tag: 1.29.2-debian-11-r5
EOT
]
}
enable_aws_gateway_api_controller = true
# ECR login required
aws_gateway_api_controller = {
repository_username = data.aws_ecrpublic_authorization_token.token.user_name
repository_password = data.aws_ecrpublic_authorization_token.token.password
set = [{
name = "clusterVpcId"
value = module.vpc.vpc_id
}]
}
enable_bottlerocket_update_operator = true
# Pass in any number of Helm charts to be created for those that are not natively supported
helm_releases = {
prometheus-adapter = {
description = "A Helm chart for k8s prometheus adapter"
namespace = "prometheus-adapter"
create_namespace = true
chart = "prometheus-adapter"
chart_version = "4.2.0"
repository = "https://prometheus-community.github.io/helm-charts"
values = [
<<-EOT
replicas: 2
podDisruptionBudget:
enabled: true
EOT
]
}
gpu-operator = {
description = "A Helm chart for NVIDIA GPU operator"
namespace = "gpu-operator"
create_namespace = true
chart = "gpu-operator"
chart_version = "v23.9.1"
repository = "https://nvidia.github.io/gpu-operator"
values = [
<<-EOT
operator:
defaultRuntime: containerd
EOT
]
}
}
tags = local.tags
}
################################################################################
# Cluster
################################################################################
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 20.4"
cluster_name = local.name
cluster_version = "1.29"
cluster_endpoint_public_access = true
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
# Give the Terraform identity admin access to the cluster
# which will allow resources to be deployed into the cluster
enable_cluster_creator_admin_permissions = true
eks_managed_node_groups = {
initial = {
instance_types = ["m5.xlarge"]
min_size = 2
max_size = 10
desired_size = 5
}
}
# For demonstrating node-termination-handler
self_managed_node_groups = {
default = {
instance_type = "m5.large"
min_size = 1
max_size = 10
desired_size = 1
}
}
tags = local.tags
}
################################################################################
# Supporting Resources
################################################################################
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 5.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
public_subnet_tags = {
"kubernetes.io/role/elb" = 1
}
private_subnet_tags = {
"kubernetes.io/role/internal-elb" = 1
}
tags = local.tags
}
module "velero_backup_s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "~> 3.0"
bucket_prefix = "${local.name}-"
# Allow deletion of non-empty bucket
# NOTE: This is enabled for example usage only, you should not enable this for production workloads
force_destroy = true
attach_deny_insecure_transport_policy = true
attach_require_latest_tls_policy = true
acl = "private"
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
control_object_ownership = true
object_ownership = "BucketOwnerPreferred"
versioning = {
status = true
mfa_delete = false
}
server_side_encryption_configuration = {
rule = {
apply_server_side_encryption_by_default = {
sse_algorithm = "AES256"
}
}
}
tags = local.tags
}
module "ebs_csi_driver_irsa" {
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
version = "~> 5.20"
role_name_prefix = "${local.name}-ebs-csi-driver-"
attach_ebs_csi_policy = true
oidc_providers = {
main = {
provider_arn = module.eks.oidc_provider_arn
namespace_service_accounts = ["kube-system:ebs-csi-controller-sa"]
}
}
tags = local.tags
}
module "adot_irsa" {
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
version = "~> 5.20"
role_name_prefix = "${local.name}-adot-"
role_policy_arns = {
prometheus = "arn:aws:iam::aws:policy/AmazonPrometheusRemoteWriteAccess"
xray = "arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
cloudwatch = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
oidc_providers = {
main = {
provider_arn = module.eks.oidc_provider_arn
namespace_service_accounts = ["opentelemetry-operator-system:opentelemetry-operator"]
}
}
tags = local.tags
}