From 443279b05bfabd981e508d95cd4cafddb83ac970 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Wed, 29 Apr 2020 13:38:15 +0200 Subject: [PATCH 1/7] feat(cloudwatch/metric): added many module --- cloudwatch/metric/many/README.md | 33 ++++++++++++++++++++ cloudwatch/metric/many/main.tf | 48 +++++++++++++++++++++++++++++ cloudwatch/metric/many/outputs.tf | 9 ++++++ cloudwatch/metric/many/variables.tf | 9 ++++++ cloudwatch/metric/many/versions.tf | 3 ++ 5 files changed, 102 insertions(+) create mode 100644 cloudwatch/metric/many/README.md create mode 100644 cloudwatch/metric/many/main.tf create mode 100644 cloudwatch/metric/many/outputs.tf create mode 100644 cloudwatch/metric/many/variables.tf create mode 100644 cloudwatch/metric/many/versions.tf diff --git a/cloudwatch/metric/many/README.md b/cloudwatch/metric/many/README.md new file mode 100644 index 00000000..2b97eff1 --- /dev/null +++ b/cloudwatch/metric/many/README.md @@ -0,0 +1,33 @@ +# cloudwatch/metric/many + +Same as [cloudwatch/metric](./..) but allows for creating many metrics using a single module + + + +## Versions + +| Provider | Requirements | +|-|-| +| terraform | `>= 0.12` | + +## Inputs + +* `vars` (`any`, required) + + List of [cloudwatch/metric](./..) variables + +* `vars_map` (`any`, required) + + Map of [cloudwatch/metric](./..) variables + + + +## Outputs + +* `out` + + List of [cloudwatch/metric](./..) outputs + +* `out_map` + + Map of [cloudwatch/metric](./..) outputs diff --git a/cloudwatch/metric/many/main.tf b/cloudwatch/metric/many/main.tf new file mode 100644 index 00000000..397306bb --- /dev/null +++ b/cloudwatch/metric/many/main.tf @@ -0,0 +1,48 @@ +module "default" { + source = "./.." + + namespace = "" + name = "" +} + +locals { + out = [for v in var.vars : { + id = "m_${v.name}_${md5(jsonencode([ + v.namespace, + v.name, + try(v.dimensions, module.default.dimensions), + try(v.period, module.default.period), + try(v.stat, module.default.stat), + try(v.label, module.default.label), + try(v.color, module.default.color), + ]))}" + + namespace = v.namespace + name = v.name + dimensions = try(v.dimensions, module.default.dimensions) + period = try(v.period, module.default.period) + stat = try(v.stat, module.default.stat) + label = try(v.label, module.default.label) + color = try(v.color, module.default.color) + }] + + out_map = { for k, v in var.vars_map : k => { + id = "m_${v.name}_${md5(jsonencode([ + v.namespace, + v.name, + try(v.dimensions, module.default.dimensions), + try(v.period, module.default.period), + try(v.stat, module.default.stat), + try(v.label, module.default.label), + try(v.color, module.default.color), + ]))}" + + namespace = v.namespace + name = v.name + dimensions = try(v.dimensions, module.default.dimensions) + period = try(v.period, module.default.period) + stat = try(v.stat, module.default.stat) + label = try(v.label, module.default.label) + color = try(v.color, module.default.color) + } } +} diff --git a/cloudwatch/metric/many/outputs.tf b/cloudwatch/metric/many/outputs.tf new file mode 100644 index 00000000..99a5d4db --- /dev/null +++ b/cloudwatch/metric/many/outputs.tf @@ -0,0 +1,9 @@ +output "out" { + description = "List of [cloudwatch/metric](./..) outputs" + value = local.out +} + +output "out_map" { + description = "Map of [cloudwatch/metric](./..) outputs" + value = local.out_map +} diff --git a/cloudwatch/metric/many/variables.tf b/cloudwatch/metric/many/variables.tf new file mode 100644 index 00000000..e125d44e --- /dev/null +++ b/cloudwatch/metric/many/variables.tf @@ -0,0 +1,9 @@ +variable "vars" { + description = "List of [cloudwatch/metric](./..) variables" + type = any +} + +variable "vars_map" { + description = "Map of [cloudwatch/metric](./..) variables" + type = any +} diff --git a/cloudwatch/metric/many/versions.tf b/cloudwatch/metric/many/versions.tf new file mode 100644 index 00000000..d9b6f790 --- /dev/null +++ b/cloudwatch/metric/many/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.12" +} From 742eec15088f4f403356dd28c88bd2fd38fff847 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Wed, 29 Apr 2020 13:38:31 +0200 Subject: [PATCH 2/7] feat(cloudwatch/metric_expression): added many module --- cloudwatch/metric_expression/many/README.md | 33 +++++++++++++++++++ cloudwatch/metric_expression/many/main.tf | 31 +++++++++++++++++ cloudwatch/metric_expression/many/outputs.tf | 9 +++++ .../metric_expression/many/variables.tf | 9 +++++ cloudwatch/metric_expression/many/versions.tf | 3 ++ 5 files changed, 85 insertions(+) create mode 100644 cloudwatch/metric_expression/many/README.md create mode 100644 cloudwatch/metric_expression/many/main.tf create mode 100644 cloudwatch/metric_expression/many/outputs.tf create mode 100644 cloudwatch/metric_expression/many/variables.tf create mode 100644 cloudwatch/metric_expression/many/versions.tf diff --git a/cloudwatch/metric_expression/many/README.md b/cloudwatch/metric_expression/many/README.md new file mode 100644 index 00000000..06f12e2d --- /dev/null +++ b/cloudwatch/metric_expression/many/README.md @@ -0,0 +1,33 @@ +# cloudwatch/metric_expression/many + +Same as [cloudwatch/metric_expression](./..) but allows for creating many metrics using a single module + + + +## Versions + +| Provider | Requirements | +|-|-| +| terraform | `>= 0.12` | + +## Inputs + +* `vars` (`any`, required) + + List of [cloudwatch/metric_expression](./..) variables + +* `vars_map` (`any`, required) + + Map of [cloudwatch/metric_expression](./..) variables + + + +## Outputs + +* `out` + + List of [cloudwatch/metric_expression](./..) outputs + +* `out_map` + + Map of [cloudwatch/metric_expression](./..) outputs diff --git a/cloudwatch/metric_expression/many/main.tf b/cloudwatch/metric_expression/many/main.tf new file mode 100644 index 00000000..92a7cf4c --- /dev/null +++ b/cloudwatch/metric_expression/many/main.tf @@ -0,0 +1,31 @@ +module "default" { + source = "./.." + + expression = "" +} + +locals { + out = [for v in var.vars : { + id = "e_${md5(jsonencode([ + v.expression, + try(v.label, module.default.label), + try(v.color, module.default.color), + ]))}" + + expression = v.expression + label = try(v.label, module.default.label) + color = try(v.color, module.default.color) + }] + + out_map = { for k, v in var.vars_map : k => { + id = "e_${md5(jsonencode([ + v.expression, + try(v.label, module.default.label), + try(v.color, module.default.color), + ]))}" + + expression = v.expression + label = try(v.label, module.default.label) + color = try(v.color, module.default.color) + } } +} diff --git a/cloudwatch/metric_expression/many/outputs.tf b/cloudwatch/metric_expression/many/outputs.tf new file mode 100644 index 00000000..1bc3a246 --- /dev/null +++ b/cloudwatch/metric_expression/many/outputs.tf @@ -0,0 +1,9 @@ +output "out" { + description = "List of [cloudwatch/metric_expression](./..) outputs" + value = local.out +} + +output "out_map" { + description = "Map of [cloudwatch/metric_expression](./..) outputs" + value = local.out_map +} diff --git a/cloudwatch/metric_expression/many/variables.tf b/cloudwatch/metric_expression/many/variables.tf new file mode 100644 index 00000000..cbf49d03 --- /dev/null +++ b/cloudwatch/metric_expression/many/variables.tf @@ -0,0 +1,9 @@ +variable "vars" { + description = "List of [cloudwatch/metric_expression](./..) variables" + type = any +} + +variable "vars_map" { + description = "Map of [cloudwatch/metric_expression](./..) variables" + type = any +} diff --git a/cloudwatch/metric_expression/many/versions.tf b/cloudwatch/metric_expression/many/versions.tf new file mode 100644 index 00000000..d9b6f790 --- /dev/null +++ b/cloudwatch/metric_expression/many/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.12" +} From 5c5cbaed3b3a36635a7abeac21a57234833b993e Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Wed, 29 Apr 2020 14:04:03 +0200 Subject: [PATCH 3/7] fix(cloudwatch/metric/many): variables should be optional --- cloudwatch/metric/many/variables.tf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cloudwatch/metric/many/variables.tf b/cloudwatch/metric/many/variables.tf index e125d44e..290d8021 100644 --- a/cloudwatch/metric/many/variables.tf +++ b/cloudwatch/metric/many/variables.tf @@ -1,9 +1,11 @@ variable "vars" { description = "List of [cloudwatch/metric](./..) variables" type = any + default = [] } variable "vars_map" { description = "Map of [cloudwatch/metric](./..) variables" type = any + default = {} } From f491f538c93f4c6545b2f18d67ffcf5b3cc5b706 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Wed, 29 Apr 2020 14:04:10 +0200 Subject: [PATCH 4/7] fix(cloudwatch/metric_expression/many): variables should be optional --- cloudwatch/metric_expression/many/variables.tf | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cloudwatch/metric_expression/many/variables.tf b/cloudwatch/metric_expression/many/variables.tf index cbf49d03..f0da6670 100644 --- a/cloudwatch/metric_expression/many/variables.tf +++ b/cloudwatch/metric_expression/many/variables.tf @@ -1,9 +1,11 @@ variable "vars" { description = "List of [cloudwatch/metric_expression](./..) variables" type = any + default = [] } variable "vars_map" { description = "Map of [cloudwatch/metric_expression](./..) variables" type = any + default = {} } From 57bc94a2b4d5185a130fa01aef8e502a7a2649d1 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Wed, 29 Apr 2020 14:04:45 +0200 Subject: [PATCH 5/7] docs(cloudwatch/metric/many): added an example --- cloudwatch/metric_widget/example/main.tf | 61 ++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/cloudwatch/metric_widget/example/main.tf b/cloudwatch/metric_widget/example/main.tf index f77f68dc..487b1471 100644 --- a/cloudwatch/metric_widget/example/main.tf +++ b/cloudwatch/metric_widget/example/main.tf @@ -15,6 +15,8 @@ module "dashboard" { module.widget_current_values, module.widget_expressions, module.widget_annotations, + module.widget_metric_many_module, + module.widget_expression_many_module, ] } @@ -211,3 +213,62 @@ module "widget_annotations" { module.annotation_last_release, ] } + +# Widget using metric/many and metric_expression/many modules + +locals { + cpu_utilization_stats = { + min = "Minimum" + average = "Average" + max = "Maximum" + } +} + +module "metrics_cpu_utilization" { + source = "./../../metric/many" + + vars_map = { for k, stat in local.cpu_utilization_stats : k => { + namespace = "AWS/EC2" + name = "CPUUtilization" + stat = stat + period = 300 + label = "${stat} EC2 CPU utilization" + } } +} + +module "widget_metric_many_module" { + source = "./.." + + title = "metric/many module" + left_metrics = [ + module.metrics_cpu_utilization.out_map.min, + module.metrics_cpu_utilization.out_map.average, + module.metrics_cpu_utilization.out_map.max, + ] + left_range = [0, 100] +} + +module "expressions_cpu_utilization_rate" { + source = "./../../metric_expression/many" + + vars_map = { for k, stat in local.cpu_utilization_stats : k => { + expression = "RATE(${module.metrics_cpu_utilization.out_map[k].id})" + label = "${stat} EC2 CPU utilization rate" + } } +} + +module "widget_expression_many_module" { + source = "./.." + + title = "metric_expression/many module" + left_metrics = [ + module.expressions_cpu_utilization_rate.out_map.min, + module.expressions_cpu_utilization_rate.out_map.average, + module.expressions_cpu_utilization_rate.out_map.max, + ] + hidden_metrics = [ + module.metrics_cpu_utilization.out_map.min, + module.metrics_cpu_utilization.out_map.average, + module.metrics_cpu_utilization.out_map.max, + ] +} From 237d485b2f14cef31d138a948e8ea62b4c3d39a6 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Wed, 29 Apr 2020 14:08:20 +0200 Subject: [PATCH 6/7] docs(cloudwatch/metric/many): updated the readme --- cloudwatch/metric/many/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudwatch/metric/many/README.md b/cloudwatch/metric/many/README.md index 2b97eff1..abe760d1 100644 --- a/cloudwatch/metric/many/README.md +++ b/cloudwatch/metric/many/README.md @@ -12,11 +12,11 @@ Same as [cloudwatch/metric](./..) but allows for creating many metrics using a s ## Inputs -* `vars` (`any`, required) +* `vars` (`any`, default: `[]`) List of [cloudwatch/metric](./..) variables -* `vars_map` (`any`, required) +* `vars_map` (`any`, default: `{}`) Map of [cloudwatch/metric](./..) variables From ca333c9d05f8084dc17212b53e035006da7eb0f4 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski Date: Wed, 29 Apr 2020 14:08:28 +0200 Subject: [PATCH 7/7] docs(cloudwatch/metric_expression/many): updated the readme --- cloudwatch/metric_expression/many/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cloudwatch/metric_expression/many/README.md b/cloudwatch/metric_expression/many/README.md index 06f12e2d..5e92a793 100644 --- a/cloudwatch/metric_expression/many/README.md +++ b/cloudwatch/metric_expression/many/README.md @@ -12,11 +12,11 @@ Same as [cloudwatch/metric_expression](./..) but allows for creating many metric ## Inputs -* `vars` (`any`, required) +* `vars` (`any`, default: `[]`) List of [cloudwatch/metric_expression](./..) variables -* `vars_map` (`any`, required) +* `vars_map` (`any`, default: `{}`) Map of [cloudwatch/metric_expression](./..) variables