CloudWatch Alarms
In this section you create an alarm on PythonAppErrorCount. When the application writes more than 5 errors in one minute, the alarm goes to In alarm and sends an email through Amazon SNS.
Create the alarm
- In the CloudWatch navigation pane choose Alarms, All alarms, then Create alarm (1).

- Keep Metrics and Classic, then choose Select metric (1).

- Choose the custom namespace FCJ/CloudWatchWorkshop (1), then Metrics with no dimensions.

- Select PythonAppErrorCount (1) and choose Select metric (2).

- Change Statistic to Sum (1) and Period to 1 minute (2). The graph now shows the number of errors per minute.

- Under Conditions keep Static and Greater (1), enter
5 as the threshold (2) and choose Next (3). The red dashed line in the preview is the threshold.

- Under Notification, keep In alarm and select Create new topic (1). Enter the topic name
FCJ-CloudWatch-Alarm-Topic (2), your email address (3), and choose Create topic (4).

- The new topic is selected (1) with your email as an endpoint (2). Scroll down and choose Next.

- Enter Alarm name
PythonAppErrorAlarm (1) and a description, for example More than 5 ERROR lines from python-app in 1 minute (2). Choose Next.

- Review the alarm, check the notification (1) and choose Create alarm (2).

- The alarm is created (1). Its state is Insufficient data until it has enough data points, and a banner reminds you that the SNS subscription is pending confirmation.

Confirm the email subscription
- Open the email AWS Notification - Subscription Confirmation from
no-reply@sns.amazonaws.com and choose Confirm subscription (1).

- On the page that opens choose Confirm Subscription (1).

- The subscription is confirmed (1). SNS only sends notifications to confirmed subscriptions.

Trigger the alarm
logger.py usually writes 2 to 5 errors per minute, so the alarm stays OK most of the time. Simulate an outage: open a Session Manager session to Instance A again and write 30 error lines at once (1):
for i in $(seq 1 30); do logger -t python-app "ERROR GET /api/orders failed: simulated outage $i"; done; echo done

- Wait 1 to 3 minutes and open the alarm. The graph shows the spike above the threshold (1) and the state timeline turns red for In alarm (2). After the next minute the error count drops again and the alarm returns to OK.

- Check your mailbox. SNS sent the email ALARM: “PythonAppErrorAlarm” in … (1). It contains the reason, the value that crossed the threshold (2) and the state change
OK -> ALARM (3).
