Set a cloudwatch alarm for 2 days

1

Hi team, I am trying to setup an alarm over two days, I am getting this error Metrics cannot be checked across more than a day (EvaluationPeriods * Period must be <= 86400)

There is no way around this, if you adust the period to be 6 hours and evaluation to be 5 it exceeds the threshold, and vise versa.

Any alternative or approaches?

  • This would be a great feature, also have a similar requirement and getting a lot of notifications due to not being able to have a longer evaluation period or datapoints to alarm on

asked a year ago2149 views
3 Answers
0

Hi, indeed CloudWatch alarms don't allow more than 1 day evaluation periods at this time. Can you please elaborate on the use case? Depending on what kind of check you are trying to set, it would help contributors of repost to understand the use case in order to suggest alternatives.

profile pictureAWS
Jsc
answered a year ago
  • I am expecting some files to be dropped into s3 bucket, want to alarm if the files didn't make it to s3 more than 36 hours.

  • Can you please elaborate on the use case

    For instance: I want the system to alarm on missed daily activities, like pushing documents to S3 or checking SSL certificates or missed daily backups -- literally anything that I don't want to run more than once in a day. I may have 100 such metrics and alarms and I don't want them to be too noisy and would like to be able to set N misses out of K, like 2 or more misses out of 3. Proposed workaround with Lambda would work, of course, but it effectively bypasses CW and is not as visual as CW alarms. I think CW can do better and easier for customers

0

Unfortunately, you can't directly set up an alarm that checks metrics across more than one day.

but there are some workarounds you can use

Use AWS Lambda and Amazon SNS: You can create a scheduled AWS Lambda function that queries your metric data with a larger time window, then evaluates the condition you want to check, and sends a notification using Amazon SNS if the condition is met. This way, you can bypass the limitations of CloudWatch Alarms and create your own alarm mechanism with more flexibility.

import boto3
import datetime

def lambda_handler(event, context):
    cloudwatch = boto3.client('cloudwatch')
    sns = boto3.client('sns')
    
    metric_name = 'YourMetricName'
    namespace = 'YourNamespace'
    start_time = datetime.datetime.utcnow() - datetime.timedelta(days=2)
    end_time = datetime.datetime.utcnow()
    period = 21600  # 6 hours
    statistic = 'SampleStatistic'  # e.g., 'Average', 'Sum', 'Minimum', 'Maximum'
    threshold = 100  # Set your threshold value
    sns_topic_arn = 'arn:aws:sns:REGION:ACCOUNT_ID:TopicName'

    response = cloudwatch.get_metric_data(
        MetricDataQueries=[
            {
                'Id': 'm1',
                'MetricStat': {
                    'Metric': {
                        'Namespace': namespace,
                        'MetricName': metric_name,
                    },
                    'Period': period,
                    'Stat': statistic
                },
                'ReturnData': True
            }
        ],
        StartTime=start_time,
        EndTime=end_time
    )
    
    metric_data_points = response['MetricDataResults'][0]['Values']

    # Custom alarm condition
    alarm_triggered = any(value > threshold for value in metric_data_points)

    if alarm_triggered:
        sns.publish(
            TopicArn=sns_topic_arn,
            Message=f'Alarm triggered: {metric_name} exceeded the threshold of {threshold} during the last 2 days.',
            Subject='Custom Alarm Notification'
        )

or you can create custom metrics that aggregate your data into larger time intervals

profile picture
EXPERT
answered a year ago
  • Lambda would work in trivial cases. Now imagine I have 100 metrics and 40 alarms that I'm managing via CDK/CloudFormation and want to add 100 infrequent metrics and 40 more alarms. Each with individual condition. You can imagine what code would be there. It won't be 1-pager. Managing such code to add/adjust/remove alarms won't be easy. Managing alarms in two places -- in this lambda and in CW is a call for trouble. Managing IAM perms for lambda and make sure it works. It's painful even if I don't have to change anything because lambda runtime is going to be deprecated. Why not in one place?

0

Thanks for the updates and requests.

This feature is not available, but I'll add your testimonies to our feature requests tracking system.

Just sharing a possible workaround that you can use in the meantime: since we released multi data source querying last December, you could implement a custom connector that retrieves a longer period of data than 1 day and computes the aggregated value that you need to alarm on. This is not the solution of course, I'm just trying to share an idea for a temporary workaround in case that may help some of you.

profile pictureAWS
Jsc
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions