Back to Blog
Lesson 29 of the AWS: AWS Core Services for Developers course
Cloud NativeAugust 5, 20264 min read

Setting Up CloudWatch Alarms: A Guide to Proactive Alerting

Stop waiting for users to report bugs. Learn to create CloudWatch Alarms and configure SNS notifications to monitor your API and Lambda health automatically.

AWSCloudWatchAlarmsSNSMonitoringServerless
Close-up of a vintage black and white alarm clock on a white background.

Previously in this course, we covered creating CloudWatch Dashboards to visualize your application's health. While dashboards are excellent for "at-a-glance" verification, they require you to watch them constantly. This lesson adds the missing piece: automated alerting. We will transform those passive metrics into proactive signals by setting up Alarms that notify you via SNS the moment your system shows signs of distress.

Understanding CloudWatch Alarms from First Principles

A CloudWatch Alarm watches a single metric over a specified time period. When that metric crosses a threshold you define, the alarm transitions to an ALARM state.

At its core, an alarm consists of three parts:

  1. The Metric: What you are tracking (e.g., Errors in Lambda, 4XXError in API Gateway).
  2. The Threshold: The limit that defines "bad" (e.g., more than 5 errors in 1 minute).
  3. The Action: What happens when the threshold is crossed (e.g., sending an email via SNS).

Configuring SNS for Alerting

Simple Notification Service (SNS) is the delivery mechanism for your alerts. Before creating an alarm, you must define a "Topic" where alerts are sent and a "Subscription" that tells AWS where to route those messages.

1. Create an SNS Topic

In the AWS Console, navigate to Simple Notification Service (SNS) and create a new Standard Topic. Name it app-error-alerts.

2. Create a Subscription

Once the topic is created, click Create subscription.

  • Protocol: Select "Email".
  • Endpoint: Enter your email address.
  • Finalize: Click Create. Check your inbox for a confirmation link from AWS and click it to verify your email.

Worked Example: Creating a Lambda Error Alarm

We will now create an alarm that notifies you if your Lambda function experiences more than one error in a 5-minute window.

  1. Navigate to CloudWatch: Go to the CloudWatch console and click Alarms > All alarms.
  2. Select Metric: Click Create alarm and then Select metric. Choose Lambda > By Function Name, and find your function's Errors metric.
  3. Define Conditions:
    • Statistic: Sum.
    • Period: 5 minutes.
    • Threshold type: Static.
    • Whenever Errors is...: Greater than 1.
  4. Configure Actions:
    • Under Alarm state trigger, select In alarm.
    • Select the SNS topic you created earlier (app-error-alerts).
  5. Name and Create: Give your alarm a descriptive name like Lambda-Error-Threshold-Exceeded and finish the setup.

Hands-on Exercise: Triggering Your Alarm

To confirm your configuration, you need to force an alarm state:

  1. Temporarily modify your Lambda code to throw an error intentionally (e.g., throw new Error('Test Alarm');).
  2. Trigger the function twice within a 5-minute window.
  3. Observe the CloudWatch Alarm console. It should switch from OK to IN ALARM.
  4. Verify that an email notification arrives in your inbox shortly after.
  5. Important: Remember to fix your code and delete the alarm or reset the state to avoid unnecessary noise!

Common Pitfalls

  • The "Silent" Alarm: Forgetting to confirm the SNS email subscription. If the status remains "Pending Confirmation," you won't receive alerts.
  • Threshold Sensitivity: Setting thresholds too low can lead to "alert fatigue"—being bombarded with emails for minor, transient blips that resolve themselves. Always set thresholds that indicate a genuine service degradation.
  • Missing Data: If your function hasn't run, the metric might show as "Missing." Configure your alarm to "Treat missing data as good" (not breaching) to avoid false positives during low traffic.

FAQ

Q: Can I send alerts to Slack instead of email? A: Yes. You can use an AWS Lambda function as a subscriber to an SNS topic, where the code formats the alert and sends it to a Slack Webhook URL.

Q: Should I alert on every single error? A: Generally, no. Most production systems have occasional transient errors. Alerting on a rate (e.g., > 1% of requests) or a count over time (e.g., > 5 errors in 5 minutes) is much more effective.

Recap

We’ve moved beyond dashboards by configuring proactive Alarms. By defining a metric, setting a threshold, and routing it through an SNS topic, you've ensured that your system will tell you when it needs attention, rather than the other way around.

Up next: Distributed Tracing with AWS X-Ray — where we'll learn how to follow a request as it travels through your entire serverless stack to pinpoint exactly where things go wrong.

Similar Posts