简体   繁体   English

AWS - 使用 CloudWatch 在 ec2 实例上触发脚本

[英]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.我发现了很多类似的问题,但似乎没有一个是我的问题所特有的:我有一个删除旧日志文件的 python 脚本。 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.我有 Cloudwatch 来监控可用存储,是否可以在触发警报时触发该脚本(IE,运行此命令:python mypythonscript.py)。

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.从我所有的研究来看,这似乎是不可能的,除非我们开发了一个系统来解释 CloudWatch 和 SNS 发送的电子邮件,然后根据该解释运行脚本。

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.目标是在发出警报时自我修复此问题,但从我们收集到的信息来看,CloudWatch 本身似乎不可能触发 EC2 实例上发生的任何事情。 If this is true, I'm just looking for confirmation from people smarter than I!如果这是真的,我只是在寻找比我更聪明的人的确认!

Thanks!谢谢!

CloudWatch, by itself, cannot run a command on your EC2 instance. CloudWatch 本身无法在您的 EC2 实例上运行命令。 You could however combine CloudWatch with some other AWS services to accomplish your goal.但是,您可以将 CloudWatch 与其他一些 AWS 服务结合使用来实现您的目标。 The way I would recommend accomplishing this is the following:我建议完成此操作的方式如下:

Or you can use或者你可以使用

CloudWatch Events for execute Run Command:用于执行运行命令的 CloudWatch 事件:

https://docs.aws.amazon.com/systems-manager/latest/userguide/rc-cwe.html https://docs.aws.amazon.com/systems-manager/latest/userguide/rc-cwe.html

To expand on @Viviane's answer: Create an EventBridge event.扩展@Viviane 的回答:创建一个 EventBridge 事件。 Have it trigger off a custom event pattern to match your CloudWatch events of interest (these events are auto-pub'd to EventBridge).让它触发自定义事件模式以匹配您感兴趣的 CloudWatch 事件(这些事件会自动发布到 EventBridge)。 These links helped me:这些链接帮助了我:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM