forked from lablabs/terraform-aws-eks-external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam.tf
102 lines (74 loc) · 2.27 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
locals {
assume_role = length(var.k8s_assume_role_arn) > 0 ? true : false
}
data "aws_iam_policy_document" "external_dns" {
count = local.k8s_irsa_role_create && !local.assume_role ? 1 : 0
statement {
sid = "ChangeResourceRecordSets"
actions = [
"route53:ChangeResourceRecordSets",
]
resources = [for id in var.policy_allowed_zone_ids : "arn:aws:route53:::hostedzone/${id}"]
effect = "Allow"
}
statement {
sid = "ListResourceRecordSets"
actions = [
"route53:ListHostedZones",
"route53:ListResourceRecordSets",
"route53:ListTagsForResource",
]
resources = [
"*",
]
effect = "Allow"
}
}
data "aws_iam_policy_document" "external_dns_assume" {
count = local.k8s_irsa_role_create && local.assume_role ? 1 : 0
statement {
sid = "AllowAssumeExternalDNSRole"
effect = "Allow"
actions = [
"sts:AssumeRole"
]
resources = [
var.k8s_assume_role_arn
]
}
}
resource "aws_iam_policy" "external_dns" {
count = local.k8s_irsa_role_create ? 1 : 0
name = "${var.cluster_name}-external-dns"
path = "/"
description = "Policy for external-dns service"
policy = local.assume_role ? data.aws_iam_policy_document.external_dns_assume[0].json : data.aws_iam_policy_document.external_dns[0].json
}
data "aws_iam_policy_document" "external_dns_irsa" {
count = local.k8s_irsa_role_create ? 1 : 0
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [var.cluster_identity_oidc_issuer_arn]
}
condition {
test = "StringEquals"
variable = "${replace(var.cluster_identity_oidc_issuer, "https://", "")}:sub"
values = [
"system:serviceaccount:${var.k8s_namespace}:${var.k8s_service_account_name}",
]
}
effect = "Allow"
}
}
resource "aws_iam_role" "external_dns" {
count = local.k8s_irsa_role_create ? 1 : 0
name = "${var.cluster_name}-external-dns"
assume_role_policy = data.aws_iam_policy_document.external_dns_irsa[0].json
}
resource "aws_iam_role_policy_attachment" "external_dns" {
count = local.k8s_irsa_role_create ? 1 : 0
role = aws_iam_role.external_dns[0].name
policy_arn = aws_iam_policy.external_dns[0].arn
}