📌Simple Notification Service (Amazon SNS) is a web service that coordinates and manages the delivery or sending of messages to subscribing endpoints or clients.
❎Components
📌EC2 Instance — An Amazon EC2 instance is a virtual server in Amazon’s Elastic Compute Cloud (EC2) for running applications on the Amazon Web Services (AWS) infrastructure.
📌- Amazon CloudWatch monitors your Amazon Web Services (AWS) resources and the applications you run on AWS in real time.
❎In the below hands-on steps, we are going to create an ec2 instance, one CloudWatch alarm and one SNS topic . So here we want to show when the CPU utilization of the server goes beyond 40%, the ec2 instance should be stopped and we should receive an email in the registered email ID.
❎Create an EC2 instance with all default settings except on Network settings.
Follow below to setup network settings:
❎Now we are going to create SNS. Search for Amazon Simple Notification Service and give a topic to the text area as below:
❎Under Details , we’re selecting standard type as FIFO is strictly for message delivering.
❎Delivery status logging — click on Create new service role and create new role. We will get a new tab for the IAM console.
❎IAM Management Console — we’re going with the default settings and click on allow.
❎All the other settings are set be default in SNS.Click on create subscription
❎ Now we need to enter the email ID which we need to receive notifications.
❎other settings are default and click on create subscription.
❎We will receive an email to the above specified ID for the confirmation on subscription.
(Please check spam folder also if did not received in Inbox)
❎Click on confirm subscription
❎We could see the email ID is confirmed from SNS page
❎we are going to create CloudWatch
❎G oto Dashboards → Create Dashboard → give a name → Add Widget → select Number → Click on Next. Then give a graph name ( here : ec2-app-server) then select EC2 → Click on Per-Instance metrics .
❎Now give instance ID from the ec2 instance we have created and select the metric name as per your wish. Here we’re selecting CPU Utilization.
❎Click on Create Widget. We can also create multiple widgets by clicking Duplicate and change widget type to desired type.
❎Go to Alarms → In alarm → Create Alarm → Select Metric → click on EC2 → Per-Instance Metrics → select CPU Utilization → click on Select Metric
❎Now we’re going to give the condition that if utilization goes greater than 40% then we need to get notified.
❎Then click on Next. Select the Dashboard name which we have created
❎specify what action needs to be taken if utilization goes beyond 40%. Here we’re selecting to stop instances.
❎Give an alarm name and description and click next → Create Alarm
❎to the server ssh connection (here I’m using MobaXterm) using public IP of the instance
Give the below commands to make the cpu utilization is high.
- yum install https://dl.fedoraproject.org/pub/epel/7Server/x86_64/Packages/e/epel-release-7-14.noarch.rpm -y — skip-broken
- yum install stress -y
- stress — CPU 80 — — → we are giving CPU load 80
❎Give the ‘top‘ command in the duplicate session and see the cpu load is getting high.
❎the instance got automatically stopped as it reach beyond 40%.
❎Also we will receive an email that CPU utilization threshold crossed.