Using CloudWatch and SNS to trigger event notification
Being aware of your current state of mind is very important! So doesn’t it make sense to be aware of your current state of cloud services as well? Think about it, What if any of your cloud service went down due to increase in load or network traffic increase or state of any of your resource changes during an Sunday evening? I don't mean to interrupt your Sunday experience but surely you need to be notified regarding the issue and need to quickly fix it before it disrupts your entire infrastructure severely.
Let’s learn how to get notified when an EC2 Instance changes it State using CloudWatch and SNS (Simple Notification Service). Let’s dive into it!
How it’s done?
- You create an event on CloudWatch to monitor the state of your EC2 Instance and trigger an event notification
- You create a SNS topic with your preferred Endpoint (Phone, Email Address, etc.) to be notified
- You get notified on your Endpoint device about the change in the state of your EC2 Instance
Creating SNS Topic and Subscription
Let’s first create an SNS Topic and Subscription, so we have an topic to select during creation of CloudWatch event
- Go to Amazon SNS (Simple Notification Service) > Topics > Create topic
2. Enter the Topic Type and Name of the Topic
We’ll use a Standard Type here to be able to receive the notification via an SMS
3. Now, we need to Create subscription with details of our Endpoint
4. Select the Protocol (SMS, Email, etc.), I want to be notified via SMS that’s why I’ll be selecting SMS. Lastly enter your Endpoint (Mobile Number in our case)
Note: If you are not able to select your preferred Endpoint from a drop-down list or if you don’t see any registered/verified Endpoint (Mobile Number). You might need to register and verify one using below process
Add a Phone Number (Register and verify), Once done you’ll be able to see the number marked as verified and listed with other numbers (if present)
Creating A CloudWatch Event
Now Let’s create a CloudWatch event to monitor the state of our EC2 Instance and notify us.
- We need a Rule to be created, to do that go to
CloudWatch > Events > Rules
2. Enter the Service Name and Event Type
3. Select the state for which you want to be notified and the Instance on which the rule is to be applied (you may select any instance to be notified for all Instances)
4. Add an Target (an SNS Topic to be notified)
5. Configure rule details, Add Name and description then click on Create rule
Once the rule is created it will be listed under rule list
Triggering an Event Notification
All done! Let’s test it.
- Right now, my Instance is in Running state. I’ll stop my instance and see if it notifies me.
Yup! received SMS notification on my mobile with the current state of the EC2 Instance ( Running to Stopped)
2. Let’s Start the Instance and see if we receive the notification
Yes! receiving SMS Notification for that too
That’s how you can use CloudWatch and SNS to get notified for any event for your preferred service. You may also configure different Endpoint to receive notification like Email, SQS, HTTP/HTTPS, etc.
Now go back to your movie and enjoy your Weekend! AWS will notify you if something is fishy 😉
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.