-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
201 lines (165 loc) · 5.68 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
provider "aws" {
region = local.region
}
provider "aws" {
alias = "ecr"
region = "us-east-1"
}
provider "kubernetes" {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
token = data.aws_eks_cluster_auth.this.token
}
provider "helm" {
kubernetes {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
token = data.aws_eks_cluster_auth.this.token
}
}
provider "kubectl" {
apply_retry_count = 30
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
load_config_file = false
token = data.aws_eks_cluster_auth.this.token
}
data "aws_eks_cluster_auth" "this" {
name = module.eks.cluster_name
}
data "aws_subnets" "private_subnets" {
filter {
name = "vpc-id"
values = [module.vpc.vpc_id]
}
tags = {
"type" = "private"
}
}
data "aws_ecrpublic_authorization_token" "token" {
provider = aws.ecr
}
data "aws_ecr_authorization_token" "token" {
registry_id = "895885662937"
}
data "aws_caller_identity" "current" {}
data "aws_availability_zones" "available" {}
data "aws_region" "current" {}
data "aws_partition" "current" {}
locals {
name = var.name
region = var.region
karpenter_nodepool_name = var.karpenter-nodepool-name
azs = slice(data.aws_availability_zones.available.names, 0, 2)
tags = merge(var.tags, {
app = local.name
})
karpenter_node_sg_tags = merge(var.tags, {
"karpenter.sh/discovery" = local.name
})
}
#---------------------------------------------------------------
# EKS Cluster
#---------------------------------------------------------------
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 19.18"
cluster_name = local.name
cluster_version = var.eks_cluster_version
# if true, Your cluster API server is accessible from the internet. You can, optionally, limit the CIDR blocks that can access the public endpoint.
#WARNING: Avoid using this option (cluster_endpoint_public_access = true) in preprod or prod accounts. This feature is designed for sandbox accounts, simplifying cluster deployment and testing.
cluster_endpoint_public_access = true
vpc_id = module.vpc.vpc_id
# Filtering only Secondary CIDR private subnets starting with "100.". Subnet IDs where the EKS Control Plane ENIs will be created
subnet_ids = data.aws_subnets.private_subnets.ids
control_plane_subnet_ids = data.aws_subnets.private_subnets.ids
manage_aws_auth_configmap = true
aws_auth_roles = [
# We need to add in the Karpenter node IAM role for nodes launched by Karpenter
{
# Required for EMR on EKS virtual cluster
rolearn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/AWSServiceRoleForAmazonEMRContainers"
username = "emr-containers"
groups = []
},
{
rolearn = module.eks_blueprints_addons.karpenter.node_iam_role_arn
username = "system:node:{{EC2PrivateDNSName}}"
groups = [
"system:bootstrappers",
"system:nodes",
]
}
]
#---------------------------------------
# Note: This can further restricted to specific required for each Add-on and your application
#---------------------------------------
# Extend cluster security group rules
cluster_security_group_additional_rules = {
ingress_nodes_ephemeral_ports_tcp = {
description = "Nodes on ephemeral ports"
protocol = "tcp"
from_port = 1025
to_port = 65535
type = "ingress"
source_node_security_group = true
}
}
# Extend node-to-node security group rules
node_security_group_additional_rules = {
ingress_self_all = {
description = "Node to node all ports/protocols"
protocol = "-1"
from_port = 0
to_port = 0
type = "ingress"
self = true
}
}
eks_managed_node_group_defaults = {
iam_role_additional_policies = {
# Not required, but used in the example to access the nodes to inspect mounted volumes
AmazonSSMManagedInstanceCore = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
}
eks_managed_node_groups = {
# We recommend to have a MNG to place your critical workloads and add-ons
# Then rely on Karpenter to scale your workloads
# You can also make uses on nodeSelector and Taints/tolerations to spread workloads on MNG or Karpenter provisioners
core_node_group = {
name = "core-node-group"
description = "EKS managed node group example launch template"
min_size = 1
max_size = 9
desired_size = 3
instance_types = ["m5.xlarge"]
ebs_optimized = true
block_device_mappings = {
xvda = {
device_name = "/dev/xvda"
ebs = {
volume_size = 100
volume_type = "gp3"
}
}
}
labels = {
WorkerType = "ON_DEMAND"
NodeGroupType = "core"
}
tags = {
Name = "core-node-grp",
"karpenter.sh/discovery" = local.name
}
}
}
tags = local.tags
}
resource "aws_ec2_tag" "cluster_primary_security_group"{
for_each = { for k, v in merge(var.tags, local.karpenter_node_sg_tags) :
k => v if var.create && k != "Name" && var.create_cluster_primary_security_group_tags && v != null
}
resource_id = module.eks.cluster_security_group_id
key = each.key
value = each.value
}