Day 46 - CloudWatch alarms and SNS in AWS

Smriti SharmaSmriti Sharma
11 min read

What if you get an AWS bill for 100$ tomorrow morning once you wake up? Wouldn’t that be horrifying? So we need to monitor our usage and set up an alarm that will inform us whenever the bill touches a threshold. In this blog, we will learn how to set up CloudWatch alarms and SNS topics in AWS.

Amazon CloudWatch

Using Cloudwatch Alarms Monitor AWS Resources | by ABHISHEK KUMAR | Medium

What is Amazon CloudWatch?

Amazon CloudWatch is an AWS monitoring and management service which is designed for the purpose of maintaining the services and resources which are used. Particularly, this is designed for developers, site reliability engineers, IT managers, and system operators to make their life easier.

But how does it help in AWS monitoring and managing our AWS services? It collects and displays the stats data, metrics, and insights on particular AWS services and your applications which lets you manage precisely what you want to manage. The collected data is in the form of logs, metrics, and events for each AWS service and resource.

Using CloudWatch you can create automated actions, troubleshooting is much easier, and optimize your applications by removing the factor increasing your application’s latency.

Features of Amazon CloudWatch

The features of Amazon CloudWatch offer 2 major services: -

  1. AWS CloudWatch Logging Service: Includes storing, monitoring, viewing, capturing, and managing service and application logs.

  2. AWS CloudWatch Metrics Services: CloudWatch offers metrics services that include capturing and managing the performance of the resource and operational metrics.

  3. Combined features of both Logging and Metric services by CloudWatch: -

    • CloudWatch Logs insights: An interactive tool for log query that helps in visualizing and analyzing log data. Whether it is about creating time-series graphs to visualize the data or publishing to CloudWatch dashboard, the log insights tool assists in filtering and aggregating log data.

    • CloudWatch Logs: With the help of this feature, users can collect and store logs for various AWS services like AWS CloudTrail, AWS API Gateway, etc, or for specific applications and on-premise resources.

    • CloudWatch Alarms: The CloudWatch Alarm constantly keeps on monitoring the CloudWatch metrics. It raises an alert if the metric (or metrics) exceeds the set/specified thresholds. The alarm’s status can also be displayed on the CloudWatch dashboard.

    • Auto Scaling: AWS auto scaling of CloudWatch is responsible for the automation of capacity and resource planning.

    • AWS Integration: CloudWatch is known to facilitate integration with AWS Identity and Access management. These integration features give a management console, ownership to manage and control access to CloudWatch data and resources.

    • CloudWatch Events: It picks for the state changes to the AWS resources and is responsible for creating a near to real-time stream of events routed to targets for processing. Eventually, the targets are notified of the state changes with the help of these events.

How Amazon CloudWatch Works?

AWS Certified Developer - Associate Guide - Second Edition

CloudWatch performs 4 actions normally, first it collects log and metric data, then monitors the applications, then Acts according to the instructions, finally analyzes the collected log and metric data for further usage.

You can mention the service in CloudWatch and it starts collecting it’s raw log and metrics data. Even while collection it keeps on monitoring and updates the data. While AWS monitoring, if any issues are found, it will act and try to troubleshoot it. With all the collected log and metric data, it will analyze and produce visualized content for better understanding and further usage.

AWS CloudWatch Use Cases

Organizations are effectively utilizing the monitoring and events features of AWS CloudWatch to maintain the health of the AWS environment. AWS CloudWatch has the following uses:

  • It finds its usage in deriving metrics and deploying applications

  • Facilitates monitoring of resource utilization, application performance, and overall infrastructure health

  • Used as a logging tool and also as a dashboard

  • Also used for organizing the metrics across all the deployed applications on AWS.

Concept of AWS CloudWatch

Here are some concepts related to Amazon CloudWatch:

  • Namespaces: A namespace is a container for CloudWatch metrics. Metrics in different namespaces are isolated from each other so that metrics from different applications are not mistakenly aggregated into the same statistics.

    There is no default namespace. The AWS namespaces typically use the following naming convention: AWS/service. For example, Amazon EC2 uses the AWS/EC2 namespace.

    For the list of AWS namespaces, see AWS services that publish CloudWatch metrics.
    For know more about Namespaces. Refer this link: Namespaces

  • Metrics: Metrics are fundamental to CloudWatch. They represent numerical data points collected at a specific time and are used to monitor the behavior and performance of AWS resources, applications, and custom services.

    Examples of metrics include CPU utilization, network traffic, or request latency. CloudWatch provides a wide range of pre-defined metrics for AWS services, and you can also create custom metrics.
    For know more about Metrics. Refer this link: Metrics

  • Dimensions: A dimension is a name/value pair that is part of the identity of a metric. You can assign up to 30 dimensions to a metric.
    For know more about Dimensions. Refer this link: Dimensions

  • Resolution: Each metric is one of the following: -

    1. The standard resolution, with data having a one-minute granularity
  1. High resolution, with data at a granularity of one second

For know more about Resolution. Refer this link: Resolution

  • Statistics: Statistics are metric data aggregations over specified periods. CloudWatch provides statistics based on the metric data points provided by your custom data or provided by other AWS services to CloudWatch.
    For know more about Statistics. Refer this link: Statistics

  • Percentiles: A percentile indicates the relative standing of a value in a dataset. For example, the 95th percentile means that 95 percent of the data is lower than this value and 5 percent of the data is higher than this value. Percentiles help you get a better understanding of the distribution of your metric data.
    For know more about Percentiles. Refer this link: Percentiles

  • Alarms: Alarms allow you to set thresholds on metrics and define actions to be triggered when those thresholds are breached. When an alarm enters the ALARM state, it can initiate actions like sending notifications, scaling AWS resources, or executing an AWS Lambda function. Alarms help you proactively respond to incidents or take automated actions based on specific conditions.
    For know more about Alarms. Refer this link: CloudWatch Alarms

Amazon SNS

AWS SNS

What is Amazon SNS?

Amazon Simple Notification Service(SNS) is a notification service provided as part of Amazon Web Services since 2010. It provides a service for sending messages. Amazon SNS acts as a single message bus that can message to a variety of devices and platforms.

Amazon SNS supports multiple endpoint types. For example, you can group together IOS, Android and SMS recipients. Once you publish the message to the topic, SNS delivers the formatted copies of your message to the subscribers.

To prevent the loss of data, all messages published to SNS are stored redundantly across multiple availability zones.

Features of the Amazon SNS

These are some of the Features of SNS: -

  1. Auto-scaling: As the volume of messages grows, it scales accordingly.

  2. Message Encryption: Protects your messages using encrypted themes from unwanted and unknown access. When messages are transmitted to subscriber endpoints, they are decrypted.

  3. Message filtering: This allows the subscriber to change the filtering policy so that he only receives relevant notifications.

  4. When a message is sent to a topic, it is replicated and sent to multiple endpoints, called message branching. Asynchronous event notification is provided by Fanout, allowing for parallel processing.

  5. Mobile Notification: User-initiated in-app actions or cloud-based business logic can be triggered. Distributing mobile push notifications for iOS, Android, Fire OS, Windows, and Baidu-based devices are cheap.

  6. SNS and Email Messaging: Amazon SNS can send text and email messages (SMTP).

Benefits of using SNS

‍These are the main benefits of using SNS in Serverless applications: -

  1. Scalability: SNS topics scale up to any number of publishers, subscribers and messages without your needing to do any infrastructure work. This is helpful for growing applications where the teams would rather not have to provision additional infrastructure as their usage of pub/sub model increases over time.

  2. Ease of setup: SNS is a fully managed service; setting it up requires zero infrastructure work. Initial ramp-up is also easy as SNS provides an HTTP API that conforms to API standards. With plenty of subscriber types supported out of the box, topic subscribers are unlikely to require any further setup either. SNS’s ease of setup means you will have a faster path to a proof-of-concept application or to a fully implemented solution.

  3. Multiple notification formats supported: SNS supports AWS Lambda and AWS SQS notifications as well as mobile push notifications, HTTP(S) endpoints, email addresses, and SMS messages. These formats cover many common use cases, so you won’t have to write custom code to implement a subscriber for your SNS topic.

  4. Integration with AWS Lambda: The native integration with AWS Lambda allows you to run a Lambda function every time a message is published to an SNS topic. This allows for many useful workflows that process data from SNS messages, such as reacting to notifications from other systems or preparing the data for storage in S3 or Redshift.

How does SNS work?

Amazon SNS

The fundamental idea of a publish/subscribe system is to allow communication between systems that are not directly connected by having one side publish messages to a shared location, called a "topic" in SNS.

‍Every SNS topic has a set of subscriptions. Once a message is published to a topic, SNS handles distributing the message to all its subscribers. The subscribers can be AWS Lambda functions and SQS queues, mobile push notifications (including iOS, macOS, Android, and Windows devices), HTTP(S) endpoints, email addresses and mobile phone numbers capable of receiving SMS messages.

To publish a message to an SNS topic, a message producer must use the SNS HTTP API. Once the message is published, all subscribers receive a copy of the message over the channel through which they established their subscription.

Each subscriber can receive messages from multiple topics and apply filters to topics in order to receive only the most relevant messages. If a message can’t be delivered to a subscriber right away, SNS will retry its delivery a number of times with varying intervals between attempts. SNS runs in multiple availability zones in AWS, guaranteeing that all messages are sent and received correctly even if one of AWS’ datacenters is experiencing downtime.

Task: Create a CloudWatch alarm that monitors your billing and sends an email to you when it reaches $2.

First, we will need to enable the Billing Preferences.

Let's dive into the Steps: -

Step 1: Go to AWS Management Console > Search & Navigate to Billing and Cost Management Service > In the left navigation pane, under Preferences click on Billing Preferences.

Step 2: Under Invoice delivery preferences > Click on edit > Checkbox for PDF invoices delivered by email > Click on Update > Under Alert preferences > Checkbox for Receive AWS Free Tier alerts and Receive CloudWatch billing alerts.

Step 3: Now, Go to AWS Management Console > Search & Navigate to CloudWatch Service. You will see the below page.

Step 4: In the left navigation pane, click on Alarms > Click on Create Alarm.

Step 5: In Select metric, Select Metric > Search “Billing” > Select Total Estimated Charge.

Step 6: Go to Graphed Metrics > Set Statistic as Maximum > For Period choose 6 hours.

Step 7: Under Conditions > Select Threshold type as Static > Select Whenever Estimated Charges is… as Greater > Give 2 USD for than… > Click on Next.

Step 8: In Configure actions, Under Notifications: For the Alarm state trigger select In alarm > Create New Topic > Create a topic with your mail ID and topic name.

Step 9: Click on Next.

Step 10: In Add name and description, For the Alarm name give MyBillingAlarm > Click on Next.

Step 11: Preview and create.
Preview the Alarm you have created and click on Create Alarm.

Step 12: Confirm the subscription from your given email to receive the Amazon SNS.

You have successfully set up the CloudWatch Service.

Step 13: Now, Delete the billing Alarm that you created now.

Step 14: Go to AWS Management Console > Navigate to the CloudWatch service > In the left navigation pane, click on “Alarms” > Select the Billing Alarm you want to delete > Click on Actions > And choose Delete.

You can see now that the Alarm has been deleted successfully.

Conclusion

In Conclusion, Amazon CloudWatch provides comprehensive monitoring services, allowing users to collect and track metrics, collect and monitor log files, and set alarms. It enables the visualization of performance metrics and logs, aiding in the identification of issues and optimization of resource utilization. CloudWatch is an essential tool for maintaining the health and performance of AWS resources.

Amazon SNS (Simple Notification Service) is a fully managed messaging service that enables the distribution of messages or notifications to a distributed set of subscribers. It supports a variety of endpoints, including email, SMS, HTTP, and more. SNS facilitates real-time communication, alerting, and notification within and outside the AWS environment. It is instrumental in building scalable and decoupled architectures.

In this blog, I have discussed how to set up CloudWatch alarms and SNS topics in AWS.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

👆The information presented above is based on my interpretation. Suggestions are always welcome.😊

~Smriti Sharma✌

0
Subscribe to my newsletter

Read articles from Smriti Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Smriti Sharma
Smriti Sharma