diff --git a/cloudwatch/metric/many/README.md b/cloudwatch/metric/many/README.md new file mode 100644 index 00000000..abe760d1 --- /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`, default: `[]`) + + List of [cloudwatch/metric](./..) variables + +* `vars_map` (`any`, default: `{}`) + + 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..290d8021 --- /dev/null +++ b/cloudwatch/metric/many/variables.tf @@ -0,0 +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 = {} +} 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" +} diff --git a/cloudwatch/metric_expression/many/README.md b/cloudwatch/metric_expression/many/README.md new file mode 100644 index 00000000..5e92a793 --- /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`, default: `[]`) + + List of [cloudwatch/metric_expression](./..) variables + +* `vars_map` (`any`, default: `{}`) + + 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..f0da6670 --- /dev/null +++ b/cloudwatch/metric_expression/many/variables.tf @@ -0,0 +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 = {} +} 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" +} 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, + ] +}