AWS RDS cloudwatch alarms - amazon-web-services

I am trying to create an alarm for "FreeStorageSpace" metric and units it takes is Bytes. What, however, I am trying to do is create an alarm using percentages (send an alarm is FreeStorageSpace < 10%) and not hardcode values. How can I create a CFT for that and reuse it for instances having different storage values.
Any help is appreciated.
Thanks

You can not use % value while creating the FreeStorageSpace alarm in CloudWatch. Also, the alarms in AWS are on per instance basis. You can not create a generic alarm for multiple instances. Hence, you may use numeric value for defining the alarm.

Related

Is there a simple way to monitor when a backup fails twice in cloudformation?

I am trying to send an SNS notification when a backup in the backup vault fails twice consecutively. Is there a CloudWatch alarm or any other way to do this in CloudFormation?
You can use CloudWatch metrics for this purpose and then setup alarms based on the thresholds that you need.
You can find the list of Metrics that are emitted to CloudWatch in this document: https://docs.aws.amazon.com/aws-backup/latest/devguide/cloudwatch.html
For instance you can setup an alarm on NumberOfBackupJobsFailed metric.

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.

Regular expressions for CloudWatch alarms

I have microservice that sends some custom metrics to AWS CloudWatch. Metric name consists of package name and some other data. For example gauge.com.example.test.time and gauge.com.example.test2.time and so on
Now I need to create some alarms based on this metrics. Is it possible to specify some reqular expression in metric name field when you create CloudWatch alarm instead of manual creation of separate alarm for each metric?
I tried such things: gauge.com.example..time gauge.com.example.*.time gauge.com.example.(\w).time and many other things but without success.
It is now possible to create aws alarms based on a Metric Math Expression.
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create-alarm-on-metric-math-expression.html
Not possible. From Creating Amazon CloudWatch Alarms
You can create a CloudWatch alarm that watches a single metric.

Stopping EC2 instance when custom cloudwatch metric passes limit

I'm trying to find a way to make an Amazon EC2 instance stop automatically when a certain custom metric on CloudWatch passes a limit. So far if I've understood correctly based on these articles:
Discussion Forum: Custom Metric EC2 Action
CloudWatch Documentation: Create Alarms to Stop, Terminate, Reboot, or Recover an Instance
This will only work if the metric is defined as follows:
Tied to certain instance
With type of System/Linux
However in my case I have a custom metric that is actually not instance-related but "global" and if a certain limit is passed, I would need to stop all instances, no matter from which instance the limiting log is received.
Does anybody know if there is way to make this work? What I'd need is some way to make CloudWatch work like this:
If arbitrary custom metric value passes a certain limit -> stop defined instances not tied to the metric itself.
The main problem is that the EC2 option is greyed out as the metric is not tied to certain EC2 instance and I'm not sure if there's any way to do this without actually making the metric itself certain instance related.
Have the custom CloudWatch metric post alerts to an SNS topic.
Have the SNS topic trigger a Lambda function that shuts down your EC2 instances via a call to the AWS API.

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.