-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam.tf
142 lines (132 loc) · 4.16 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
resource "aws_iam_policy" "external_dns_policy" {
name = "${module.eks_cluster.eks_cluster_id}-external-dns"
description = "policy to allow k8s external dns for ${module.eks_cluster.eks_cluster_id}"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"route53:ChangeResourceRecordSets"
],
"Resource" : [
"arn:aws:route53:::hostedzone/*"
]
},
{
"Effect" : "Allow",
"Action" : [
"route53:ListHostedZones",
"route53:ListResourceRecordSets"
],
"Resource" : [
"*"
]
}
]
})
}
resource "aws_iam_policy" "efs_csi_policy" {
name = "${module.eks_cluster.eks_cluster_id}-efs_csi_policy"
description = "${module.eks_cluster.eks_cluster_id} efs csi policy"
policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Action" : [
"elasticfilesystem:DescribeAccessPoints",
"elasticfilesystem:DescribeFileSystems"
],
"Resource" : "*"
},
{
"Effect" : "Allow",
"Action" : [
"elasticfilesystem:CreateAccessPoint"
],
"Resource" : "*",
"Condition" : {
"StringLike" : {
"aws:RequestTag/efs.csi.aws.com/cluster" : "true"
}
}
},
{
"Effect" : "Allow",
"Action" : "elasticfilesystem:DeleteAccessPoint",
"Resource" : "*",
"Condition" : {
"StringEquals" : {
"aws:ResourceTag/efs.csi.aws.com/cluster" : "true"
}
}
}
]
})
}
resource "aws_iam_role" "efs_csi_role" {
depends_on = [module.eks_cluster.eks_cluster_identity_oidc_issuer_arn]
name = "${module.eks_cluster.eks_cluster_id}-efs-csi-role"
assume_role_policy = jsonencode(
{
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Federated" : "${module.eks_cluster.eks_cluster_identity_oidc_issuer_arn}"
},
"Action" : "sts:AssumeRoleWithWebIdentity",
"Condition" : {
"StringEquals" : {
"${replace(module.eks_cluster.eks_cluster_identity_oidc_issuer, "https://", "")}:sub" : "system:serviceaccount:kube-system:efs-csi-controller-sa",
"${replace(module.eks_cluster.eks_cluster_identity_oidc_issuer, "https://", "")}:aud" : "sts.amazonaws.com"
}
}
}
]
}
)
}
resource "kubernetes_service_account" "efs-csi-controller-sa" {
metadata {
name = "efs-csi-controller-sa"
namespace = "kube-system"
annotations = { "eks.amazonaws.com/role-arn" : "${aws_iam_role.efs_csi_role.arn}" }
}
}
resource "aws_iam_role" "external_dns_controller_role" {
depends_on = [module.eks_cluster.eks_cluster_identity_oidc_issuer_arn]
name = "${module.eks_cluster.eks_cluster_id}-external-dns"
assume_role_policy = jsonencode(
{
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Federated" : "${module.eks_cluster.eks_cluster_identity_oidc_issuer_arn}"
},
"Action" : "sts:AssumeRoleWithWebIdentity",
"Condition" : {
"StringEquals" : {
"${replace(module.eks_cluster.eks_cluster_identity_oidc_issuer, "https://", "")}:sub" : "system:serviceaccount:external-dns:external-dns-controller",
"${replace(module.eks_cluster.eks_cluster_identity_oidc_issuer, "https://", "")}:aud" : "sts.amazonaws.com"
}
}
}
]
}
)
}
resource "aws_iam_role_policy_attachment" "external_dns_policy_external_dns_controller_role" {
policy_arn = aws_iam_policy.external_dns_policy.arn
role = aws_iam_role.external_dns_controller_role.name
depends_on = [aws_iam_role.external_dns_controller_role]
}
resource "aws_iam_role_policy_attachment" "efs_controller" {
policy_arn = aws_iam_policy.efs_csi_policy.arn
role = aws_iam_role.efs_csi_role.name
depends_on = [aws_iam_role.efs_csi_role]
}