Bootstrap FreeKB - Amazon Web Services (AWS) - List Cloudwatch Alarms using the AWS CLI
Amazon Web Services (AWS) - List Cloudwatch Alarms using the AWS CLI

Updated:   |  Amazon Web Services (AWS) articles

This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

Cloudwatch alarms can be used to alert you when some conditiion is met, such as:

  • an EC2 instance has high CPU or high memory usage
  • an EC2 instance in a Load Balancer Target Group health check returns "unhealthy"
  • a Lambda Function has one or more errors

The aws cloudwatch describe-alarms command can be used to list the cloudwatch alarms you have created.

aws cloudwatch describe-alarms

 

And here is an example of how to limit the output using the --query option.

aws cloudwatch describe-alarms --query 'MetricAlarms[?AlarmName==`high-cpu`]'

 

Additionally, the aws cloudwatch describe-alarms-for-metric command can be used to list the Cloudwatch Alarms that match a certain metric name and namespace and dimension.

aws cloudwatch describe-alarms-for-metric --metric-name CPUUtilization --namespace AWS/EC2 --dimensions Name=InstanceId,Value=i-123abd456efg123abc45

 

If no alarms have been created, something like this should be returned.

{
    "MetricAlarms": [],
    "CompositeAlarms": []
}

 

On the other hand, if one of more cloudwatch alarms have been created, something like this should be returned.

{
    "MetricAlarms": [
        {
            "AlarmName": "high-cpu",
            "AlarmArn": "arn:aws:cloudwatch:us-east-1:123456789012:alarm:high-cpu",
            "AlarmDescription": "CPU Utilization exceeds 10% in the last 5 minutes",
            "AlarmConfigurationUpdatedTimestamp": "2023-06-21T01:53:16.629000+00:00",
            "ActionsEnabled": true,
            "OKActions": [],
            "AlarmActions": [
                "arn:aws:sns:us-east-1:123456789012:my-topic"
            ],
            "InsufficientDataActions": [
                "arn:aws:sns:us-east-1:123456789012:my-topic"
            ],
            "StateValue": "OK",
            "StateReason": "Threshold Crossed: 1 datapoints [1.3967213114600159 (21/06/23 01:44:00)] was not greater than the threshold (10.0).",
            "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2024-01-16T06:23:48.140+0000\",\"startDate\":\"2024-01-16T06:20:00.000+0000\",\"unit\":\"Percent\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0015606770489069999],\"threshold\":10.0,\"evaluatedDatapoints\":[{\"timestamp\":\"2024-01-16T06:20:00.000+0000\",\"sampleCount\":1.0,\"value\":0.0015606770489069999}]}",
            "StateUpdatedTimestamp": "2023-06-21T01:54:00.144000+00:00",
            "MetricName": "CPUUtilization",
            "Namespace": "AWS/EC2",
            "Statistic": "Average",
            "Dimensions": [
                {
                    "Name": "InstanceId",
                    "Value": "i-0d241234f9665abcd"
                }
            ],
            "Period": 300,
            "Unit": "Percent",
            "EvaluationPeriods": 1,
            "Threshold": 10.0,
            "ComparisonOperator": "GreaterThanThreshold"
        }
    ],
    "CompositeAlarms": []
}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter cbc198 in the box below so that we can be sure you are a human.