I have an EC2 instance that I want to trigger to run a script when certain events happen in another AWS service. I see a lot about SNS leading to SQS or Lambda, primarily for notifications, but I'm looking for a direct way to have my target EC2 execute a script based on these events. Specifically, my auto-scaling group sends notifications to an SNS topic on launch or termination events, and I need my custom load balancer (which resides on an EC2 instance) to adjust the server pool accordingly. From my research, it seems that using SNS to subscribe doesn't allow for immediate event-driven responses, and SQS still necessitates polling. The closest I've found for real-time management involves using the SSM agent to let a Lambda function respond to SNS messages and trigger commands on my EC2. This feels complicated and may not be the best route. Is there a more straightforward way to turn SNS messages into EC2 actions reactively without having to resort to polling?
3 Answers
Just a heads up, make sure you're using health checks as well to manage your instance pool effectively.
You should check out EventBridge. It captures AWS events and can trigger actions based on those events. You can set rules for the events and have them execute actions on your EC2 using SSM to run your command. It might simplify your setup compared to other methods.
Thanks! I looked into EventBridge, but I thought you couldn't run specific commands directly on an EC2 target.
You're on the right track, but with the right setup in EventBridge, you can trigger SSM documents which can run commands for you.
To achieve what you want, consider using EventBridge to call an SSM document that executes your script through the Run Command feature. It can make things much easier for you.
Got it! I’ll explore that tomorrow, thanks!

Totally! HAProxy has built-in health checks already.