North-Seattle-College / ad440-winter2021-thursday-repo

NSC AD 440 Winter 2021 Thursday cohort practicum repo
Apache License 2.0
2 stars 27 forks source link

Create alerts for error 500 generated by /users/{user_id}/tasks/{task_id} API in AWS using CloudWatch #260

Closed kevinles117 closed 3 years ago

kevinles117 commented 3 years ago

Creating alerts for error 500 in AWS using CloudWatch. If there is more than 10 errors 500 within 5 minutes, send an alert.

kevinles117 commented 3 years ago
Date Activity Time Spent
3/10/2021 Setting up issue 30 mins
3/11/2021 Researching 2 hours
3/15/2021 Reading about CloudWatch 2 hours
3/16/2021 Researching about alerts 1 hour
3/18/2021 Testing 2 hours
3/19/2021 Trying to fix keyerror 2 hours
3/20/2021 Rewatched lecture and got error email 2 hours
3/20/2021 Total time spent 11.5 hours

Steps to Create an Alert

  1. Create SimpleNotificationService topic for the email notification image
  2. Make sure to set it up as a Standard type
  3. After creating the topic, make sure you create a subscription and use an Email protocol and provide the email you would like notifications sent to. image

Creating an Error 500 on the function

  1. Navigate to the lambda function page and click on the TaskId function
  2. Scroll down to line 82 and the comment out the name of the database and replace it with "name" or a generic string. The function will not be able to connect to the database and this will send an Error 500 to the log. image

Configure the Alarm

To create a metric filter using the CloudWatch console

  1. Open the CloudWatch dashboard

  2. In the navigation pane, choose Log groups.

  3. Scroll down to the lambda function TaskId log group.

  4. Click on metric filters and create a metric filter.

  5. Enter "Error 500" for the filter pattern.

  6. Select the latest log data for testing the filter and click test pattern to make sure you had Error 500's in the log.

  7. Click Next, and enter a name for the filter as well as the Metric namespace and Metric value (I chose 1.0 for the metric value)

  8. Once the metric filter is created, you can click the box in the upper right corner of the metric filter to create an alarm. image

  9. Create the alarm

  10. The alarm should have a statistic of sum and time period of 5 minutes. The threshold should be Static and Greater/Equal to 10 image

  11. Click Next and select the SNS topic you created.

  12. Click Next and add the name of your alarm and click next and confirm the details of the alarm. Create the alarm.

  13. Go back to the lambda function and test it with this event: { "body": "value1", "requestContext": { "http": { "method": "GET" } }, "pathParameters": { "userId": "1", "taskId": "1" } }

  14. This should be the email you get: image