-
Notifications
You must be signed in to change notification settings - Fork 2
/
iam.tf
196 lines (152 loc) · 5.1 KB
/
iam.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
/*
* Copyright (c) 2019 Netic A/S. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
data "aws_iam_policy_document" "assume_cluster_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["eks.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
name = "eks-cluster-${var.name}"
description = "Used for managing the EKS cluster"
assume_role_policy = data.aws_iam_policy_document.assume_cluster_role.json
tags = merge(var.tags, local.tags)
}
resource "aws_iam_role_policy_attachment" "eks_cluster_AmazonEKSClusterPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.this.name
}
resource "aws_iam_role_policy_attachment" "eks_cluster_AmazonEKSServicePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
role = aws_iam_role.this.name
}
// Global cluster admins role
data "aws_iam_policy_document" "account_assume_cluster_admin_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${var.cluster_admin_account_id}:root"]
}
}
}
resource "aws_iam_role" "cluster_admin" {
name = var.global_cluster_admin_role
description = "Used for granting access to the EKS cluster for admins"
assume_role_policy = data.aws_iam_policy_document.account_assume_cluster_admin_role.json
tags = merge(var.tags, local.tags)
}
// Workers role
data "aws_iam_policy_document" "assume_workers_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "workers" {
name = "eks-workers-${var.name}"
description = "Allows EKS worker nodes to interact with AWS"
assume_role_policy = data.aws_iam_policy_document.assume_workers_role.json
tags = merge(var.tags, local.tags)
}
resource "aws_iam_role_policy_attachment" "eks_workers_AmazonEKSWorkerNodePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
role = join("", aws_iam_role.workers.*.name)
}
resource "aws_iam_role_policy_attachment" "eks_workers_AmazonEKS_CNI_Policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
role = join("", aws_iam_role.workers.*.name)
}
resource "aws_iam_role_policy_attachment" "eks_workers_AmazonEC2ContainerRegistryReadOnly" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
role = join("", aws_iam_role.workers.*.name)
}
resource "aws_iam_role_policy_attachment" "eks_workers_AmazonCWAgentServerPolicy" {
policy_arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
role = join("", aws_iam_role.workers.*.name)
}
resource "aws_iam_instance_profile" "workers" {
name = "eks-workers-${var.name}"
role = join("", aws_iam_role.workers.*.name)
}
// Workers autoscaling
data "aws_iam_policy_document" "worker_autoscaling" {
statement {
sid = "eksWorkerAutoscalingAll"
effect = "Allow"
actions = [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
]
resources = ["*"]
}
statement {
sid = "eksWorkerAutoscalingOwn"
effect = "Allow"
actions = [
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"autoscaling:UpdateAutoScalingGroup",
]
resources = ["*"]
condition {
test = "StringEquals"
variable = "autoscaling:ResourceTag/kubernetes.io/cluster/${var.name}"
values = ["owned"]
}
condition {
test = "StringEquals"
variable = "autoscaling:ResourceTag/k8s.io/cluster-autoscaler/enabled"
values = ["true"]
}
}
}
resource "aws_iam_policy" "worker_autoscaling" {
name_prefix = "eks-workers-autoscaling-${var.name}"
description = "EKS worker node autoscaling policy for cluster ${var.name}"
policy = data.aws_iam_policy_document.worker_autoscaling.json
}
resource "aws_iam_role_policy_attachment" "workers_autoscaling" {
policy_arn = aws_iam_policy.worker_autoscaling.arn
role = aws_iam_role.workers.name
}
// Route53
data "aws_iam_policy_document" "dns_management" {
statement {
effect = "Allow"
actions = ["route53:ChangeResourceRecordSets"]
resources = ["arn:aws:route53:::hostedzone/*"]
}
statement {
effect = "Allow"
actions = [
"route53:ListHostedZones",
"route53:ListResourceRecordSets",
]
resources = ["*"]
}
}
resource "aws_iam_policy" "dns_management" {
name_prefix = "eks-workers-dns-${var.name}"
description = "EKS worker node can manage Route 53 for cluster ${var.name}"
policy = data.aws_iam_policy_document.dns_management.json
}
resource "aws_iam_role_policy_attachment" "dns_management" {
policy_arn = aws_iam_policy.dns_management.arn
role = aws_iam_role.workers.name
}