Trigger a Cloudwatch alarm using AWS CLI


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.

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:

  • OK
  • ALARM
  • INSUFFICIENT_DATA

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.

Amazon CloudWatch alarm history overview in the AWS Console

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.

Conclusion

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.



Danny Steenman

A Senior AWS Cloud Engineer with over 9 years of experience migrating workloads from on-premises to AWS Cloud.

I have helped companies of all sizes shape their cloud adoption strategies, optimizing operational efficiency, reducing costs, and improving organizational agility.

Connect with me today to discuss your cloud aspirations, and let’s work together to transform your business by leveraging the power of AWS Cloud.

I need help with..
stacked cubes
Improving or managing my CDK App.Maximize the potential of your AWS CDK app by leveraging the expertise of a seasoned CDK professional.
Reducing AWS Costs.We can start by doing a thorough assessment of your current AWS infrastructure, identifying areas with potential for cost reduction and efficiency improvement.
Verifying if my infrastructure is reliable and efficient.We’ve created a comprehensive AWS Operations Checklist that you can utilize to quickly verify if your AWS Resources are set up reliably and efficiently.