Skip to content

Commit 9c389f6

Browse files
committed
Add support for Metrics Insights Alarms
1 parent 1076f59 commit 9c389f6

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

.changelog/28547.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/aws_cloudwatch_metric_alarm: Add `period` attribute to `metric_query` in order to support AWS CloudWatch Metrics Insights Alarms.
3+
```

internal/service/cloudwatch/metric_alarm.go

+11
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ func ResourceMetricAlarm() *schema.Resource {
8282
Optional: true,
8383
ValidateFunc: validation.StringLenBetween(1, 1024),
8484
},
85+
"period": {
86+
Type: schema.TypeInt,
87+
Optional: true,
88+
ValidateFunc: validation.Any(
89+
validation.IntDivisibleBy(60),
90+
validation.IntInSlice([]int{10, 30})),
91+
},
8592
"metric": {
8693
Type: schema.TypeList,
8794
MaxItems: 1,
@@ -598,6 +605,7 @@ func flattenMetricAlarmMetrics(metrics []*cloudwatch.MetricDataQuery) []map[stri
598605
"id": aws.StringValue(mq.Id),
599606
"label": aws.StringValue(mq.Label),
600607
"return_data": aws.BoolValue(mq.ReturnData),
608+
"period": int(aws.Int64Value(mq.Period)),
601609
}
602610
if mq.MetricStat != nil {
603611
metric := flattenMetricAlarmMetricsMetricStat(mq.MetricStat)
@@ -638,6 +646,9 @@ func expandMetricAlarmMetrics(v *schema.Set) []*cloudwatch.MetricDataQuery {
638646
if v, ok := metricQueryResource["expression"]; ok && v.(string) != "" {
639647
metricQuery.Expression = aws.String(v.(string))
640648
}
649+
if v, ok := metricQueryResource["period"]; ok && v.(int) > 0 {
650+
metricQuery.Period = aws.Int64(int64(v.(int)))
651+
}
641652
if v, ok := metricQueryResource["label"]; ok && v.(string) != "" {
642653
metricQuery.Label = aws.String(v.(string))
643654
}

internal/service/cloudwatch/metric_alarm_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ func TestAccCloudWatchMetricAlarm_expression(t *testing.T) {
459459
resource.TestCheckResourceAttr(resourceName, "metric_query.#", "2"),
460460
),
461461
},
462+
{
463+
Config: testAccMetricAlarmConfig_metricsInsightsQueryExpression(rName),
464+
Check: resource.ComposeTestCheckFunc(
465+
testAccCheckMetricAlarmExists(resourceName, &alarm),
466+
resource.TestCheckResourceAttr(resourceName, "metric_query.#", "1"),
467+
resource.TestCheckResourceAttr(resourceName, "metric_query.0.period", "300")),
468+
},
462469
{
463470
ResourceName: resourceName,
464471
ImportState: true,
@@ -1000,6 +1007,27 @@ resource "aws_cloudwatch_metric_alarm" "test" {
10001007
`, rName)
10011008
}
10021009

1010+
func testAccMetricAlarmConfig_metricsInsightsQueryExpression(rName string) string {
1011+
expression := "SELECT AVG(CPUUtilization) FROM SCHEMA(\\\"AWS/EC2\\\", InstanceId)"
1012+
return fmt.Sprintf(`
1013+
resource "aws_cloudwatch_metric_alarm" "test" {
1014+
alarm_name = "%s"
1015+
comparison_operator = "GreaterThanOrEqualToThreshold"
1016+
evaluation_periods = "2"
1017+
threshold = "80"
1018+
alarm_description = "This metric monitors ec2 cpu utilization"
1019+
insufficient_data_actions = []
1020+
1021+
metric_query {
1022+
id = "e1"
1023+
expression = "%s"
1024+
period = 300
1025+
return_data = true
1026+
}
1027+
}
1028+
`, rName, expression)
1029+
}
1030+
10031031
// EC2 Automate requires a valid EC2 instance
10041032
// ValidationError: Invalid use of EC2 'Recover' action. i-abc123 is not a valid EC2 instance.
10051033
func testAccMetricAlarmConfig_actionsEC2Automate(rName, action string) string {

website/docs/r/cloudwatch_metric_alarm.html.markdown

+23-1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ resource "aws_cloudwatch_metric_alarm" "nlb_healthyhosts" {
166166
}
167167
```
168168

169+
## Example with AWS Metric Insights Alarm
170+
171+
```terraform
172+
resource "aws_cloudwatch_metric_alarm" "this" {
173+
alarm_name = "alarmname"
174+
alarm_description = "Alarm if any of the EC2 instances go above 50% in CPU utilization."
175+
comparison_operator = "GreaterThanThreshold"
176+
evaluation_periods = 1
177+
datapoints_to_alarm = 1
178+
threshold = 50
179+
ok_actions = [aws_sns_topic.sns.arn]
180+
alarm_actions = [aws_sns_topic.sns.arn]
181+
metric_query {
182+
id = "m1"
183+
return_data = true
184+
period = 60
185+
expression = "SELECT AVG(CPUUtilization) FROM SCHEMA(\"AWS/EC2\", InstanceId)"
186+
}
187+
}
188+
```
189+
169190
~> **NOTE:** You cannot create a metric alarm consisting of both `statistic` and `extended_statistic` parameters.
170191
You must choose one or the other
171192

@@ -215,9 +236,10 @@ The following values are supported: `ignore`, and `evaluate`.
215236

216237
* `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.
217238
* `account_id` - (Optional) The ID of the account where the metrics are located, if this is a cross-account alarm.
218-
* `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](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html#metric-math-syntax).
239+
* `expression` - (Optional) The math expression or Metric Insights query 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](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html#metric-math-syntax).
219240
* `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.
220241
* `return_data` (Optional) Specify exactly one `metric_query` to be `true` to use that `metric_query` result as the alarm.
242+
* `period` (Optional) The period in seconds over which the specified AWS Metric Insights query in `expression` is applied. Should only be used when `expression` is AWS Metric Insights query. For metrics with regular resolution, the value must be a multiple of 60. For high-resolution metrics that are collected at intervals of less than one minute, the period can be 1, 5, 10, 30, 60, or any multiple of 60.
221243
* `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.
222244

223245
~> **NOTE:** You must specify either `metric` or `expression`. Not both.

0 commit comments

Comments
 (0)