How to do it...

Let's create an alarm and simulate conditions that will trigger it:

  1. We can create an alarm for a metric with the name FailedLogins and namespace ServerlessProgrammingCookbook as follows:
aws cloudwatch put-metric-alarm 
--alarm-name FailedRequestsAlarmWithDimensions
--alarm-description 'Alarm for failed login requests'
--metric-name 'FailedLogins'
--namespace 'ServerlessProgrammingCookbook'
--statistic 'Average'
--period 60
--threshold 5
--comparison-operator GreaterThanOrEqualToThreshold
--evaluation-periods 1
--dimensions Name=Device,Value=Laptop
--alarm-actions arn:aws:sns:us-east-1:<account id>:my-first-sns-topic
--region us-east-1
--profile admin
  1. Check the current status of the alarm using the describe-alarms command as follows:
aws cloudwatch describe-alarms 
--alarm-names FailedRequestsAlarm
--region us-east-1
--profile admin

If we try the describe-alarms command immediately after the alarm's creation or if we have not sent any data for the metric within the specified period (60 seconds, in this case), we get the state INSUFFICIENT_DATA within the response.

Send the data with the dimension using the following code: 

aws cloudwatch put-metric-data 
--namespace 'ServerlessProgrammingCookbook'
--metric-name 'FailedLogins'
--value 1
--dimensions Device=Laptop
--region us-east-1
--profile admin

If we check with describe-alarms after some time, we can see that the state has changed to OK. We can now post data with a higher value (for example, 10) so that the average is more than the threshold, as shown in the following screenshot. Based on the interval we take to send data, and based on when the average is calculated, we may get a 10 or 0.5 average:

We will also receive a mail notification similar to the one we received in the previous recipe, Setting up CloudWatch alarms (AWS CLI).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.33.235