Set up CloudWatch alarms and SNS topics in AWS

Set up CloudWatch alarms and SNS topics in AWS

·

5 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

Amazon CloudWatch is a monitoring and observability service provided by Amazon Web Services (AWS). It allows you to collect and track metrics, collect and monitor log files, and set alarms on the metrics and logs.

Here's the link to the official documentation of Amazon CloudWatch.

How Amazon CloudWatch works?

  1. Collect data: CloudWatch collects data from a variety of sources, including metrics, logs, and events.

  2. Store data: CloudWatch stores the data it collects in a time series database.

  3. Analyze data: CloudWatch provides a variety of tools and features that you can use to analyze your data, including dashboards, alarms, and Metrics Insights.

  4. Visualize data: CloudWatch allows you to visualize your data using a variety of charts and graphs.

  5. Take action: CloudWatch can help you to take action to resolve problems by sending notifications, triggering alerts, and automating remediation workflows.

Concepts of Amazon 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.

  • 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.

  • 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.

  • Resolution: Each metric is one of the following:

    • The standard resolution, with data having a one-minute granularity

    • High resolution, with data at a granularity of one second

  • 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

  • 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.

  • 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.

Amazon SNS

Amazon SNS (Simple Notification Service) is a fully managed messaging service provided by Amazon Web Services (AWS).

It enables you to send messages or notifications to various endpoints, such as email, SMS, mobile push notifications, or even HTTP endpoints, in a flexible and scalable manner.


            A publisher sends a message to an Amazon SNS topic, and subscribers receive the
                message.

Tasks

Task 1: 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.

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

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.

Now let us go ahead and create the CloudWatch alarm.

Go to AWS Management Console > Search & Navigate to Cloudwatch Service > In the left navigation pane, click on Alarms > Click on Create Alarm.

Step 1: Select metric

Select Metric > Search "Billing" > Select Total Estimated Charge

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

Under Conditions > Select Threshold type as Static > Select Whenever EstimatedCharges is... as Greater > Give 2 USD for than… > Click on Next.

Step 2: 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.

Click on Next.

Step 3: Add name and description

For the Alarm name give MyBillingAlarm > Click on Next.

Step 4: Preview and create

Preview the Alarm you have created and click on Create Alarm.

Confirm the subscription to receive the Amazon SNS.

And yay! We have successfully set up the CloudWatch Service.

Task 2: Delete the billing Alarm that you created now.

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 that the Alarm was deleted successfully.


In this blog, I have discussed how to set up CloudWatch alarms and SNS topics in AWS. If you have any questions or want to share your experiences, please comment below. Don't forget to read my blogs and connect with me on LinkedIn and let's have a conversation.

To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.

#Day46 #90daysofdevops

Did you find this article valuable?

Support Sneha K S by becoming a sponsor. Any amount is appreciated!