How to trigger a script when CloudWatch alarm is triggered

Viewed 698

Based on CloudWatch logs I have created a metric using a Metric Filter.

Based on this metric I created an Alarm. The alarm is triggered when the count goes more than 3 through alert. Now if the alarm is triggered I would like to run a script.

How would I trigger the script what AWS service would I use?

One additional use case: I need to read the logs that triggered the alarm so that the script can get more information and act according to the information provided.

1 Answers

When an Amazon CloudWatch Alarm is triggered it can:

  • Send a message to an Amazon SNS topic
  • Start/Stop/Terminate/Reboot an Amazon EC2 instance
  • Auto-Scale EC2/ECS

Therefore, to trigger a script when the Alarm activates, you will need to:

  • Create an Amazon SNS topic
  • Create an AWS Lambda function to run your script
  • Subscribe the Lambda function to the SNS topic
  • Configure the alarm to send a message to the SNS topic when it enters the ALARM state

However, there is no direct relationship between the Alarm and the data in CloudWatch Logs that generated the metric. Thus, it is not possible to provide information about why the alarm was triggered. It is merely an accumulated metric over a period of time.

While the Lambda function could read the data from CloudWatch logs, it would not be easy to identify the specific lines that led to the alarm being triggered.

Related