Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS - Using CloudWatch to trigger script on ec2 instance

I've found a lot of similar questions but none that seem to be specific to my question here: I have a python script that deletes old log files. I have Cloudwatch to monitor available storage, is it possible to trigger that script (IE, run this command: python mypythonscript.py) when alarm is triggered.

From all my research this does not appear to be possible unless we developed a system that interpreted the emails sent by CloudWatch and SNS and then run a script based on that interpretation.

The goal is self-healing this issue when it alarms, but from what we've gathered it doesn't appear to be possible that CloudWatch itself can trigger anything to happen on the EC2 instance. If this is true, I'm just looking for confirmation from people smarter than I!

Thanks!

like image 261
PDL Avatar asked Dec 28 '16 15:12

PDL


1 Answers

CloudWatch, by itself, cannot run a command on your EC2 instance. You could however combine CloudWatch with some other AWS services to accomplish your goal. The way I would recommend accomplishing this is the following:

  • Create an AWS EC2 Run Command that executes your Python script.
  • Create an AWS Lambda function that invokes the Run Command when called.
  • Configure the Lambda function to use the CloudWatch alerts SNS topic as a trigger.
like image 91
Mark B Avatar answered Nov 02 '22 09:11

Mark B