-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudwatch.tf
68 lines (65 loc) · 2.67 KB
/
cloudwatch.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
data "aws_arn" "eb-eu-alb-arns-decode" {
provider = aws.eu-west-1
for_each = local.is_production ? module.eb-eu-west-1.eb_load_balancers : {}
arn = one(each.value)
}
data "aws_arn" "eb-us-alb-arns-decode" {
provider = aws.us-east-1
for_each = local.is_production && length(local.multi_region_environments) > 0 ? one(module.eb-us-east-1).eb_load_balancers : {}
arn = one(each.value)
}
locals {
widgets = {
cw_max_panel_width = 24
height = 6
max_regions_per_x = length(local.multi_region_environments) > 0 ? 2 : 1 // edit the true one for having more regions on the same line
}
}
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/CloudWatch-Dashboard-Body-Structure.html
# todo: https://emilenijssen.nl/8-aws-elastic-beanstalk-cloudwatch-ram-and-disk-monitoring/
resource "aws_cloudwatch_dashboard" "main" { // use better data structure to be future-proof
provider = aws.eu-west-1
for_each = local.is_production ? local.environments : {}
dashboard_name = "${local.tags["Project"]}-${each.key}-dashboard"
dashboard_body = jsonencode(
{
"widgets" : flatten(
[
for filename in setunion(
fileset("${path.module}", "widgets/*.alb.tftpl"),
fileset("${path.module}", "widgets/*.redis.tftpl"),
fileset("${path.module}", "widgets/*.ec2.tftpl"),
) :
jsondecode(templatefile(filename, merge({
albs = merge({
eu-west-1 = trimprefix(data.aws_arn.eb-eu-alb-arns-decode[each.key].resource, "loadbalancer/"),
},
can(data.aws_arn.eb-us-alb-arns-decode[each.key]) ? {
us-east-1 = trimprefix(data.aws_arn.eb-us-alb-arns-decode[each.key].resource, "loadbalancer/")
} : {}),
},
{
redis = (local.is_redis && lookup(each.value, "redis", false)) ? merge({
eu-west-1 = can(one(module.redis-eu-west-1)) ? one(module.redis-eu-west-1).member_clusters : [],
},
can(one(module.redis-us-east-1)) ? {
us-east-1 = one(module.redis-us-east-1).member_clusters,
} : {}) : {},
},
{
autoscaling = merge({
eu-west-1 = module.eb-eu-west-1.autoscaling[each.key]
},
can(one(module.eb-us-east-1).autoscaling[each.key]) ? {
us-east-1 = one(module.eb-us-east-1).autoscaling[each.key]
} : {})
},
{
env_name = "${local.tags["Project"]}-${each.key}", // Beanstalk Environment Names
},
local.widgets)))
]
)
}
)
}