-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam.tf
111 lines (90 loc) · 3.34 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
locals {
create_iam_role = var.iam_role_arn == null ? true : false
data_source_iam_policies = {
ATHENA = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonGrafanaAthenaAccess"
CLOUDWATCH = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonGrafanaCloudWatchAccess"
REDSHIFT = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonGrafanaRedshiftAccess"
SITEWISE = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSIoTSiteWiseReadOnlyAccess"
TIMESTREAM = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonTimestreamReadOnlyAccess"
XRAY = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSXrayReadOnlyAccess"
}
}
data "aws_iam_policy_document" "assume_policy" {
count = local.create_iam_role ? 1 : 0
statement {
sid = "GrafanaAssume"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["grafana.${data.aws_partition.current.dns_suffix}"]
}
}
}
data "aws_iam_policy_document" "default" {
count = local.create_iam_role ? 1 : 0
dynamic "statement" {
for_each = contains(var.data_sources, "AMAZON_OPENSEARCH_SERVICE") ? { create : true } : {}
content {
actions = [
"es:ESHttpGet",
"es:DescribeElasticsearchDomains",
"es:ListDomainNames",
]
resources = ["*"]
}
}
dynamic "statement" {
for_each = contains(var.data_sources, "AMAZON_OPENSEARCH_SERVICE") ? { create : true } : {}
content {
actions = ["es:ESHttpGet"]
resources = [
"arn:${data.aws_partition.current.partition}:es:*:*:domain/*/_msearch*",
"arn:${data.aws_partition.current.partition}:es:*:*:domain/*/_opendistro/_ppl",
]
}
}
dynamic "statement" {
for_each = contains(var.data_sources, "PROMETHEUS") ? { create : true } : {}
content {
actions = [
"aps:ListWorkspaces",
"aps:DescribeWorkspace",
"aps:QueryMetrics",
"aps:GetLabels",
"aps:GetSeries",
"aps:GetMetricMetadata",
]
resources = ["*"]
}
}
dynamic "statement" {
for_each = contains(var.notification_destinations, "SNS") ? { create : true } : {}
content {
actions = ["sns:Publish"]
resources = ["arn:${data.aws_partition.current.partition}:sns:*:${data.aws_caller_identity.current.account_id}:grafana*"]
}
}
}
resource "aws_iam_role" "default" {
count = local.create_iam_role ? 1 : 0
name = "GrafanaExecutionRole-${var.name}"
assume_role_policy = data.aws_iam_policy_document.assume_policy[0].json
tags = var.tags
}
resource "aws_iam_policy" "default" {
count = local.create_iam_role ? 1 : 0
name = "GrafanaExecutionRolePolicy-${var.name}"
policy = data.aws_iam_policy_document.default[0].json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "data_sources" {
for_each = { for i, v in var.data_sources : v => local.data_source_iam_policies[v] if local.create_iam_role }
role = aws_iam_role.default[0].name
policy_arn = each.value
}
resource "aws_iam_role_policy_attachment" "default" {
count = local.create_iam_role ? 1 : 0
role = aws_iam_role.default[0].name
policy_arn = aws_iam_policy.default[0].arn
}