How to monitor your EC2 instance status in AWS by using CloudWatch Alarm!

How to monitor your EC2 instance status in AWS by using CloudWatch Alarm!

Introduction

In cloud environments, security, monitoring, and seamless integration between services are crucial.
AWS provides various tools to achieve these goals, including CloudTrail for auditing, CloudWatch for monitoring, SNS (Simple Notification Service) for messaging, and SQS (Simple Queue Service) for decoupled communication.

  • This blog deals with the following:
  • Enabling CloudTrail to track EC2 instance start/stop events.
  • Using CloudWatch to monitor events.
  • Setting up an SNS topic, subscribing to it, and sending messages.

Getting Started with!

Enabling CloudTrail to Monitor EC2 Events

AWS CloudTrail provides a history of API calls made within an AWS account, helping track changes and security-related events.
Screenshot 2025-02-20 at 3.43.32 PM

Steps to Enable CloudTrail:

  • Navigate to the AWS CloudTrail console.
    Screenshot 2025-02-17 at 3.26.32 PM

  • Click on Create Trail and provide a name (e.g., EC2-Monitoring-Trail).

  • Select Apply trail to all regions for comprehensive tracking.

  • Choose an S3 bucket to store logs or create a new one.
    Screenshot 2025-02-17 at 3.34.32 PM

  • Enable CloudWatch Logs to send logs for monitoring.

  • Click Create Trail.

Monitoring an EC2 Start/Stop Event:

  • Start or stop an EC2 instance from the EC2 console.
  • Go to CloudTrail > Event History and filter by Event Name: StartInstances or StopInstances.
  • Verify that the event is logged with details like user, time, and instance ID.
    Screenshot 2025-02-17 at 4.50.43 PM

Creating an SNS Topic and Sending Notifications

AWS SNS (Simple Notification Service) allows sending notifications to multiple subscribers.
Screenshot 2025-02-20 at 3.45.17 PM

Steps to Create an SNS Topic and Subscribe:

  • Navigate to AWS SNS.
    Screenshot 2025-02-17 at 5.34.22 PM

  • Click Create Topic and choose Standard type.
    Screenshot 2025-02-17 at 5.35.09 PM

  • Provide a name (e.g., DemoTopic) and create the topic.

Click Create Subscription, select:

  • Protocol: Email or SMS.
  • Endpoint: Your email or phone number.
  • Confirm the subscription via the email/SMS received.
    Screenshot 2025-02-17 at 5.41.50 PM

Sending a Message to SNS:

  • Open the SNS topic and click Publish Message.
  • Enter a subject and message body.
  • Click Publish Message and verify the notification.
    Screenshot 2025-02-17 at 5.45.12 PM

Receiving the mail:

  • Once the message is published to the SNS topic, all subscribed endpoints (e.g., email, SMS) will receive the notification.
  • Check your email inbox (or spam folder if not found).
  • You should see an email with the subject and message body that was entered while publishing the SNS message.
    Screenshot 2025-02-20 at 7.52.05 PM

Note: If you configured SNS with CloudWatch Alarms, you would receive an email automatically when the monitored event occurs (e.g., EC2 instance state change). Check the above CloudWatch Alarms.

Setting Up CloudWatch for Monitoring

AWS CloudWatch helps monitor AWS resources and applications in real time.
Screenshot 2025-02-20 at 3.45.00 PM

Steps to Monitor EC2 Events with CloudWatch:

  • Navigate to AWS CloudWatch.
  • Go to Logs > Log Groups and check if CloudTrail logs are available.
    Screenshot 2025-02-17 at 5.03.54 PM

Create a CloudWatch Alarm:

  • Click Alarms > Create Alarm.
    Screenshot 2025-02-17 at 5.08.36 PM

  • Select EC2 Instance State Change as the metric.
    Screenshot 2025-02-17 at 5.10.33 PM

  • Set a threshold (e.g., NetworkIn).
    Screenshot 2025-02-17 at 5.13.22 PM

  • Choose an SNS topic for notifications.
    Screenshot 2025-02-17 at 5.17.31 PM

  • Click Create Alarm.
    Screenshot 2025-02-17 at 5.20.51 PM

Getting Notified:

  • Once the CloudWatch alarm is triggered, AWS SNS sends a notification to the subscribed email address.
  • The email contains details about the alarm, such as its name, the reason it was triggered, and a link to view it in the AWS Management Console.
    Screenshot 2025-02-21 at 9.38.09 AM

Conclusion

By following these steps, we successfully:

  • Enabled CloudTrail to audit EC2 events.
  • Configured CloudWatch to monitor instance state changes.
  • Created an SNS topic to send notifications.
    These AWS services enhance security, monitoring, and integration in cloud environments. Implementing them ensures better visibility, automated alerts, and efficient communication between applications.

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.