This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
forked from cloudposse/terraform-aws-eks-node-group
-
Notifications
You must be signed in to change notification settings - Fork 2
/
iam.tf
100 lines (84 loc) · 3.29 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
locals {
create_role = local.enabled && length(var.node_role_arn) == 0
aws_policy_prefix = local.create_role ? format("arn:%s:iam::aws:policy", join("", data.aws_partition.current.*.partition)) : ""
node_role_policy_arns = sort(var.node_role_policy_arns)
}
data "aws_partition" "current" {
count = local.create_role ? 1 : 0
}
data "aws_iam_policy_document" "assume_role" {
count = local.create_role ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "default" {
count = local.create_role ? 1 : 0
name = module.label.id
assume_role_policy = join("", data.aws_iam_policy_document.assume_role.*.json)
permissions_boundary = var.node_role_permissions_boundary
tags = module.label.tags
}
resource "aws_iam_role_policy_attachment" "amazon_eks_worker_node_policy" {
count = local.create_role ? 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_ec2_container_registry_read_only" {
count = local.create_role ? 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" {
count = local.create_role ? length(var.node_role_policy_arns) : 0
policy_arn = local.node_role_policy_arns[count.index]
role = join("", aws_iam_role.default.*.name)
}
# Create a CNI policy that is a merger of AmazonEKS_CNI_Policy and required IPv6 permissions
# https://github.com/SummitRoute/aws_managed_policies/blob/master/policies/AmazonEKS_CNI_Policy
# https://docs.aws.amazon.com/eks/latest/userguide/cni-iam-role.html#cni-iam-role-create-ipv6-policy
data "aws_iam_policy_document" "ipv6_eks_cni_policy" {
count = local.create_role && var.node_role_cni_policy_enabled ? 1 : 0
statement {
effect = "Allow"
actions = [
"ec2:AssignIpv6Addresses",
"ec2:AssignPrivateIpAddresses",
"ec2:AttachNetworkInterface",
"ec2:CreateNetworkInterface",
"ec2:DeleteNetworkInterface",
"ec2:DescribeInstances",
"ec2:DescribeInstanceTypes",
"ec2:DescribeTags",
"ec2:DescribeNetworkInterfaces",
"ec2:DetachNetworkInterface",
"ec2:ModifyNetworkInterfaceAttribute",
"ec2:UnassignPrivateIpAddresses"
]
resources = ["*"]
}
statement {
effect = "Allow"
actions = [
"ec2:CreateTags"
]
resources = [
"arn:${join("", data.aws_partition.current.*.partition)}:ec2:*:*:network-interface/*"
]
}
}
resource "aws_iam_policy" "ipv6_eks_cni_policy" {
count = local.create_role && var.node_role_cni_policy_enabled ? 1 : 0
name = "${module.this.id}-CNI_Policy"
policy = join("", data.aws_iam_policy_document.ipv6_eks_cni_policy.*.json)
}
resource "aws_iam_role_policy_attachment" "ipv6_eks_cni_policy" {
count = local.create_role && var.node_role_cni_policy_enabled ? 1 : 0
policy_arn = join("", aws_iam_policy.ipv6_eks_cni_policy.*.arn)
role = join("", aws_iam_role.default.*.name)
}