-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.tf
227 lines (208 loc) · 7.38 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
module "eks" {
count = var.default_addon_enabled ? 0 : 1
source = "terraform-aws-modules/eks/aws"
version = "20.16.0"
vpc_id = var.vpc_id
subnet_ids = var.vpc_private_subnet_ids
enable_irsa = var.irsa_enabled
cluster_name = format("%s-%s", var.environment, var.name)
create_kms_key = var.kms_key_enabled
cluster_version = var.cluster_version
cluster_enabled_log_types = var.cluster_log_types
tags = {
"Name" = format("%s-%s", var.environment, var.name)
"Environment" = var.environment
}
access_entries = var.access_entry_enabled ? var.access_entries : null
enable_cluster_creator_admin_permissions = var.enable_cluster_creator_admin_permissions
authentication_mode = var.authentication_mode
cluster_security_group_additional_rules = var.cluster_security_group_additional_rules
cluster_endpoint_public_access = var.cluster_endpoint_public_access
cluster_endpoint_private_access = var.cluster_endpoint_private_access
cluster_endpoint_public_access_cidrs = var.cluster_endpoint_public_access_cidrs
cloudwatch_log_group_retention_in_days = var.cluster_log_retention_in_days
cloudwatch_log_group_kms_key_id = var.kms_key_arn
cluster_encryption_config = {
provider_key_arn = var.kms_key_arn
resources = ["secrets"]
}
cluster_ip_family = var.ipv6_enabled ? "ipv6" : null
cluster_addons = var.enable_vpc_cni_addon ? {
vpc-cni = {
before_compute = true
addon_version = var.vpc_cni_version
configuration_values = jsonencode({
env = {
ENABLE_PREFIX_DELEGATION = "true"
WARM_PREFIX_TARGET = "1"
}
enableNetworkPolicy = "true"
})
service_account_role_arn = module.vpc_cni_irsa_role[0].iam_role_arn
}
} : {}
}
module "vpc_cni_irsa_role" {
count = var.enable_vpc_cni_addon ? 1 : 0
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
version = "5.52.2"
role_name = format("%s-%s-%s", var.environment, var.name, "aws-node-irsa")
attach_vpc_cni_policy = true
vpc_cni_enable_ipv4 = var.ipv6_enabled ? false : true
vpc_cni_enable_ipv6 = var.ipv6_enabled ? true : false
oidc_providers = {
oidc = {
provider_arn = module.eks[0].oidc_provider_arn
namespace_service_accounts = ["kube-system:aws-node"]
}
}
}
resource "aws_iam_policy" "kubernetes_pvc_kms_policy" {
name = format("%s-%s-%s", var.environment, var.name, "kubernetes-pvc-kms-policy")
description = "Allow kubernetes pvc to get access of KMS."
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:Decrypt",
"kms:CreateGrant",
"kms:GenerateDataKeyWithoutPlaintext"
],
"Resource": "${var.kms_key_arn}"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "kms_cluster_policy_attachment" {
role = module.eks[0].cluster_iam_role_name
policy_arn = aws_iam_policy.kubernetes_pvc_kms_policy.arn
}
resource "aws_iam_role" "node_role" {
name = format("%s-%s-node-role", var.environment, var.name)
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
tags = merge(
{ "Name" = format("%s-%s-node-role", var.environment, var.name)
"Environment" = var.environment
}
)
}
data "aws_iam_policy" "SSMManagedInstanceCore" {
arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
resource "aws_iam_role_policy_attachment" "SSMManagedInstanceCore_attachment" {
role = aws_iam_role.node_role.name
policy_arn = data.aws_iam_policy.SSMManagedInstanceCore.arn
}
data "aws_iam_policy" "S3Access" {
arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"
}
resource "aws_iam_role_policy_attachment" "S3Access_attachment" {
role = aws_iam_role.node_role.name
policy_arn = data.aws_iam_policy.S3Access.arn
}
resource "aws_iam_policy" "node_autoscaler_policy" {
name = format("%s-%s-node-autoscaler-policy", var.environment, var.name)
path = "/"
description = "Node auto scaler policy for node groups."
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"ec2:DescribeLaunchTemplateVersions",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:TerminateInstanceInAutoScalingGroup"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "node_autoscaler_policy" {
role = aws_iam_role.node_role.name
policy_arn = aws_iam_policy.node_autoscaler_policy.arn
}
resource "aws_iam_policy" "cni_ipv6_policy" {
count = var.ipv6_enabled == true ? 1 : 0
name = format("%s-%s-eks-cni-ipv6-policy", var.environment, var.name)
path = "/"
description = "Node cni policy for node groups."
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:AssignIpv6Addresses",
"ec2:DescribeInstances",
"ec2:DescribeTags",
"ec2:DescribeNetworkInterfaces",
"ec2:DescribeInstanceTypes"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ec2:CreateTags"
],
"Resource": [
"arn:aws:ec2:*:*:network-interface/*"
]
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "kms_worker_policy_attachment" {
role = aws_iam_role.node_role.name
policy_arn = aws_iam_policy.kubernetes_pvc_kms_policy.arn
}
resource "aws_iam_role_policy_attachment" "worker_policy" {
role = aws_iam_role.node_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
}
resource "aws_iam_role_policy_attachment" "cni_policy" {
role = aws_iam_role.node_role.name
policy_arn = var.ipv6_enabled == false ? "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy" : aws_iam_policy.cni_ipv6_policy[0].arn
}
resource "aws_iam_role_policy_attachment" "worker_ecr_policy" {
role = aws_iam_role.node_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
}
resource "aws_iam_role_policy_attachment" "vpc_cni_addons_policy" {
count = var.enable_vpc_cni_addon ? 1 : 0
role = module.vpc_cni_irsa_role[0].iam_role_name
policy_arn = var.ipv6_enabled == false ? "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy" : aws_iam_policy.cni_ipv6_policy[0].arn
}
resource "aws_iam_role_policy_attachment" "kms_vpc_cni_policy_attachment" {
count = var.enable_vpc_cni_addon ? 1 : 0
role = module.vpc_cni_irsa_role[0].iam_role_name
policy_arn = aws_iam_policy.kubernetes_pvc_kms_policy.arn
}