Skip to content

oozou/terraform-aws-cloudwatch-alarm

Repository files navigation

terraform-aws-cloudwatch

Usage simeple usecase

locals {
  comparison_operators = {
    ">=" = "GreaterThanOrEqualToThreshold",
    ">"  = "GreaterThanThreshold",
    "<"  = "LessThanThreshold",
    "<=" = "LessThanOrEqualToThreshold",
  }
}

module "step_alarm" {
  source = "<source>"

  prefix      = oozou
  environment = test
  name        = format("%s-alarm", local.service_name)

  comparison_operator = local.comparison_operators["<="]
  evaluation_periods  = "1"
  metric_name         = CPUUtilization
  namespace           = "AWS/ECS"
  period              = "60"
  statistic           = "Average"
  threshold           = "80"

  dimensions = {
    ClusterName = var.ecs_cluster_name
    ServiceName = local.service_name
  }

  alarm_actions = ["arn-of-the-action"]

  tags = {"Workspace" = "xxx-yyy-zzz"}
}

Usage for complex query

locals {
  comparison_operators = {
    ">=" = "GreaterThanOrEqualToThreshold",
    ">"  = "GreaterThanThreshold",
    "<"  = "LessThanThreshold",
    "<=" = "LessThanOrEqualToThreshold",
  }
}

module "alarm" {
  source = "git@github.com:oozou/terraform-aws-cloudwatch-alarm.git?ref=<version>"

  prefix      = "oozou"
  environment = "devops"
  name        = "kafka-cpu-reach"

  comparison_operator = local.comparison_operators[">="]
  threshold           = "85"
  evaluation_periods  = 1

  # Conflict with metric_query
  metric_name = null
  namespace   = null
  period      = null
  statistic   = null
  dimensions  = null

  metric_query = [
    {
      id          = "e1"
      expression  = "m1+m2"
      label       = "CPUSummation"
      return_data = "true"
      metric      = []
    },
    {
      id = "m1"
      metric = [
        {
          metric_name = "CpuUser"
          namespace   = "AWS/Kafka"
          period      = "60"
          stat        = "Maximum"
          dimensions = {
            "Cluster Name" = "oozou-devops-demo-msk"
            "Broker ID"    = "1"
          }
        }
      ]
    },
    {
      id = "m2"
      metric = [
        {
          metric_name = "CpuSystem"
          namespace   = "AWS/Kafka"
          period      = "60"
          stat        = "Maximum"
          dimensions = {
            "Cluster Name" = "oozou-devops-demo-msk"
            "Broker ID"    = "1"
          }
        }
      ]
    }
  ]

  alarm_actions = ["arn-of-action"]

  tags = { "Workspace" = "xxx-yyy-zzz" }
}

Requirements

Name Version
terraform >= 1.0.0
aws >= 5.0.0

Providers

Name Version
aws 5.1.0

Modules

No modules.

Resources

Name Type
aws_cloudwatch_metric_alarm.this resource

Inputs

Name Description Type Default Required
actions_enabled Indicates whether or not actions should be executed during any changes to the alarm's state. Defaults to true. bool true no
alarm_actions The list of actions to execute when this alarm transitions into an ALARM state from any other state. Each action is specified as an Amazon Resource Name (ARN). list(string) null no
alarm_description The description for the alarm. string null no
comparison_operator The arithmetic operation to use when comparing the specified Statistic and Threshold. The specified Statistic value is used as the first operand. Either of the following is supported: GreaterThanOrEqualToThreshold, GreaterThanThreshold, LessThanThreshold, LessThanOrEqualToThreshold. string n/a yes
datapoints_to_alarm The number of datapoints that must be breaching to trigger the alarm. number null no
dimensions The dimensions for the alarm's associated metric. map(string) {} no
environment Environment Variable used as a prefix string n/a yes
evaluate_low_sample_count_percentiles Used only for alarms based on percentiles. If you specify ignore, the alarm state will not change during periods with too few data points to be statistically significant. If you specify evaluate or omit this parameter, the alarm will always be evaluated and possibly change state no matter how many data points are available. The following values are supported: ignore, and evaluate. string null no
evaluation_periods The number of periods over which data is compared to the specified threshold. number n/a yes
extended_statistic The percentile statistic for the metric associated with the alarm. Specify a value between p0.0 and p100. string null no
insufficient_data_actions The list of actions to execute when this alarm transitions into an INSUFFICIENT_DATA state from any other state. Each action is specified as an Amazon Resource Name (ARN). list(string) null no
metric_name The name for the alarm's associated metric. See docs for supported metrics. string null no
metric_query Enables you to create an alarm based on a metric math expression. You may specify at most 20. any [] no
name Name of the ECS cluster to create string n/a yes
namespace The namespace for the alarm's associated metric. See docs for the list of namespaces. See docs for supported metrics. string null no
ok_actions The list of actions to execute when this alarm transitions into an OK state from any other state. Each action is specified as an Amazon Resource Name (ARN). list(string) null no
period The period in seconds over which the specified statistic is applied. string null no
prefix The prefix name of customer to be displayed in AWS console and resource string n/a yes
statistic The statistic to apply to the alarm's associated metric. Either of the following is supported: SampleCount, Average, Sum, Minimum, Maximum string null no
tags Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys map(any) {} no
threshold The value against which the specified statistic is compared. number null no
threshold_metric_id If this is an alarm based on an anomaly detection model, make this value match the ID of the ANOMALY_DETECTION_BAND function. string null no
treat_missing_data Sets how this alarm is to handle missing data points. The following values are supported: missing, ignore, breaching and notBreaching. string "missing" no
unit The unit for the alarm's associated metric. string null no

Outputs

Name Description
cloudwatch_metric_alarm_arn The ARN of the Cloudwatch metric alarm.
cloudwatch_metric_alarm_id The ID of the Cloudwatch metric alarm.