Provides a CloudWatch Metric Alarm resource.
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar5"
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"
insufficient_data_actions = []
}
resource "aws_autoscaling_policy" "bat" {
name = "foobar3-terraform-test"
scaling_adjustment = 4
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.bar.name
}
resource "aws_cloudwatch_metric_alarm" "bat" {
alarm_name = "terraform-test-foobar5"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 120
statistic = "Average"
threshold = 80
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.bar.name
}
alarm_description = "This metric monitors ec2 cpu utilization"
alarm_actions = [aws_autoscaling_policy.bat.arn]
}
resource "aws_cloudwatch_metric_alarm" "foobar" {
alarm_name = "terraform-test-foobar"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 2
threshold = 10
alarm_description = "Request error rate has exceeded 10%"
insufficient_data_actions = []
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"
}
}
}
}
resource "aws_cloudwatch_metric_alarm" "xx_anomaly_detection" {
alarm_name = "terraform-test-foobar"
comparison_operator = "GreaterThanUpperThreshold"
evaluation_periods = 2
threshold_metric_id = "e1"
alarm_description = "This metric monitors ec2 cpu utilization"
insufficient_data_actions = []
metric_query {
id = "e1"
expression = "ANOMALY_DETECTION_BAND(m1)"
label = "CPUUtilization (Expected)"
return_data = "true"
}
metric_query {
id = "m1"
return_data = "true"
metric {
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 120
stat = "Average"
unit = "Count"
dimensions = {
InstanceId = "i-abc123"
}
}
}
}
resource "aws_cloudwatch_metric_alarm" "nlb_healthyhosts" {
alarm_name = "alarmname"
comparison_operator = "LessThanThreshold"
evaluation_periods = 1
metric_name = "HealthyHostCount"
namespace = "AWS/NetworkELB"
period = 60
statistic = "Average"
threshold = var.logstash_servers_count
alarm_description = "Number of healthy nodes in Target Group"
actions_enabled = "true"
alarm_actions = [aws_sns_topic.sns.arn]
ok_actions = [aws_sns_topic.sns.arn]
dimensions = {
TargetGroup = aws_lb_target_group.lb-tg.arn_suffix
LoadBalancer = aws_lb.lb.arn_suffix
}
}
See related part of AWS Docs for details about valid values.
This argument supports the following arguments:
alarm_name
- (Required) The descriptive name for the alarm. This name must be unique within the user's AWS accountcomparison_operator
- (Required) 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 values LessThanLowerOrGreaterThanUpperThreshold
, LessThanLowerThreshold
, and GreaterThanUpperThreshold
are used only for alarms based on anomaly detection models.evaluation_periods
- (Required) The number of periods over which data is compared to the specified threshold.metric_name
- (Optional) The name for the alarm's associated metric.
See docs for supported metrics.namespace
- (Optional) The namespace for the alarm's associated metric. See docs for the list of namespaces.
See docs for supported metrics.period
- (Optional) The period in seconds over which the specified statistic
is applied.
Valid values are 10
, 30
, or any multiple of 60
.statistic
- (Optional) The statistic to apply to the alarm's associated metric.
Either of the following is supported: SampleCount
, Average
, Sum
, Minimum
, Maximum
threshold
- (Optional) 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.threshold_metric_id
- (Optional) If this is an alarm based on an anomaly detection model, make this value match the ID of the ANOMALY_DETECTION_BAND function.actions_enabled
- (Optional) Indicates whether or not actions should be executed during any changes to the alarm's state. Defaults to true
.alarm_actions
- (Optional) 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).alarm_description
- (Optional) The description for the alarm.datapoints_to_alarm
- (Optional) The number of datapoints that must be breaching to trigger the alarm.dimensions
- (Optional) The dimensions for the alarm's associated metric. For the list of available dimensions see the AWS documentation here.insufficient_data_actions
- (Optional) 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).ok_actions
- (Optional) 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).unit
- (Optional) The unit for the alarm's associated metric.extended_statistic
- (Optional) The percentile statistic for the metric associated with the alarm. Specify a value between p0.0 and p100.treat_missing_data
- (Optional) Sets how this alarm is to handle missing data points. The following values are supported: missing
, ignore
, breaching
and notBreaching
. Defaults to missing
.evaluate_low_sample_count_percentiles
- (Optional) 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
.metric_query
(Optional) Enables you to create an alarm based on a metric math expression. You may specify at most 20.tags
- (Optional) A map of tags to assign to the resource. If configured with a provider default_tags
configuration block present, tags with matching keys will overwrite those defined at the provider-level.metric_query
id
- (Required) A short name used to tie this object to the results in the response. If you are performing math expressions on this set of data, this name represents that data and can serve as a variable in the mathematical expression. The valid characters are letters, numbers, and underscore. The first character must be a lowercase letter.account_id
- (Optional) The ID of the account where the metrics are located, if this is a cross-account alarm.expression
- (Optional) The math expression to be performed on the returned data, if this object is performing a math expression. This expression can use the id of the other metrics to refer to those metrics, and can also use the id of other expressions to use the result of those expressions. For more information about metric math expressions, see Metric Math Syntax and Functions in the Amazon CloudWatch User Guide.label
- (Optional) A human-readable label for this metric or expression. This is especially useful if this is an expression, so that you know what the value represents.metric
- (Optional) The metric to be returned, along with statistics, period, and units. Use this parameter only if this object is retrieving a metric and not performing a math expression on returned data.period
- (Optional) Granularity in seconds of returned data points.
For metrics with regular resolution, valid values are any multiple of 60
.
For high-resolution metrics, valid values are 1
, 5
, 10
, 30
, or any multiple of 60
.return_data
- (Optional) Specify exactly one metric_query
to be true
to use that metric_query
result as the alarm.metric
dimensions
- (Optional) The dimensions for this metric. For the list of available dimensions see the AWS documentation here.metric_name
- (Required) The name for this metric.
See docs for supported metrics.namespace
- (Required) The namespace for this metric. See docs for the list of namespaces.
See docs for supported metrics.period
- (Required) Granularity in seconds of returned data points.
For metrics with regular resolution, valid values are any multiple of 60
.
For high-resolution metrics, valid values are 1
, 5
, 10
, 30
, or any multiple of 60
.stat
- (Required) The statistic to apply to this metric.
See docs for supported statistics.unit
- (Optional) The unit for this metric.This resource exports the following attributes in addition to the arguments above:
arn
- The ARN of the CloudWatch Metric Alarm.id
- The ID of the health check.tags_all
- A map of tags assigned to the resource, including those inherited from the provider default_tags
configuration block.In Terraform v1.5.0 and later, use an import
block to import CloudWatch Metric Alarm using the alarm_name
. For example:
import {
to = aws_cloudwatch_metric_alarm.test
id = "alarm-12345"
}
Using terraform import
, import CloudWatch Metric Alarm using the alarm_name
. For example:
% terraform import aws_cloudwatch_metric_alarm.test alarm-12345