generated from stackxcloud/template-terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
48 lines (40 loc) · 1.71 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
# --------------------------------------------------------------------------
# Worker - IAM role & Instance Profile
# --------------------------------------------------------------------------
data "aws_iam_policy_document" "tr" {
count = var.node_role_arn == null ? 1 : 0
statement {
actions = ["sts:AssumeRole"]
principals {
identifiers = ["ec2.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_iam_role" "eks_worker" {
count = var.node_role_arn == null ? 1 : 0
// expected length of name to be in the range (1 - 64)
name = substr(lower("${var.cluster_name}-eks-worker-${var.name}-${random_string.random_name.result}"), 0, 63)
assume_role_policy = data.aws_iam_policy_document.tr.0.json
force_detach_policies = true
tags = local.tags
}
resource "aws_iam_instance_profile" "eks_worker" {
count = var.node_role_arn == null ? 1 : 0
# https://docs.aws.amazon.com/IAM/latest/APIReference/API_InstanceProfile.html
# Minimum length of 1.
# Maximum length of 128.
name = substr(lower("${var.cluster_name}-eks-worker-${var.name}-${random_string.random_name.result}"), 0, 127)
role = aws_iam_role.eks_worker.0.name
tags = local.tags
}
resource "aws_iam_role_policy_attachment" "attach" {
for_each = toset(flatten([
"arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly",
"arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonEKSWorkerNodePolicy",
"arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonSSMManagedInstanceCore",
var.list_policies_arns,
]))
policy_arn = each.key
role = var.node_role_arn == null ? aws_iam_role.eks_worker.0.name : var.node_role_arn
}