-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
371 lines (302 loc) · 11.7 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
locals {
workspace_id = var.create_workspace ? try(aws_grafana_workspace.this[0].id, null) : var.workspace_id
}
################################################################################
# Workspace
################################################################################
resource "aws_grafana_workspace" "this" {
count = var.create && var.create_workspace ? 1 : 0
name = var.name
description = var.description
account_access_type = var.account_access_type
authentication_providers = var.authentication_providers
permission_type = var.permission_type
grafana_version = var.grafana_version
configuration = var.configuration
data_sources = var.data_sources
notification_destinations = var.notification_destinations
organization_role_name = var.organization_role_name
organizational_units = var.organizational_units
role_arn = var.create_iam_role ? aws_iam_role.this[0].arn : var.iam_role_arn
stack_set_name = coalesce(var.stack_set_name, var.name)
dynamic "vpc_configuration" {
for_each = length(var.vpc_configuration) > 0 ? [var.vpc_configuration] : []
content {
security_group_ids = var.create_security_group ? flatten(concat([aws_security_group.this[0].id], try(vpc_configuration.value.security_group_ids, []))) : vpc_configuration.value.security_group_ids
subnet_ids = vpc_configuration.value.subnet_ids
}
}
tags = var.tags
}
################################################################################
# Security Group
################################################################################
locals {
create_security_group = length(var.vpc_configuration) > 0 && var.create_security_group
security_group_name = try(coalesce(var.security_group_name, var.name), "")
}
data "aws_subnet" "this" {
count = local.create_security_group ? 1 : 0
id = element(var.vpc_configuration.subnet_ids, 0)
}
resource "aws_security_group" "this" {
count = local.create_security_group ? 1 : 0
name = var.security_group_use_name_prefix ? null : local.security_group_name
name_prefix = var.security_group_use_name_prefix ? "${local.security_group_name}-" : null
description = var.security_group_description
vpc_id = data.aws_subnet.this[0].vpc_id
tags = merge(var.tags, var.security_group_tags)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "this" {
for_each = { for k, v in var.security_group_rules : k => v if local.create_security_group }
# Required
security_group_id = aws_security_group.this[0].id
protocol = each.value.protocol
from_port = each.value.from_port
to_port = each.value.to_port
type = try(each.value.type, "egress")
# Optional
description = lookup(each.value, "description", null)
cidr_blocks = lookup(each.value, "cidr_blocks", null)
ipv6_cidr_blocks = lookup(each.value, "ipv6_cidr_blocks", null)
prefix_list_ids = lookup(each.value, "prefix_list_ids", null)
self = lookup(each.value, "self", null)
source_security_group_id = lookup(each.value, "source_security_group_id", null)
}
################################################################################
# Workspace API Key
################################################################################
resource "aws_grafana_workspace_api_key" "this" {
for_each = { for k, v in var.workspace_api_keys : k => v if var.create }
key_name = try(each.value.key_name, each.key)
key_role = each.value.key_role
seconds_to_live = each.value.seconds_to_live
workspace_id = local.workspace_id
}
################################################################################
# Workspace IAM Role
################################################################################
locals {
create_role = var.create && var.create_iam_role
iam_role_name = coalesce(var.iam_role_name, var.name)
create_account_policy = local.create_role && var.account_access_type == "CURRENT_ACCOUNT"
create_custom_policy = length(setintersection(var.data_sources, ["CLOUDWATCH", "AMAZON_OPENSEARCH_SERVICE", "PROMETHEUS", "SNS"])) > 0
}
data "aws_iam_policy_document" "assume" {
count = local.create_role ? 1 : 0
statement {
sid = "GrafanaAssume"
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["grafana.${data.aws_partition.current.dns_suffix}"]
}
}
}
resource "aws_iam_role" "this" {
count = local.create_role ? 1 : 0
name = var.use_iam_role_name_prefix ? null : local.iam_role_name
name_prefix = var.use_iam_role_name_prefix ? "${local.iam_role_name}-" : null
description = var.iam_role_description
path = var.iam_role_path
assume_role_policy = data.aws_iam_policy_document.assume[0].json
force_detach_policies = var.iam_role_force_detach_policies
max_session_duration = var.iam_role_max_session_duration
permissions_boundary = var.iam_role_permissions_boundary
tags = merge(var.tags, var.iam_role_tags)
}
resource "aws_iam_role_policy_attachment" "additional" {
for_each = { for k, v in var.iam_role_policy_arns : k => v if local.create_role }
role = aws_iam_role.this[0].name
policy_arn = each.value
}
# https://docs.aws.amazon.com/grafana/latest/userguide/AMG-manage-permissions.html
data "aws_iam_policy_document" "this" {
count = local.create_account_policy ? 1 : 0
# CloudWatch
dynamic "statement" {
for_each = contains(var.data_sources, "CLOUDWATCH") ? [1] : []
content {
sid = "AllowReadingMetricsFromCloudWatch"
actions = [
"cloudwatch:DescribeAlarmsForMetric",
"cloudwatch:DescribeAlarmHistory",
"cloudwatch:DescribeAlarms",
"cloudwatch:ListMetrics",
"cloudwatch:GetMetricStatistics",
"cloudwatch:GetMetricData",
]
resources = ["*"]
}
}
dynamic "statement" {
for_each = contains(var.data_sources, "CLOUDWATCH") ? [1] : []
content {
sid = "AllowReadingLogsFromCloudWatch"
actions = [
"logs:DescribeLogGroups",
"logs:GetLogGroupFields",
"logs:StartQuery",
"logs:StopQuery",
"logs:GetQueryResults",
"logs:GetLogEvents",
]
resources = ["*"]
}
}
dynamic "statement" {
for_each = contains(var.data_sources, "CLOUDWATCH") ? [1] : []
content {
sid = "AllowReadingTagsInstancesRegionsFromEC2"
actions = [
"ec2:DescribeTags",
"ec2:DescribeInstances",
"ec2:DescribeRegions",
]
resources = ["*"]
}
}
dynamic "statement" {
for_each = contains(var.data_sources, "CLOUDWATCH") ? [1] : []
content {
sid = "AllowReadingResourcesForTags"
actions = [
"tag:GetResources",
]
resources = ["*"]
}
}
# OpenSearch
dynamic "statement" {
for_each = contains(var.data_sources, "AMAZON_OPENSEARCH_SERVICE") ? [1] : []
content {
actions = [
"es:ESHttpGet",
"es:DescribeElasticsearchDomains",
"es:ListDomainNames",
]
resources = ["*"]
}
}
dynamic "statement" {
for_each = contains(var.data_sources, "AMAZON_OPENSEARCH_SERVICE") ? [1] : []
content {
actions = [
"es:ESHttpPost",
]
resources = [
"arn:${data.aws_partition.current.partition}:es:*:*:domain/*/_msearch*",
"arn:${data.aws_partition.current.partition}:es:*:*:domain/*/_opendistro/_ppl",
]
}
}
# Prometheus
dynamic "statement" {
for_each = contains(var.data_sources, "PROMETHEUS") ? [1] : []
content {
actions = [
"aps:ListWorkspaces",
"aps:DescribeWorkspace",
"aps:QueryMetrics",
"aps:GetLabels",
"aps:GetSeries",
"aps:GetMetricMetadata",
]
resources = ["*"]
}
}
# SNS Notification
dynamic "statement" {
for_each = contains(var.notification_destinations, "SNS") ? [1] : []
content {
actions = [
"sns:Publish",
]
resources = ["arn:${data.aws_partition.current.partition}:sns:*:${data.aws_caller_identity.current.account_id}:grafana*"]
}
}
}
resource "aws_iam_policy" "this" {
count = local.create_account_policy && local.create_custom_policy ? 1 : 0
name_prefix = "${local.iam_role_name}-"
description = var.iam_role_description
path = var.iam_role_path
policy = data.aws_iam_policy_document.this[0].json
tags = var.tags
}
locals {
policies_to_attach = {
this = {
arn = try(aws_iam_policy.this[0].arn, null)
attach = local.create_custom_policy
}
sitewise = {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSIoTSiteWiseReadOnlyAccess"
attach = contains(var.data_sources, "SITEWISE")
}
redshift = {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonGrafanaRedshiftAccess"
attach = contains(var.data_sources, "REDSHIFT")
}
athena = {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonGrafanaAthenaAccess"
attach = contains(var.data_sources, "ATHENA")
}
timestream = {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AmazonTimestreamReadOnlyAccess"
attach = contains(var.data_sources, "TIMESTREAM")
}
xray = {
arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/AWSXrayReadOnlyAccess"
attach = contains(var.data_sources, "XRAY")
}
}
}
resource "aws_iam_role_policy_attachment" "this" {
for_each = { for k, v in local.policies_to_attach : k => v if local.create_account_policy && v.attach }
role = aws_iam_role.this[0].name
policy_arn = each.value.arn
}
################################################################################
# Workspace SAML Configuration
################################################################################
resource "aws_grafana_workspace_saml_configuration" "this" {
count = var.create && var.create_saml_configuration && contains(var.authentication_providers, "SAML") ? 1 : 0
editor_role_values = var.saml_editor_role_values
workspace_id = local.workspace_id
idp_metadata_url = var.saml_idp_metadata_url
idp_metadata_xml = var.saml_idp_metadata_xml
admin_role_values = var.saml_admin_role_values
allowed_organizations = var.saml_allowed_organizations
email_assertion = var.saml_email_assertion
groups_assertion = var.saml_groups_assertion
login_assertion = var.saml_login_assertion
login_validity_duration = var.saml_login_validity_duration
name_assertion = var.saml_name_assertion
org_assertion = var.saml_org_assertion
role_assertion = var.saml_role_assertion
}
################################################################################
# License Association
################################################################################
resource "aws_grafana_license_association" "this" {
count = var.create && var.associate_license ? 1 : 0
license_type = var.license_type
workspace_id = local.workspace_id
}
################################################################################
# Role Association
################################################################################
resource "aws_grafana_role_association" "this" {
for_each = { for k, v in var.role_associations : k => v if var.create }
role = try(each.value.role, each.key)
group_ids = try(each.value.group_ids, null)
user_ids = try(each.value.user_ids, null)
workspace_id = local.workspace_id
}