Is it possible to create a CloudWatch alarm to send an SNS when an EC2 is running over 2hr? Currently I have this (in yaml):
F1RunningAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: F1LongRunning
AlarmDescription: Alarms when the F1 instance is running for a long time
AlarmActions:
- !Ref ‘F1SNSTopic’
MetricName: CPUUtilization
Namespace: AWS/EC2
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 7200
Statistic: Average
Threshold: 0
TreatMissingData: notBreaching
Dimensions:
- Name: “InstanceId”
Value:
!Ref EC2
Currently it Alarms immediately as the CPU utilization passes 0 in the 2 hour period. I’d like it to alarm after the CPU has been running for more than 2 hour. Is this possible?
Rather than use the period, you can use the EvaluationPeriods for this. Currently, the value needs to be breaching for one period, however, changing the period to 60 (1 minute metrics) and changing the EvaluationPeriods to 120 should get you what you want.
Run that in cron every minute. Make sure to attach appropriate cloudwatch:PutMetricData IAM policy to instance profile role.
Your metric is already an incremental integer value, so just trigger the alarm based on return value over 7200 (seconds) without the need to take seconds eval period interval into consideration or any other hacks AWS/EC2 metrics does not support.