-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgrafana-role.tf
126 lines (107 loc) · 3.26 KB
/
grafana-role.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
######################
# Grafana Cloudwatch #
######################
# Grafana datasource for cloudwatch
# Ref: https://github.com/grafana/helm-charts/blob/main/charts/grafana/values.yaml
# Minimal policy permissions
# Ref: https://grafana.com/docs/grafana/latest/datasources/aws-cloudwatch/
resource "aws_iam_policy" "grafana_datasource" {
name_prefix = "grafana"
description = "EKS grafana datasource policy for cluster ${var.cluster_domain_name}"
policy = data.aws_iam_policy_document.grafana_datasource_irsa.json
}
data "aws_iam_policy_document" "grafana_datasource_irsa" {
statement {
actions = [
"logs:DescribeLogGroups",
"logs:GetLogGroupFields",
"logs:StartQuery",
"logs:StopQuery",
"logs:GetQueryResults",
"logs:GetLogEvents"
]
resources = ["*"]
}
statement {
actions = [
"cloudwatch:GetMetricStatistics",
"cloudwatch:DescribeAlarmsForMetric",
"cloudwatch:DescribeAlarmHistory",
"cloudwatch:DescribeAlarms",
"cloudwatch:ListMetrics",
"cloudwatch:GetMetricData",
"cloudwatch:GetInsightRuleReport"
]
resources = ["*"]
}
statement {
actions = [
"ec2:DescribeTags",
"ec2:DescribeInstances",
"ec2:DescribeRegions"
]
resources = ["*"]
}
statement {
actions = [
"tag:GetResources",
]
resources = ["*"]
}
statement {
actions = ["sts:AssumeRole"]
resources = [aws_iam_role.grafana_role.arn]
}
}
# Create role referencing iam-assumable-role-with-oidc,
# as "allow_self_assume_role" is only available on latest module which have a dependency of terraform version >1
# IRSA
locals {
aws_account_id = data.aws_caller_identity.current.account_id
role_name = "grafana.${var.cluster_domain_name}"
}
data "aws_caller_identity" "current" {}
data "aws_iam_policy_document" "assume_role_with_oidc" {
statement {
# https://aws.amazon.com/blogs/security/announcing-an-update-to-iam-role-trust-policy-behavior/
sid = "ExplicitSelfRoleAssumption"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "ArnLike"
variable = "aws:PrincipalArn"
values = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:role/${local.role_name}"]
}
}
statement {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = ["arn:aws:iam::${local.aws_account_id}:oidc-provider/${var.eks_cluster_oidc_issuer_url}"]
}
condition {
test = "StringEquals"
variable = "${var.eks_cluster_oidc_issuer_url}:sub"
values = ["system:serviceaccount:monitoring:prometheus-operator-grafana"]
}
}
}
resource "aws_iam_role" "grafana_role" {
name = "grafana.${var.cluster_domain_name}"
description = "iam-assumable-role-with-oidc"
max_session_duration = "3600"
assume_role_policy = data.aws_iam_policy_document.assume_role_with_oidc.json
tags = {
Terraform = "true"
Cluster = var.cluster_domain_name
}
}
resource "aws_iam_role_policy_attachment" "custom" {
role = aws_iam_role.grafana_role.name
policy_arn = aws_iam_policy.grafana_datasource.arn
}