Skip to content

fix(cloudwatch_metrics): revert to cron expression #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion modules/cloudwatch_metrics/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ resource "aws_iam_role_policy_attachment" "this" {
policy_arn = aws_iam_policy.this.arn
}



locals {
cron_expression = coalesce(
var.interval >= 60 && var.interval < 60 * 60 ? "*/${var.interval / 60} * * * ? *" : "", # minutely
var.interval >= 60 * 60 && var.interval < 24 * 60 * 60 ? "0 */${var.interval / 60 / 60} * * ? *" : "", # hourly
var.interval >= 24 * 60 * 60 ? "0 0 */${var.interval / 24 / 60 / 60} * ? *" : "", # daily
)
}

resource "aws_cloudwatch_event_rule" "trigger" {
name_prefix = var.eventbridge_name_prefix
description = "Periodically trigger Observe Lambda to collect CloudWatch metrics"
schedule_expression = var.interval == 60 ? "rate(1 minute)" : "rate(${var.interval / 60} minutes)"
schedule_expression = "cron(${local.cron_expression})"
event_bus_name = var.eventbridge_schedule_event_bus_name
}

Expand Down
4 changes: 2 additions & 2 deletions modules/cloudwatch_metrics/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ variable "interval" {
nullable = false
default = 300
validation {
condition = var.interval >= 60 && var.interval <= 10800
error_message = "interval must be in [60, 10800] (1 minute to 3 hours)"
condition = (var.interval >= 60 && var.interval <= 60 * 60) || (var.interval >= 60 * 60 && var.interval <= 24 * 60 * 60 && var.interval % (60 * 60) == 0)
error_message = "interval must be minutely, up to an hour, or hourly, up to a day"
}
}

Expand Down