set a alarm to lambda function based on execution time - amazon-web-services

Is it possible to set an alarm when lambda functions take more than a specific time?
ex:- I want to set alarm if my lambda function takes more than 10 seconds to execute

AWS web console:
CloudWatch -> Alarm -> Create Alarm
Select metric:
Rely on the standard AWS lambda metric Duration:
Set the alarm condition
then set Notification etc. depends on your needs.
Set the alarm name and description.
As a result you will get a needed alarm. (Screen is with lower threshold (2000 ms))

Related

AWS CloudWatch Composite Alarms: Send Alert When 1 Alarm Has been "In Alarm" For Over a Certain Amount Of Time

So I'm trying to setup composite alarms on AWS. So far, I have most of it set up. At the moment, I have a composite alarm set up with 3 alarms. If any 2 of these 3 alarms trigger, then the composite alarm also triggers. This part works fine.
However, I am having trouble with part of my use case. I'd also like to make it so that if one of these alarms within the composite alarm stays in alarm for over a certain period of time, then an alert is also sent out.
Here's an example of the situation:
2 out of the 3 alarms turn on in any time period: Alert should be sent
1 out of the 3 alarms turn on for under a certain time period: Alert should not be sent
1 out of the 3 alarms turn on for over a certain time period: Alert should be sent
I've tried looking into the settings available on the alarms themselves, and there doesn't seem to be an option for what I'm trying to do.
I'm wondering if this would require a lambda function? Is it possible for a lambda function to keep track of how long an alarm has been in alarm?
As talked in the comment section above, I am providing you with a possible solution to your problem. The only blocker is that you can't have different time frame for the alarms, both should be the same.
So you will have (example)- Alarm 1(cpu) if for 15 min it's over 60%. Alarm 2(EFS connections) if for 15 min there are more than 10 connections.
Now the alarm will go off when both the statements are true. Also the alarm will go off when only Alarm 1 goes off.
This is how you are going to make this alarm.
As for testing, it depends on what type of alarms you are making. For example cpu and ram increment methods are widely available on stackoverflow.
Also with aws cli you can change state of an alarm. It's usually for a very small amount of time, maybe 10 seconds.
aws cloudwatch set-alarm-state --alarm-name "myalarm" --state-value ALARM --state-reason "testing purposes"
You need to find a method which can suite your needs better.

Is there a way to ignore cloudwatch alarms when they are triggered at night?

I have alarms in AWS Cloudwatch but at night I keep getting False positives due to low volumes. How can I set up an alarm so that it only triggers at certain times of the day? Or how do you suggest approaching this problem?
Using AWS CLI you can disable cloudwatch alaram using the following command:
aws cloudwatch disable-alarm-actions --alarm-names "alarm name"
And then enable it again using this command:
aws cloudwatch enable-alarm-actions --alarm-names "alarm name"
You scheduled this disable/enable using cronjob for example.
You can automate this by creating an EventBridge rule where you specify a cron or schedule expression that runs a lambda function.
Then, you can use your Lambda function to enable or disable an alarm (or even multiple alarms together) according to your desired schedules.
disable_alarm = client.disable_alarm_actions(AlarmNames=alarm_names)
Here's a good tutorial: https://medium.com/geekculture/terraform-structure-for-enabling-disabling-alarms-in-batches-5c4f165a8db7
Alternatively, I found that it is possible to create a metric based on a Math expression where I could say for example:
IF(Invocations > threshold, metric, 0)
And this will output 0 at night where the Invocations volume is less than the threshold.
Then I could create an alarm on top of this new metric.

Is it possible to determine a time for sending alerts on the cloudwatch aws?

Is it possible to determine a time for sending alerts on the cloudwatch aws?
For example:
I would like to configure the operation of alerts from Monday to Friday at 8 am to 6 pm
A CloudWatch Alarm will trigger whenever the rule is met. It might take a minute or so to react (depending upon the metric and the aggregate function used).
However, it is not possible to schedule when alerts operate.
If you wish, you could create an AWS Lambda function (triggered on a schedule) that will call disable_alarm_actions() and enable_alarm_actions(). The documentation says:
disable_alarm_actions(): Disables the actions for the specified alarms. When an alarm's actions are disabled, the alarm actions do not execute when the alarm state changes.

AWS Cloudwatch alarm state not updating

I have a cloudwatch alarm that is watching a somewhat sparse metrics (manually published at unpredictable intervals).
I didn't think this would be an issue if I used: Treat missing data as "ignore", but it looks like this is not working.
Basically I have a lambda function that is triggered at unpredictable intervals (might not get called for days, or get called 5 times an hour, etc. - it's triggered based on a human-controlled action).
This lambda function records a metric (ex. # of example metric). If the value is > 0, I want the alarm to go into ALARM. If the value is < 0, I want the alarm to go into OK.
But for some reason, not sure why, the alarm isn't clearing automatically whenever I record a 0 metric from the lambda function. And when I record the metric w/ value of 1, it should go into alarm, but often doesn't.
Am I misunderstanding how these sparce metrics work?

CloudWatch alarm for Amazon EC2 Service Instance Limits

Is it possible to set a CloudWatch alarm for when we are approaching the limit of EC2 instances currently allowed on our account?
For instance, if limit for EC2 instances is currently 250, when instance number 240 is provisioned, I want an alarm to trigger.
If you have an auto scaling group which launches new instances and you want to control it, you can use GroupInServiceInstances which gives you the number of instances running as part of the ASG. Read more here.
Yes, you could do this with a Lambda function, a CloudWatch Metric and a CloudWatch alarm.
Your alarm would be configured to alarm on the metric, if it exceeds some threshold (the threshold being your instance limit).
Your Lambda function, would run on a schedule e.g. every 5 mins, and would do the following:
Use the ec2:DescribeAccountAttributes API to get the account instance limit and cloudwatch:DescribeAlarms to get the current threshold of the alarm. If they differ, the alarm threshold should be updated the the instance limit via the cloudwatch:PutMetricAlarm API.
Use the ec2:DescribeInstances API and count the number of instances that are running and publish the value to a custom CloudWatch metric with the cloudwatch:PutMetricData API.
If the value published to the metric exceeds the threshold of the alarm, it will fire. The lambda function will keep the alarm threshold configured to the limit of instances and will publish datapoints to the metric based on the number of instances currently running.