Terraform Module for AWS AWS CloudWatch Metric Alarm
This is a stable example. It should successfully build out of the box
This examples does is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build.
This module may be used to create AWS CloudWatch Metric Alarm resources in AWS cloud provider......
This module needs Terraform 0.12.19 or newer. You can download the latest Terraform version from here.
This module deploys aws services details are in respective feature branches.
Below we are able to check the resources that are being created as part of this module call:
- AWS CloudWatch Metric Alarm
To use this module, add the following call to your code:
-
Example Usage
-
Example in Conjunction with Scaling Policies
module "cloudwatch_metric_alarm_up" {
source = "git::https://github.com/nitinda/terraform-module-aws-cloudwatch-metric-alarm.git?ref=master"
providers = {
aws = aws.services
}
alarm_name = "AlarmHigh"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
alarm_description = "This metric monitors ec2 cpu utilization"
}
- Example with an Expression
module "cloudwatch_metric_alarm_up" {
source = "git::https://github.com/nitinda/terraform-module-aws-cloudwatch-metric-alarm.git?ref=master"
providers = {
aws = aws.services
}
alarm_name = "AlarmHigh"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 60
statistic = "Average"
threshold = 20
datapoints_to_alarm = 1
dimensions = {
AutoScalingGroupName = module.autoscaling_group.name
}
alarm_description = "This metric monitors ec2 cpu utilization"
alarm_actions = [ module.aws_autoscaling_policy.arn ]
}
- Example of monitoring Healthy Hosts on NLB using Target Group and NLB
module "cloudwatch_metric_alarm_up" {
source = "git::https://github.com/nitinda/terraform-module-aws-cloudwatch-metric-alarm.git?ref=master"
providers = {
aws = aws.services
}
alarm_name = "AlarmHigh"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
threshold = "10"
alarm_description = "Request error rate has exceeded 10%"
metric_query {
id = "e1"
expression = "m2/m1*100"
label = "Error Rate"
return_data = "true"
}
metric_query {
id = "m1"
metric {
metric_name = "RequestCount"
namespace = "AWS/ApplicationELB"
period = "120"
stat = "Sum"
unit = "Count"
dimensions = {
LoadBalancer = "app/web"
}
}
}
metric_query {
id = "m2"
metric {
metric_name = "HTTPCode_ELB_5XX_Count"
namespace = "AWS/ApplicationELB"
period = "120"
stat = "Sum"
unit = "Count"
dimensions = {
LoadBalancer = "app/web"
}
}
}
}
The variables required in order for the module to be successfully called from the deployment repository are the following:
NOTE: You cannot create a metric alarm consisting of both statistic and extended_statistic parameters. You must choose one or the other
Variable | Description | Type | Argument Status | Default Value |
---|---|---|---|---|
alarm_name | The descriptive name for the alarm. This name must be unique within the user's AWS account | string | Required | |
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. Additionally, the valuesLessThanLowerOrGreaterThanUpperThreshold, LessThanLowerThreshold, and GreaterThanUpperThreshold are used only for alarms based on anomaly detection models. | string | Required | |
evaluation_periods | The number of periods over which data is compared to the specified threshold. | number | Required | |
metric_name | The name for the alarm's associated metric | string | Optional | null |
namespace | The namespace for the alarm's associated metric | string | Optional | null |
period | The period in seconds over which the specified statistic is applied | number | Optional | null |
statistic | The statistic to apply to the alarm's associated metric. Either of the following is supported: SampleCount, Average, Sum, Minimum, Maximum | string | Optional | null |
threshold | The value against which the specified statistic is compared. This parameter is required for alarms based on static thresholds, but should not be used for alarms based on anomaly detection models. | number | Optional | null |
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 | Optional | null |
actions_enabled | Indicates whether or not actions should be executed during any changes to the alarm's state. Defaults to true | boolean | Optional | true |
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). | string | Optional | null |
alarm_description | The description for the alarm | string | Optional | null |
datapoints_to_alarm | The number of datapoints that must be breaching to trigger the alarm. | string | Optional | null |
dimensions | The dimensions for the alarm's associated metric. For the list of available dimensions see the AWS documentation here. | string | Optional | null |
alarm_description | The description for the alarm | string | Optional | null |
insufficient_data_actions | The description for the alarm | list(string) | Optional | null |
alarm_description | 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). | string | Optional | null |
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) | Optional | null |
unit | The unit for the alarm's associated metric | string | Optional | null |
extended_statistic | The percentile statistic for the metric associated with the alarm. Specify a value between p0.0 and p100. | string | Optional | null |
treat_missing_data | Sets how this alarm is to handle missing data points. The following values are supported: missing, ignore, breaching and notBreaching. Defaults to missing | string | Optional | missing |
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 | Optional | null |
metric_query | Enables you to create an alarm based on a metric math expression. You may specify at most 20. | any | Optional | null |
tags | A map of tags to assign to the resource. | any | Optional | {} |
NOTE: If you specify at least one metric_query, you may not specify a metric_name, namespace, period or statistic. If you do not specify a metric_query, you must specify each of these (although you may use extended_statistic instead of statistic).
This module has the following outputs:
The ARN of the cloudwatch metric alarm
- arn
The ID of the health check
- id
In order for the variables to be accessed on module level please use the syntax below:
module.<module_name>.<output_variable_name>
The output variable is able to be accessed through terraform state file using the syntax below:
data.terraform_remote_state.<module_name>.<output_variable_name>
Module maintained by Module maintained by the - Nitin Das