Skip to content

Commit 604dc58

Browse files
author
João Taveira Araújo
authored
fix(cloudwatch_metrics): revert to cron expression (#64)
PR #62 fixed our schedule expression to fire at the correct cadence, but subtly changed the time at which the rule would trigger. The use of the `rate` function ensures a rule will trigger at regular intervals from rule creation. Given the nature of the data we are collecting, we want rules to trigger aligned to fixed time boundaries (e.g. every minute, every hour). This commit reintroduces a correct cron expression.
1 parent 168d9ca commit 604dc58

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

modules/cloudwatch_metrics/main.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,20 @@ resource "aws_iam_role_policy_attachment" "this" {
3636
policy_arn = aws_iam_policy.this.arn
3737
}
3838

39+
40+
41+
locals {
42+
cron_expression = coalesce(
43+
var.interval >= 60 && var.interval < 60 * 60 ? "*/${var.interval / 60} * * * ? *" : "", # minutely
44+
var.interval >= 60 * 60 && var.interval < 24 * 60 * 60 ? "0 */${var.interval / 60 / 60} * * ? *" : "", # hourly
45+
var.interval >= 24 * 60 * 60 ? "0 0 */${var.interval / 24 / 60 / 60} * ? *" : "", # daily
46+
)
47+
}
48+
3949
resource "aws_cloudwatch_event_rule" "trigger" {
4050
name_prefix = var.eventbridge_name_prefix
4151
description = "Periodically trigger Observe Lambda to collect CloudWatch metrics"
42-
schedule_expression = var.interval == 60 ? "rate(1 minute)" : "rate(${var.interval / 60} minutes)"
52+
schedule_expression = "cron(${local.cron_expression})"
4353
event_bus_name = var.eventbridge_schedule_event_bus_name
4454
}
4555

modules/cloudwatch_metrics/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ variable "interval" {
3535
nullable = false
3636
default = 300
3737
validation {
38-
condition = var.interval >= 60 && var.interval <= 10800
39-
error_message = "interval must be in [60, 10800] (1 minute to 3 hours)"
38+
condition = (var.interval >= 60 && var.interval <= 60 * 60) || (var.interval >= 60 * 60 && var.interval <= 24 * 60 * 60 && var.interval % (60 * 60) == 0)
39+
error_message = "interval must be minutely, up to an hour, or hourly, up to a day"
4040
}
4141
}
4242

0 commit comments

Comments
 (0)