-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.tf
445 lines (382 loc) · 11.6 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
locals {
aws_alb_ingress_controller_docker_image = "docker.io/amazon/aws-alb-ingress-controller:v${var.aws_alb_ingress_controller_version}"
aws_alb_ingress_controller_version = var.aws_alb_ingress_controller_version
aws_alb_ingress_class = "alb"
aws_vpc_id = data.aws_vpc.selected.id
aws_region_name = data.aws_region.current.name
aws_iam_path_prefix = var.aws_iam_path_prefix == "" ? null : var.aws_iam_path_prefix
}
data "aws_vpc" "selected" {
id = var.k8s_cluster_type == "eks" ? data.aws_eks_cluster.selected[0].vpc_config[0].vpc_id : var.aws_vpc_id
}
data "aws_region" "current" {
name = var.aws_region_name
}
data "aws_caller_identity" "current" {}
# The EKS cluster (if any) that represents the installation target.
data "aws_eks_cluster" "selected" {
count = var.k8s_cluster_type == "eks" ? 1 : 0
name = var.k8s_cluster_name
}
data "aws_iam_policy_document" "ec2_assume_role" {
count = var.k8s_cluster_type == "vanilla" ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "eks_oidc_assume_role" {
count = var.k8s_cluster_type == "eks" ? 1 : 0
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
condition {
test = "StringEquals"
variable = "${replace(data.aws_eks_cluster.selected[0].identity[0].oidc[0].issuer, "https://", "")}:sub"
values = [
"system:serviceaccount:${var.k8s_namespace}:aws-alb-ingress-controller"
]
}
principals {
identifiers = [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/${replace(data.aws_eks_cluster.selected[0].identity[0].oidc[0].issuer, "https://", "")}"
]
type = "Federated"
}
}
}
resource "aws_iam_role" "this" {
name = "${var.aws_resource_name_prefix}${var.k8s_cluster_name}-alb-ingress-controller"
description = "Permissions required by the Kubernetes AWS ALB Ingress controller to do it's job."
path = local.aws_iam_path_prefix
tags = var.aws_tags
force_detach_policies = true
assume_role_policy = var.k8s_cluster_type == "vanilla" ? data.aws_iam_policy_document.ec2_assume_role[0].json : data.aws_iam_policy_document.eks_oidc_assume_role[0].json
}
data "aws_iam_policy_document" "alb_management" {
statement {
actions = [
"acm:DescribeCertificate",
"acm:ListCertificates",
"acm:GetCertificate",
]
resources = ["*"]
}
statement {
actions = [
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CreateSecurityGroup",
"ec2:CreateTags",
"ec2:DeleteTags",
"ec2:DeleteSecurityGroup",
"ec2:DescribeAccountAttributes",
"ec2:DescribeAddresses",
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"ec2:DescribeInternetGateways",
"ec2:DescribeNetworkInterfaces",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:DescribeTags",
"ec2:DescribeVpcs",
"ec2:ModifyInstanceAttribute",
"ec2:ModifyNetworkInterfaceAttribute",
"ec2:RevokeSecurityGroupIngress",
]
resources = ["*"]
}
statement {
actions = [
"elasticloadbalancing:AddListenerCertificates",
"elasticloadbalancing:AddTags",
"elasticloadbalancing:CreateListener",
"elasticloadbalancing:CreateLoadBalancer",
"elasticloadbalancing:CreateRule",
"elasticloadbalancing:CreateTargetGroup",
"elasticloadbalancing:DeleteListener",
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:DeleteRule",
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:DeregisterTargets",
"elasticloadbalancing:DescribeListenerCertificates",
"elasticloadbalancing:DescribeListeners",
"elasticloadbalancing:DescribeLoadBalancers",
"elasticloadbalancing:DescribeLoadBalancerAttributes",
"elasticloadbalancing:DescribeRules",
"elasticloadbalancing:DescribeSSLPolicies",
"elasticloadbalancing:DescribeTags",
"elasticloadbalancing:DescribeTargetGroups",
"elasticloadbalancing:DescribeTargetGroupAttributes",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:ModifyListener",
"elasticloadbalancing:ModifyLoadBalancerAttributes",
"elasticloadbalancing:ModifyRule",
"elasticloadbalancing:ModifyTargetGroup",
"elasticloadbalancing:ModifyTargetGroupAttributes",
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:RemoveListenerCertificates",
"elasticloadbalancing:RemoveTags",
"elasticloadbalancing:SetIpAddressType",
"elasticloadbalancing:SetSecurityGroups",
"elasticloadbalancing:SetSubnets",
"elasticloadbalancing:SetWebACL",
]
resources = ["*"]
}
statement {
actions = [
"iam:CreateServiceLinkedRole",
"iam:GetServerCertificate",
"iam:ListServerCertificates",
]
resources = ["*"]
}
statement {
actions = [
"cognito-idp:DescribeUserPoolClient",
]
resources = ["*"]
}
statement {
actions = [
"tag:GetResources",
"tag:TagResources",
]
resources = ["*"]
}
statement {
actions = [
"waf:GetWebACL",
"waf-regional:GetWebACLForResource",
"waf-regional:GetWebACL",
"waf-regional:AssociateWebACL",
"waf-regional:DisassociateWebACL",
]
resources = ["*"]
}
statement {
actions = [
"wafv2:GetWebACL",
"wafv2:GetWebACLForResource",
"wafv2:AssociateWebACL",
"wafv2:DisassociateWebACL"
]
resources = ["*"]
}
statement {
actions = [
"shield:DescribeProtection",
"shield:GetSubscriptionState",
"shield:DeleteProtection",
"shield:CreateProtection",
"shield:DescribeSubscription",
"shield:ListProtections"
]
resources = ["*"]
}
}
resource "aws_iam_policy" "this" {
name = "${var.aws_resource_name_prefix}${var.k8s_cluster_name}-alb-management"
description = "Permissions that are required to manage AWS Application Load Balancers."
path = local.aws_iam_path_prefix
policy = data.aws_iam_policy_document.alb_management.json
}
resource "aws_iam_role_policy_attachment" "this" {
policy_arn = aws_iam_policy.this.arn
role = aws_iam_role.this.name
}
resource "kubernetes_service_account" "this" {
automount_service_account_token = true
metadata {
name = "aws-alb-ingress-controller"
namespace = var.k8s_namespace
annotations = {
# This annotation is only used when running on EKS which can
# use IAM roles for service accounts.
"eks.amazonaws.com/role-arn" = aws_iam_role.this.arn
}
labels = {
"app.kubernetes.io/name" = "aws-alb-ingress-controller"
"app.kubernetes.io/managed-by" = "terraform"
}
}
}
resource "kubernetes_cluster_role" "this" {
metadata {
name = "aws-alb-ingress-controller"
labels = {
"app.kubernetes.io/name" = "aws-alb-ingress-controller"
"app.kubernetes.io/managed-by" = "terraform"
}
}
rule {
api_groups = [
"",
"extensions",
]
resources = [
"configmaps",
"endpoints",
"events",
"ingresses",
"ingresses/status",
"services",
]
verbs = [
"create",
"get",
"list",
"update",
"watch",
"patch",
]
}
rule {
api_groups = [
"",
"extensions",
]
resources = [
"nodes",
"pods",
"secrets",
"services",
"namespaces",
]
verbs = [
"get",
"list",
"watch",
]
}
}
resource "kubernetes_cluster_role_binding" "this" {
metadata {
name = "aws-alb-ingress-controller"
labels = {
"app.kubernetes.io/name" = "aws-alb-ingress-controller"
"app.kubernetes.io/managed-by" = "terraform"
}
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = kubernetes_cluster_role.this.metadata[0].name
}
subject {
api_group = ""
kind = "ServiceAccount"
name = kubernetes_service_account.this.metadata[0].name
namespace = kubernetes_service_account.this.metadata[0].namespace
}
}
resource "kubernetes_deployment" "this" {
depends_on = [kubernetes_cluster_role_binding.this]
metadata {
name = "aws-alb-ingress-controller"
namespace = var.k8s_namespace
labels = {
"app.kubernetes.io/name" = "aws-alb-ingress-controller"
"app.kubernetes.io/version" = "v${local.aws_alb_ingress_controller_version}"
"app.kubernetes.io/managed-by" = "terraform"
}
annotations = {
"field.cattle.io/description" = "AWS ALB Ingress Controller"
}
}
spec {
replicas = var.k8s_replicas
selector {
match_labels = {
"app.kubernetes.io/name" = "aws-alb-ingress-controller"
}
}
strategy {
type = "Recreate"
}
template {
metadata {
labels = merge(
{
"app.kubernetes.io/name" = "aws-alb-ingress-controller"
"app.kubernetes.io/version" = local.aws_alb_ingress_controller_version
},
var.k8s_pod_labels
)
annotations = merge(
{
# Annotation which is only used by KIAM and kube2iam.
# Should be ignored by your cluster if using IAM roles for service accounts, e.g.
# when running on EKS.
"iam.amazonaws.com/role" = aws_iam_role.this.arn
},
var.k8s_pod_annotations
)
}
spec {
affinity {
pod_anti_affinity {
preferred_during_scheduling_ignored_during_execution {
weight = 100
pod_affinity_term {
label_selector {
match_expressions {
key = "app.kubernetes.io/name"
operator = "In"
values = ["aws-alb-ingress-controller"]
}
}
topology_key = "kubernetes.io/hostname"
}
}
}
}
automount_service_account_token = true
dns_policy = "ClusterFirst"
restart_policy = "Always"
container {
name = "server"
image = local.aws_alb_ingress_controller_docker_image
image_pull_policy = "Always"
termination_message_path = "/dev/termination-log"
args = [
"--ingress-class=${local.aws_alb_ingress_class}",
"--cluster-name=${var.k8s_cluster_name}",
"--aws-vpc-id=${local.aws_vpc_id}",
"--aws-region=${local.aws_region_name}",
"--aws-max-retries=10",
]
port {
name = "health"
container_port = 10254
protocol = "TCP"
}
readiness_probe {
http_get {
path = "/healthz"
port = "health"
scheme = "HTTP"
}
initial_delay_seconds = 30
period_seconds = 60
timeout_seconds = 3
}
liveness_probe {
http_get {
path = "/healthz"
port = "health"
scheme = "HTTP"
}
initial_delay_seconds = 60
period_seconds = 60
}
}
service_account_name = kubernetes_service_account.this.metadata[0].name
termination_grace_period_seconds = 60
}
}
}
}