To trigger or test an Amazon CloudWatch alarm using the AWS CLI, you have to run the command
aws cloudwatch set-alarm-state in your terminal.
Note: When using an AWS CLI command to trigger and test an existing CloudWatch alarm, make sure to log in to the specified AWS CLI profile. Otherwise, you can’t run any AWS CLI commands on your AWS account.
Here are the steps to trigger a CloudWatch alarm and verify that the state has changed, so you can test if it successfully executes a configured action like sending a message to an Amazon SNS topic.
Table of Contents
How to test a CloudWatch alarm using AWS CLI
Let’s say you created an alarm for a specific metric on an AWS resource you wish to monitor more proactively.
You want to make sure that when the alarm triggers the right action is taken automatically like executing autoscaling or sending a message to an SNS topic.
You can use the AWS CLI to change the state of the Cloudwatch Alarm to the following available values:
This gives you the flexibility to alter the state of the alarm and run automated actions on behalf of CloudWatch.
1. Change the state of the CloudWatch alarm
To change the state of the alarm run the following command:
aws cloudwatch set-alarm-state --alarm-name "ec2-cpu-above-75-percent" --state-reason "Testing the Amazon Cloudwatch alarm" --state-value ALARM
Note: make sure to replace the
--alarm-name option with your own custom CloudWatch alarm name.
2. Verify if the CloudWatch alarm changed state successfully
Log in to the AWS Console and go to the CloudWatch service. Find the alarm that you’ve triggered with the previous command and check the “History” tab.
Here you can verify if the alarm has changed state and executed your action successfully.
As you can see the alarm updated its state from “OK” to “in alarm” and afterward triggered the action to send a message to an SNS topic that I’ve configured for the alarm.
You’ve seen how you can trigger any Amazon CloudWatch alarm on your AWS account using the
set-alarm-state command using the AWS CLI.
Next to that, you can use the AWS Console to verify and validate that any action you’ve configured to run with an alarm state change gets executed correctly using the “History” tab on the AWS CloudWatch alarms page.