-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheks.tf
More file actions
257 lines (226 loc) · 8.35 KB
/
eks.tf
File metadata and controls
257 lines (226 loc) · 8.35 KB
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
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
data "aws_eks_cluster_auth" "this" {
name = module.eks.cluster_name
}
################################################################################
# EKS Module
################################################################################
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "~> 20.0"
enable_cluster_creator_admin_permissions = true
cluster_name = var.cluster_name
cluster_version = var.cluster_version
cluster_endpoint_private_access = true
cluster_endpoint_public_access = true
cluster_ip_family = "ipv4"
cloudwatch_log_group_retention_in_days = "400"
cluster_addons = {
amazon-cloudwatch-observability = {}
}
cluster_encryption_config = {
provider_key_arn = aws_kms_key.eks.arn
resources = ["secrets"]
}
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets
fargate_profiles = {
default = {
name = "default"
cluster_name = var.cluster_name
subnets = module.vpc.private_subnets
iam_role_additional_policies = {
cloudwatch_observability = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
selectors = [{
namespace = var.namespace
}]
}
adm = {
name = "default-adm"
cluster_name = var.cluster_name
subnets = module.vpc.private_subnets
iam_role_additional_policies = {
cloudwatch_observability = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
selectors = [{
namespace = "${var.namespace}-adm"
}]
}
observability = {
name = "observability"
cluster_name = var.cluster_name
subnets = module.vpc.private_subnets
iam_role_additional_policies = {
cloudwatch_observability = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
selectors = [{
namespace = "amazon-cloudwatch"
}]
}
system = {
name = "default-system"
cluster_name = var.cluster_name
subnets = module.vpc.private_subnets
iam_role_additional_policies = {
cloudwatch_observability = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
selectors = [{
namespace = "kube-system"
}]
}
coredns = {
name = "coredns"
cluster_name = var.cluster_name
subnets = module.vpc.private_subnets
iam_role_additional_policies = {
cloudwatch_observability = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
selectors = [{
namespace = "kube-system"
labels = {
k8s-app = "kube-dns"
}
}]
}
}
# Extend cluster security group rules
cluster_security_group_additional_rules = {
egress_nodes_ephemeral_ports_tcp = {
description = "To node 1025-65535"
protocol = "tcp"
from_port = 1025
to_port = 65535
type = "egress"
source_node_security_group = true
}
ingress_self_all = {
description = "Node to node all ports/protocols"
protocol = "-1"
from_port = 0
to_port = 0
type = "ingress"
self = true
}
egress_all = {
description = "Node all egress"
protocol = "-1"
from_port = 0
to_port = 0
type = "egress"
cidr_blocks = ["0.0.0.0/0"]
}
}
# aws-auth configmap
#manage_aws_auth_configmap = true
#aws_auth_roles = var.aws_auth_roles
#aws_auth_users = var.aws_auth_users
cluster_tags = {
# This should not affect the name of the cluster primary security group
# Ref: https://github.com/terraform-aws-modules/terraform-aws-eks/pull/2006
# Ref: https://github.com/terraform-aws-modules/terraform-aws-eks/pull/2008
Name = var.cluster_name,
}
tags = local.tags
}
################################################################################
# Supporting Resources
################################################################################
# This policy is required for the KMS key used for EKS root volumes, so the cluster is allowed to enc/dec/attach encrypted EBS volumes
data "aws_iam_policy_document" "ebs" {
# Copy of default KMS policy that lets you manage it
statement {
sid = "Enable IAM User Permissions"
actions = ["kms:*"]
resources = ["*"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
}
# Required for EKS
statement {
sid = "Allow service-linked role use of the CMK"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:DescribeKey"
]
resources = ["*"]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling", # required for the ASG to manage encrypted volumes for nodes
module.eks.cluster_iam_role_arn, # required for the cluster / persistentvolume-controller to create encrypted PVCs
]
}
}
statement {
sid = "Allow attachment of persistent resources"
actions = ["kms:CreateGrant"]
resources = ["*"]
principals {
type = "AWS"
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling", # required for the ASG to manage encrypted volumes for nodes
module.eks.cluster_iam_role_arn, # required for the cluster / persistentvolume-controller to create encrypted PVCs
]
}
condition {
test = "Bool"
variable = "kms:GrantIsForAWSResource"
values = ["true"]
}
}
}
resource "aws_kms_key" "eks" {
description = "EKS Secret Encryption Key"
deletion_window_in_days = 7
enable_key_rotation = true
tags = local.tags
}
resource "aws_kms_key" "ebs" {
description = "Customer managed key to encrypt EKS managed node group volumes"
deletion_window_in_days = 7
policy = data.aws_iam_policy_document.ebs.json
}
resource "null_resource" "k8s_patcher" {
depends_on = [module.eks.cluster_id]
triggers = {
// fire any time the cluster is update in a way that changes its endpoint or auth
endpoint = module.eks.cluster_endpoint
ca_crt = base64decode(module.eks.cluster_certificate_authority_data)
}
provisioner "local-exec" {
command = <<EOH
cat >/tmp/ca.crt <<EOF
${base64decode(module.eks.cluster_certificate_authority_data)}
EOF
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/$(uname -s | tr '[:upper:]' '[:lower:]')/$(uname -m | tr '[:upper:]' '[:lower:]')/kubectl && chmod +x ./kubectl && \
if [ -n "$(kubectl --server="${module.eks.cluster_endpoint}" --certificate_authority=/tmp/ca.crt --token="${data.aws_eks_cluster_auth.this.token}" get deployment coredns -n kube-system -o 'jsonpath={.spec.template.metadata.annotations}')" ]; then
./kubectl \
--server="${module.eks.cluster_endpoint}" \
--certificate_authority=/tmp/ca.crt \
--token="${data.aws_eks_cluster_auth.this.token}" \
patch deployment coredns \
-n kube-system --type json \
-p='[{"op": "remove", "path": "/spec/template/metadata/annotations/eks.amazonaws.com~1compute-type"}]' && \
./kubectl \
--server="${module.eks.cluster_endpoint}" \
--certificate_authority=/tmp/ca.crt \
--token="${data.aws_eks_cluster_auth.this.token}" \
scale deployment coredns \
-n kube-system --replicas 0 && \
./kubectl \
--server="${module.eks.cluster_endpoint}" \
--certificate_authority=/tmp/ca.crt \
--token="${data.aws_eks_cluster_auth.this.token}" \
scale deployment coredns \
-n kube-system --replicas 2 ; fi && \
rm ./kubectl
EOH
}
}