When attempting to create alarms in AWS console Cloudwatch, you may find that the Metric required for a test is not showing. Below, we'll discuss ways to create Alarms despite not being able to find it in during the Alarm creation workflow.
Prerequisites
- AWS Cloudwatch
-
You need to have the necessary IAM permissions to create metrics and alarms in CloudWatch.
Alarm Creation and Hidden Metrics
Due to how metrics work on AWS Cloudwatch, if no new data points are collected in the past two weeks, the metric is hidden from the console. For example:
In the test - Load balancer server errors monitored (AWS)
We require that you set up an alarm for each load balancer, and the alarm must monitor one of these 5 metrics:
HTTPCode_ELB_5XX
HTTPCode_ELB_5XX_Count
HTTPCode_Backend_5XX
HTTPCode_Target_5XX_Count
ApplicationRequests5xx
However, When we search for this metric during alarm creation, we find no results:
This is because the Load Balancer in this example has not logged an 5xx HTTP Code in the last 2 weeks. You can read more on AWS Cloudwatch metrics in the article below:
How to Create an Alarm on Hidden Metrics
There are two ways you can do this. Either through the AWS CLI, or through the Alarm builder, under the query tab. However, if they aren't appearing there either, you can create them directly through the CLI.
Query Builder Example:
Example CLI:
Creating an alarm on a metric with no data points is possible however, The alarm will start in an INSUFFICIENT_DATA state but will transition to an `ALARM` or `OK` state when the metric data points are available.
Here's an example:
aws cloudwatch put-metric-alarm --alarm-name [AlarmName] --metric-name [MetricName] --namespace [Namespace] --statistic Average --period 300 --threshold 100 --comparison-operator GreaterThanThreshold --evaluation-periods 2 --alarm-description "Alarm when metric exceeds threshold" --actions-enabled --alarm-actions [SNS Topic ARN] --unit Seconds
In this command, replace [AlarmName] with the name you want for your alarm, [MetricName] and [Namespace] with your metric name and namespace, and [ SNS Topic ARN] with the ARN of an Amazon SNS topic where the alarm will send a notification.
Example Terraform:
resource "aws_cloudwatch_metric_alarm" "cpu_alarm" {
alarm_name = "cpu-utilization-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 300 # 5 minutes
statistic = "Average"
threshold = 80
alarm_description = "Alarm when CPU utilization exceeds 80% for 2 consecutive periods"
dimensions = {
InstanceId = "your-instance-id" # Replace with your EC2 instance ID
}
alarm_actions = ["arn:aws:sns:us-east-1:123456789012:my-sns-topic"] # Replace with your SNS topic ARN
}
Note:
- The `--unit` parameter is optional and may not be necessary depending on your metric.
- Make sure to change `--threshold`, `--statistic`, `--period`, and `--evaluation-periods` as needed to suit your use case.
- The AWS CLI must be installed and properly configured with your credentials to run this command.
Additional Resources
- https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-data.html
- https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-statistics.html
- https://stackoverflow.com/questions/68674013/why-do-some-metrics-missing-in-cloudwatch-metrics-view
- https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/viewing_metrics_with_cloudwatch.html