/
dynamodb_table_dashboard.sp
630 lines (562 loc) · 13.4 KB
/
dynamodb_table_dashboard.sp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
dashboard "dynamodb_table_dashboard" {
title = "AWS DynamoDB Table Dashboard"
documentation = file("./dashboards/dynamodb/docs/dynamodb_table_dashboard.md")
tags = merge(local.dynamodb_common_tags, {
type = "Dashboard"
})
# Top cards
container {
# Analysis
card {
query = query.dynamodb_table_count
width = 2
}
# Assessments
card {
query = query.dynamodb_table_unused_count
width = 2
}
card {
query = query.dynamodb_table_autoscaling_disabled_count
width = 2
}
card {
query = query.dynamodb_table_continuous_backups_count
width = 2
}
# Costs
card {
type = "info"
icon = "currency-dollar"
width = 2
query = query.dynamodb_table_cost_mtd
}
}
container {
title = "Assessments"
width = 6
chart {
title = "Unused Table Status"
type = "donut"
width = 4
query = query.dynamodb_table_unused_status
series "table_count" {
point "in-use" {
color = "ok"
}
point "unused" {
color = "alert"
}
}
}
chart {
title = "Autoscaling Status"
type = "donut"
width = 4
query = query.dynamodb_table_autoscaling_status
series "table_count" {
point "enabled" {
color = "ok"
}
point "disabled" {
color = "alert"
}
}
}
chart {
title = "Continuous Backups"
type = "donut"
width = 4
query = query.dynamodb_table_continuous_backups_status
series "count" {
point "enabled" {
color = "ok"
}
point "disabled" {
color = "alert"
}
}
}
chart {
title = "Backup Plan Protection"
type = "donut"
width = 4
query = query.dynamodb_table_backup_plan_protection_status
series "table_count" {
point "protected" {
color = "ok"
}
point "unprotected" {
color = "alert"
}
}
}
}
container {
title = "Costs"
width = 6
table {
width = 6
title = "Forecast"
query = query.dynamodb_monthly_forecast_table
}
chart {
width = 6
type = "column"
title = "Monthly Cost - 12 Months"
query = query.dynamodb_table_cost_per_month
}
}
container {
title = "Analysis"
chart {
title = "Tables by Account"
type = "column"
width = 4
query = query.dynamodb_table_by_account
}
chart {
title = "Tables by Region"
type = "column"
width = 4
query = query.dynamodb_table_by_region
}
chart {
title = "Tables by Age"
query = query.dynamodb_table_by_creation_month
type = "column"
width = 4
}
}
container {
chart {
title = "Table Item Count by Account"
type = "column"
width = 4
query = query.dynamodb_table_item_count_by_account
series "GB" {
color = "tan"
}
}
chart {
title = "Table Item Count by Region"
type = "column"
width = 4
query = query.dynamodb_table_item_count_by_region
series "GB" {
color = "tan"
}
}
chart {
title = "Table Item Count by Age"
type = "column"
width = 4
query = query.dynamodb_table_item_count_by_creation_month
series "GB" {
color = "tan"
}
}
}
container {
title = "Performance & Utilization"
chart {
title = "Average Read Throughput - Last 7 days"
type = "line"
width = 6
query = query.dynamodb_table_average_read_throughput
}
chart {
title = "Average Write Throughput - Last 7 days"
type = "line"
width = 6
query = query.dynamodb_table_average_write_throughput
}
}
}
# Card Queries
query "dynamodb_table_count" {
sql = <<-EOQ
select count(*) as "Tables" from aws_dynamodb_table;
EOQ
}
query "dynamodb_table_unused_count" {
sql = <<-EOQ
select
count(*) as value,
'Unused' as label,
case count(*) when 0 then 'ok' else 'alert' end as type
from
aws_dynamodb_table
where
item_count = 0;
EOQ
}
query "dynamodb_table_autoscaling_disabled_count" {
sql = <<-EOQ
with table_with_autoscaling as (
select
t.resource_id as resource_id,
count(t.resource_id) as count
from
aws_appautoscaling_target as t where service_namespace = 'dynamodb'
group by t.resource_id
)
select
count(*) as value,
'Autoscaling Disabled' as label,
case count(*) when 0 then 'ok' else 'alert' end as type
from
aws_dynamodb_table as d
left join table_with_autoscaling as t on concat('table/', d.name) = t.resource_id
where
t.resource_id is null
or t.count < 2;
EOQ
}
query "dynamodb_table_continuous_backups_count" {
sql = <<-EOQ
select
count(*) as value,
'Continuous Backups Disabled' as label,
case count(*) when 0 then 'ok' else 'alert' end as type
from
aws_dynamodb_table
where
not (continuous_backups_status = 'ENABLED');
EOQ
}
query "dynamodb_table_cost_mtd" {
sql = <<-EOQ
select
'Cost - MTD' as label,
sum(unblended_cost_amount)::numeric::money as value
from
aws_cost_by_service_usage_type_monthly as c
where
service = 'Amazon DynamoDB'
and period_end > date_trunc('month', CURRENT_DATE::timestamp);
EOQ
}
# Assessment Queries
query "dynamodb_table_unused_status" {
sql = <<-EOQ
with table_status as (
select
name,
case
when item_count > 0 then 'in-use'
else 'unused'
end as table_usage_status
from
aws_dynamodb_table
)
select
table_usage_status,
count(*) as table_count
from
table_status
group by table_usage_status;
EOQ
}
query "dynamodb_table_autoscaling_status" {
sql = <<-EOQ
with table_with_autoscaling as (
select
t.resource_id as resource_id,
count(t.resource_id) as count
from
aws_appautoscaling_target as t where service_namespace = 'dynamodb'
group by t.resource_id
),
table_autoscaling_status as (
select
d.name as table_name,
case
when t.resource_id is null or t.count < 2 then 'disabled'
else 'enabled'
end as autoscaling_status
from
aws_dynamodb_table as d
left join table_with_autoscaling as t on concat('table/', d.name) = t.resource_id
)
select
autoscaling_status,
count(*) as table_count
from
table_autoscaling_status
group by autoscaling_status;
EOQ
}
query "dynamodb_table_continuous_backups_status" {
sql = <<-EOQ
select
case
when continuous_backups_status = 'ENABLED' then 'enabled'
else 'disabled'
end as status,
count(*)
from
aws_dynamodb_table
group by status;
EOQ
}
query "dynamodb_table_backup_plan_protection_status" {
sql = <<-EOQ
with backup_protected_table as (
select
resource_arn as arn
from
aws_backup_protected_resource as b
where
resource_type = 'DynamoDB'
),
table_backup_plan_protection_status as (
select
t.name as table_name,
case
when b.arn is not null then 'protected'
else 'unprotected'
end as backup_plan_protection_status
from
aws_dynamodb_table as t
left join backup_protected_table as b on t.arn = b.arn
)
select
backup_plan_protection_status,
count(*) as table_count
from
table_backup_plan_protection_status
group by backup_plan_protection_status;
EOQ
}
# Cost Queries
query "dynamodb_monthly_forecast_table" {
sql = <<-EOQ
with monthly_costs as (
select
period_start,
period_end,
case
when date_trunc('month', period_start) = date_trunc('month', CURRENT_DATE::timestamp) then 'Month to Date'
when date_trunc('month', period_start) = date_trunc('month', CURRENT_DATE::timestamp - interval '1 month') then 'Previous Month'
else to_char (period_start, 'Month')
end as period_label,
period_end::date - period_start::date as days,
sum(unblended_cost_amount)::numeric::money as unblended_cost_amount,
(sum(unblended_cost_amount) / (period_end::date - period_start::date ) )::numeric::money as average_daily_cost,
date_part('days', date_trunc ('month', period_start) + '1 MONTH'::interval - '1 DAY'::interval ) as days_in_month,
sum(unblended_cost_amount) / (period_end::date - period_start::date ) * date_part('days', date_trunc ('month', period_start) + '1 MONTH'::interval - '1 DAY'::interval )::numeric::money as forecast_amount
from
aws_cost_by_service_usage_type_monthly as c
where
service = 'Amazon DynamoDB'
and date_trunc('month', period_start) >= date_trunc('month', CURRENT_DATE::timestamp - interval '1 month')
group by
period_start,
period_end
)
select
period_label as "Period",
unblended_cost_amount as "Cost",
average_daily_cost as "Daily Avg Cost"
from
monthly_costs
union all
select
'This Month (Forecast)' as "Period",
(select forecast_amount from monthly_costs where period_label = 'Month to Date') as "Cost",
(select average_daily_cost from monthly_costs where period_label = 'Month to Date') as "Daily Avg Cost";
EOQ
}
query "dynamodb_table_cost_per_month" {
sql = <<-EOQ
select
to_char(period_start, 'Mon-YY') as "Month",
sum(unblended_cost_amount) as "Unblended Cost"
from
aws_cost_by_service_usage_type_monthly
where
service = 'Amazon DynamoDB'
group by
period_start
order by
period_start;
EOQ
}
# Analysis Queries
query "dynamodb_table_by_account" {
sql = <<-EOQ
select
a.title as "Account",
count(t.*) as "tables"
from
aws_dynamodb_table as t,
aws_account as a
where
a.account_id = t.account_id
group by
a.title
order by
a.title;
EOQ
}
query "dynamodb_table_by_region" {
sql = <<-EOQ
select
region as "Region",
count(*) as "tables"
from
aws_dynamodb_table
group by
region
order by
region;
EOQ
}
query "dynamodb_table_by_creation_month" {
sql = <<-EOQ
with volumes as (
select
title,
creation_date_time,
to_char(creation_date_time,
'YYYY-MM') as creation_month
from
aws_dynamodb_table
),
months as (
select
to_char(d,
'YYYY-MM') as month
from
generate_series(date_trunc('month',
(
select
min(creation_date_time)
from
volumes
)),
date_trunc('month',
current_date),
interval '1 month') as d
),
tables_by_month as (
select
creation_month,
count(*)
from
volumes
group by
creation_month
)
select
months.month,
tables_by_month.count
from
months
left join tables_by_month on months.month = tables_by_month.creation_month
order by
months.month;
EOQ
}
query "dynamodb_table_item_count_by_account" {
sql = <<-EOQ
select
a.title as "Account",
sum(t.item_count) as "Count"
from
aws_dynamodb_table as t,
aws_account as a
where
a.account_id = t.account_id
group by
a.title
order by
a.title;
EOQ
}
query "dynamodb_table_item_count_by_region" {
sql = <<-EOQ
select
region as "Region",
sum(item_count) as "Count"
from
aws_dynamodb_table
group by
region
order by
region;
EOQ
}
query "dynamodb_table_item_count_by_creation_month" {
sql = <<-EOQ
with volumes as (
select
title,
item_count,
creation_date_time,
to_char(creation_date_time,
'YYYY-MM') as creation_month
from
aws_dynamodb_table
),
months as (
select
to_char(d,
'YYYY-MM') as month
from
generate_series(date_trunc('month',
(
select
min(creation_date_time)
from
volumes
)),
date_trunc('month',
current_date),
interval '1 month') as d
),
tables_by_month as (
select
creation_month,
sum(item_count) as item_count
from
volumes
group by
creation_month
)
select
months.month,
tables_by_month.item_count as "Count"
from
months
left join tables_by_month on months.month = tables_by_month.creation_month
order by
months.month;
EOQ
}
# Performance Queries
query "dynamodb_table_average_read_throughput" {
sql = <<-EOQ
select
timestamp,
average
from
aws_dynamodb_metric_account_provisioned_read_capacity_util
where
timestamp >= current_date - interval '7 day'
order by timestamp;
EOQ
}
query "dynamodb_table_average_write_throughput" {
sql = <<-EOQ
select
timestamp,
average
from
aws_dynamodb_metric_account_provisioned_write_capacity_util
where
timestamp >= current_date - interval '7 day'
order by timestamp;
EOQ
}