diff --git a/ecs/README.md b/ecs/README.md index b4afe1d2..bf11f0b6 100644 --- a/ecs/README.md +++ b/ecs/README.md @@ -161,6 +161,10 @@ Based on [AWS reference architecture](https://github.com/aws-samples/ecs-refarch The canonical hosted zone ID of the Application Load Balancer (to be used in a Route 53 Alias record) +* `metrics` + + ECS cluster Cloudwatch metrics, see [metrics.tf](./metrics.tf) for details + * `name` Cluster name @@ -216,3 +220,7 @@ Based on [AWS reference architecture](https://github.com/aws-samples/ecs-refarch * `web_service_role_name` ECS web service task role name + +* `widgets` + + ECS cluster Cloudwatch dashboard widgets, see [widgets.tf](./widgets.tf) for details diff --git a/ecs/example/main.tf b/ecs/example/main.tf index 18f300be..308d78c6 100644 --- a/ecs/example/main.tf +++ b/ecs/example/main.tf @@ -103,6 +103,11 @@ module "dashboard" { name = "terraform-ecs-example" widgets = [ + module.cluster.widgets.cpu_utilization, + module.cluster.widgets.memory_utilization, + module.cluster.widgets.instances, + module.cluster.widgets.services, + module.cluster.widgets.tasks, module.cluster.lb_widgets.responses, module.cluster.lb_widgets.response_percentages, module.cluster.lb_widgets.target_response_time, diff --git a/ecs/metrics.tf b/ecs/metrics.tf new file mode 100644 index 00000000..a87ef820 --- /dev/null +++ b/ecs/metrics.tf @@ -0,0 +1,80 @@ +locals { + metrics = { + cpu_utilization = module.metrics_cpu.out_map.utilization + cpu_reservation = module.metrics_cpu.out_map.reservation + memory_utilization = module.metrics_memory.out_map.utilization + memory_reservation = module.metrics_memory.out_map.reservation + + instances = module.metrics_count.out_map.instance + services = module.metrics_count.out_map.service + tasks = module.metrics_count.out_map.task + } +} + +module "cloudwatch_consts" { + source = "./../cloudwatch/consts" +} + +locals { + colors = module.cloudwatch_consts.colors +} + +locals { + cluster_dimensions = { + ClusterName = var.create ? aws_ecs_cluster.cluster[0].name : "" + } + + metrics_utilization = { + reservation = { name = "Reservation", color = local.colors.grey } + utilization = { name = "Utilization", color = local.colors.orange } + } +} + +module "metrics_cpu" { + source = "./../cloudwatch/metric/many" + + vars_map = { for k, v in local.metrics_utilization : k => { + namespace = "AWS/ECS" + dimensions = local.cluster_dimensions + name = "CPU${v.name}" + label = "CPU ${lower(v.name)}" + color = v.color + stat = "Average" + period = 60 + } } +} + +module "metrics_memory" { + source = "./../cloudwatch/metric/many" + + vars_map = { for k, v in local.metrics_utilization : k => { + namespace = "AWS/ECS" + dimensions = local.cluster_dimensions + name = "Memory${v.name}" + label = "Memory ${lower(v.name)}" + color = v.color + stat = "Average" + period = 60 + } } +} + +locals { + metrics_count = { + instance = { name = "ContainerInstance", label = "Instances" } + service = { name = "Service", label = "Services" } + task = { name = "Task", label = "Tasks" } + } +} + +module "metrics_count" { + source = "./../cloudwatch/metric/many" + + vars_map = { for k, v in local.metrics_count : k => { + namespace = "ECS/ContainerInsights" + dimensions = local.cluster_dimensions + name = "${v.name}Count" + label = v.label + stat = "Average" + period = 60 + } } +} diff --git a/ecs/outputs.tf b/ecs/outputs.tf index d9b13b66..0502912e 100644 --- a/ecs/outputs.tf +++ b/ecs/outputs.tf @@ -8,6 +8,16 @@ output "arn" { description = "Cluster ARN" } +output "metrics" { + value = local.metrics + description = "ECS cluster Cloudwatch metrics, see [metrics.tf](./metrics.tf) for details" +} + +output "widgets" { + value = local.widgets + description = "ECS cluster Cloudwatch dashboard widgets, see [widgets.tf](./widgets.tf) for details" +} + # network outputs output "vpc_id" { diff --git a/ecs/widgets.tf b/ecs/widgets.tf new file mode 100644 index 00000000..83278a93 --- /dev/null +++ b/ecs/widgets.tf @@ -0,0 +1,56 @@ +locals { + widgets = { + cpu_utilization = module.widget_cpu_utilization + memory_utilization = module.widget_memory_utilization + + instances = module.widget_instances + services = module.widget_services + tasks = module.widget_tasks + } +} + +module "widget_cpu_utilization" { + source = "./../cloudwatch/metric_widget" + + title = "ECS CPU utilization" + left_metrics = [ + local.metrics.cpu_reservation, + local.metrics.cpu_utilization, + ] + left_range = [0, 100] +} + +module "widget_memory_utilization" { + source = "./../cloudwatch/metric_widget" + + title = "ECS memory utilization" + left_metrics = [ + local.metrics.memory_reservation, + local.metrics.memory_utilization, + ] + left_range = [0, 100] +} + +module "widget_instances" { + source = "./../cloudwatch/metric_widget" + + title = "ECS instances" + left_metrics = [local.metrics.instances] + left_range = [0, null] +} + +module "widget_services" { + source = "./../cloudwatch/metric_widget" + + title = "ECS services" + left_metrics = [local.metrics.services] + left_range = [0, null] +} + +module "widget_tasks" { + source = "./../cloudwatch/metric_widget" + + title = "ECS tasks" + left_metrics = [local.metrics.tasks] + left_range = [0, null] +}