-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmain.tf
169 lines (145 loc) · 3.96 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
/*
EKS control plane
*/
resource "aws_eks_cluster" "control_plane" {
name = var.name
role_arn = local.eks_cluster_role_arn
tags = var.tags
version = local.versions.k8s
enabled_cluster_log_types = var.cluster_log_types
vpc_config {
endpoint_private_access = true
endpoint_public_access = var.endpoint_public_access
public_access_cidrs = var.endpoint_public_access_cidrs
security_group_ids = concat(aws_security_group.control_plane.*.id, var.security_group_ids)
subnet_ids = concat(values(var.vpc_config.public_subnet_ids), values(var.vpc_config.private_subnet_ids))
}
encryption_config {
resources = ["secrets"]
provider {
key_arn = local.kms_cmk_arn
}
}
depends_on = [aws_cloudwatch_log_group.control_plane]
}
resource "aws_iam_openid_connect_provider" "cluster_oidc" {
url = aws_eks_cluster.control_plane.identity.0.oidc.0.issuer
thumbprint_list = var.oidc_root_ca_thumbprints
client_id_list = ["sts.amazonaws.com"]
}
resource "aws_cloudwatch_log_group" "control_plane" {
name = "/aws/eks/${var.name}/cluster"
retention_in_days = 7
tags = var.tags
kms_key_id = local.kms_cmk_arn
}
/*
Allow nodes to join the cluster
*/
locals {
aws_auth_configmap_data = {
mapRoles = yamlencode(concat(
[
{
rolearn = aws_iam_role.fargate.arn
username = "system:node:{{SessionName}}"
groups = [
"system:bootstrappers",
"system:nodes",
"system:node-proxier",
]
},
],
var.aws_auth_role_map,
))
mapUsers = yamlencode(var.aws_auth_user_map)
}
}
resource "kubernetes_config_map" "aws_auth" {
metadata {
name = "aws-auth"
namespace = "kube-system"
}
data = local.aws_auth_configmap_data
lifecycle {
# We are ignoring the data here since we will manage it with the resource below
# This is only intended to be used in scenarios where the configmap does not exist
ignore_changes = [data, metadata[0].labels, metadata[0].annotations]
}
}
resource "kubernetes_config_map_v1_data" "aws_auth" {
force = true
metadata {
name = "aws-auth"
namespace = "kube-system"
}
data = local.aws_auth_configmap_data
depends_on = [
# Required for instances where the configmap does not exist yet to avoid race condition
kubernetes_config_map.aws_auth,
]
}
locals {
create_key = length(var.kms_cmk_arn) == 0
kms_cmk_arn = local.create_key ? aws_kms_key.cmk.*.arn[0] : var.kms_cmk_arn
}
data "aws_iam_policy_document" "cloudwatch" {
policy_id = "key-policy-cloudwatch"
statement {
sid = "Enable IAM User Permissions"
actions = [
"kms:*",
]
effect = "Allow"
principals {
type = "AWS"
identifiers = [
format(
"arn:%s:iam::%s:root",
data.aws_partition.current.partition,
data.aws_caller_identity.current.account_id
)
]
}
resources = ["*"]
}
statement {
sid = "AllowCloudWatchLogs"
actions = [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
]
effect = "Allow"
principals {
type = "Service"
identifiers = [
format(
"logs.%s.amazonaws.com",
data.aws_region.current.name
)
]
}
resources = ["*"]
condition {
test = "ArnEquals"
variable = "kms:EncryptionContext:aws:logs:arn"
values = [
format(
"arn:aws:logs:%s:%s:log-group:/aws/eks/%s/cluster",
data.aws_region.current.name,
data.aws_caller_identity.current.account_id,
var.name,
)
]
}
}
}
resource "aws_kms_key" "cmk" {
count = local.create_key ? 1 : 0
description = "eks secrets cmk: ${var.name}"
enable_key_rotation = true
policy = data.aws_iam_policy_document.cloudwatch.json
}