-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam_external_dns.tf
66 lines (58 loc) · 2.04 KB
/
iam_external_dns.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
locals {
oidc_url = replace(module.eks.cluster_oidc_issuer_url, "https://", "")
}
# 1. external-dns IAM Role Policy Document
data "aws_iam_policy_document" "external_dns_policy_doc" {
statement {
sid = "ChangeResourceRecordSets"
effect = "Allow"
actions = ["route53:ChangeResourceRecordSets"]
resources = ["arn:${var.arn_format}:route53:::hostedzone/${local.public_hosted_zone_id}"]
}
statement {
sid = "ListResourceRecordSets"
actions = [
"route53:ListHostedZones",
"route53:ListResourceRecordSets",
"route53:ListTagsForResource",
]
resources = ["*"]
effect = "Allow"
}
}
# 2. external-dns IAM Role Policy
resource "aws_iam_policy" "external_dns_policy" {
count = var.aws_public_hosted_zone == null ? 0 : 1
name = "${var.module_prefix}-external-dns"
path = "/"
description = "Policy for external-dns service"
policy = data.aws_iam_policy_document.external_dns_policy_doc.json
}
# 3. external-dns Assume Role Policy Document
data "aws_iam_policy_document" "external_dns_irsa_assume_role_policy_doc" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]
effect = "Allow"
principals {
type = "Federated"
identifiers = [module.eks.oidc_provider_arn]
}
condition {
test = "StringEquals"
variable = "${local.oidc_url}:sub"
values = ["system:serviceaccount:kube-system:${var.module_prefix}-external-dns"]
}
}
}
# 4. external-dns IAM Role
resource "aws_iam_role" "external_dns_role" {
count = var.aws_public_hosted_zone == null ? 0 : 1
name = "${var.module_prefix}-external-dns"
assume_role_policy = data.aws_iam_policy_document.external_dns_irsa_assume_role_policy_doc.json
}
# 5. external-dns IAM Role Policy Attachment
resource "aws_iam_role_policy_attachment" "external_dns_policy_attachment" {
count = var.aws_public_hosted_zone == null ? 0 : 1
role = aws_iam_role.external_dns_role[0].name
policy_arn = aws_iam_policy.external_dns_policy[0].arn
}