Bootstrap FreeKB - Amazon Web Services (AWS) - Set CloudWatch Alarm state using the AWS CLI
Amazon Web Services (AWS) - Set CloudWatch Alarm state 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.

Let's say you have an alarm named high-cpu that publishes a message to one of your Simple Notification Service (SNS) Topics if one of your EC2 instances CPU Utilization is greater than 10% in the last 5 minutes. The aws cloudwatch describe-alarms command can be used your CloudWatch Alarms. Something like this should be returned.

BE CAREFUL!

Notice in this example that when the alarm is triggered, the AlarmAction will reboot the EC2 instance.

{
    "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:swf:us-east-1:123456789012:action/actions/AWS_EC2.InstanceId.Reboot/1.0",
                "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": []
}

 

The aws cloudwatch set-alarm-state command can be used to update the Alarm StateValue. Notice in the above output the StateValue is OK. In this example, the Alarm StateValue will be updated to ALARM and StateReason will be testing.

aws cloudwatch set-alarm-state --alarm-name high-cpu --state-value ALARM --state-reason "testing"

 

Then if you reissue the aws cloudwatch describe-alarms command the Alarm StateValue should be ALARM and the StateReason would be testing. Since this alarm puslishes as message to your Simple Notification Service (SNS) Topics, the subscribers of the topic should be notified.

~]$ aws cloudwatch describe-alarms --query 'MetricAlarms[?AlarmName==`high-cpu`]'
[
    {
        "Dimensions": [
            {
                "Name": "InstanceId",
                "Value": "i-0d241234f9665abcd"
            }
        ],
        "Namespace": "AWS/EC2",
        "DatapointsToAlarm": 1,
        "ActionsEnabled": true,
        "MetricName": "CPUUtilization",
        "EvaluationPeriods": 1,
        "StateValue": "ALARM",
        "StateUpdatedTimestamp": "2024-01-24T02:31:49.162Z",
        "AlarmConfigurationUpdatedTimestamp": "2024-01-24T02:14:19.711Z",
        "AlarmActions": [
            "arn:aws:swf:us-east-1:123456789012:action/actions/AWS_EC2.InstanceId.Reboot/1.0",
            "arn:aws:sns:us-east-1:123456789012:my-topic"
        ],
        "InsufficientDataActions": [
            "arn:aws:sns:us-east-1:123456789012:my-topic"
        ],
        "AlarmArn": "arn:aws:cloudwatch:us-east-1:123456789012:alarm:high-cpu",
        "Threshold": 10.0,
        "StateReason": "testing",
        "OKActions": [],
        "AlarmDescription": "CPU Utilization exceeds 10% in the last 5 minutes",
        "Period": 300,
        "ComparisonOperator": "GreaterThanThreshold",
        "AlarmName": "high-cpu",
        "Statistic": "Average",
        "TreatMissingData": "missing",
        "Unit": "Percent"
    }
]

 

 

 




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 a722ae in the box below so that we can be sure you are a human.