forked from cloudposse/terraform-aws-eks-node-group
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
112 lines (94 loc) · 3.62 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
locals {
aws_policy_prefix = format("arn:%s:iam::aws:policy", join("", data.aws_partition.current.*.partition))
node_role_arn = format("arn:aws:iam::%s:role/%s", join("", data.aws_caller_identity.current.*.account_id), module.label.id)
account_principal = format("arn:aws:iam::%s:root", join("", data.aws_caller_identity.current.*.account_id))
}
data "aws_partition" "current" {
count = local.enabled ? 1 : 0
}
data "aws_caller_identity" "current" {
count = local.enabled ? 1 : 0
}
data "aws_iam_policy_document" "assume_role" {
count = local.enabled ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
dynamic "statement" {
for_each = var.node_role_explicit_self_trust ? [1] : []
content {
sid = "AllowSelfAssume"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = [local.account_principal]
}
condition {
test = "ArnEquals"
variable = "aws:PrincipalArn"
values = [local.node_role_arn]
}
}
}
}
data "aws_iam_policy_document" "amazon_eks_worker_node_autoscale_policy" {
count = (local.enabled && var.worker_role_autoscale_iam_enabled) ? 1 : 0
statement {
sid = "AllowToScaleEKSNodeGroupAutoScalingGroup"
actions = [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeAutoScalingInstances",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"autoscaling:SetDesiredCapacity",
"autoscaling:TerminateInstanceInAutoScalingGroup",
"ec2:DescribeLaunchTemplateVersions"
]
resources = [
"*"
]
}
}
resource "aws_iam_policy" "amazon_eks_worker_node_autoscale_policy" {
count = (local.enabled && var.worker_role_autoscale_iam_enabled) ? 1 : 0
name = "${module.label.id}-autoscale"
policy = join("", data.aws_iam_policy_document.amazon_eks_worker_node_autoscale_policy.*.json)
}
resource "aws_iam_role" "default" {
count = local.enabled ? 1 : 0
name = module.label.id
assume_role_policy = join("", data.aws_iam_policy_document.assume_role.*.json)
permissions_boundary = var.permissions_boundary
tags = module.label.tags
}
resource "aws_iam_role_policy_attachment" "amazon_eks_worker_node_policy" {
count = local.enabled ? 1 : 0
policy_arn = format("%s/%s", local.aws_policy_prefix, "AmazonEKSWorkerNodePolicy")
role = join("", aws_iam_role.default.*.name)
}
resource "aws_iam_role_policy_attachment" "amazon_eks_worker_node_autoscale_policy" {
count = (local.enabled && var.worker_role_autoscale_iam_enabled) ? 1 : 0
policy_arn = join("", aws_iam_policy.amazon_eks_worker_node_autoscale_policy.*.arn)
role = join("", aws_iam_role.default.*.name)
}
resource "aws_iam_role_policy_attachment" "amazon_eks_cni_policy" {
count = local.enabled ? 1 : 0
policy_arn = format("%s/%s", local.aws_policy_prefix, "AmazonEKS_CNI_Policy")
role = join("", aws_iam_role.default.*.name)
}
resource "aws_iam_role_policy_attachment" "amazon_ec2_container_registry_read_only" {
count = local.enabled ? 1 : 0
policy_arn = format("%s/%s", local.aws_policy_prefix, "AmazonEC2ContainerRegistryReadOnly")
role = join("", aws_iam_role.default.*.name)
}
resource "aws_iam_role_policy_attachment" "existing_policies_for_eks_workers_role" {
for_each = local.enabled ? toset(var.existing_workers_role_policy_arns) : []
policy_arn = each.value
role = join("", aws_iam_role.default.*.name)
}