Skip to content
triggers.tf 1.29 KiB
Newer Older
nimrod's avatar
nimrod committed
variable "rate" {
  default     = 15
  description = "How often in minutes the Lambda functions will trigger."
  type        = number
}

output "rate" {
  description = "How often in minutes the Lambda functions will trigger."
  value       = var.rate
}

resource "aws_cloudwatch_event_rule" "schedule" {
nimrod's avatar
nimrod committed
  count               = length(local.function_names)
  name                = local.function_names[count.index]
  description         = "Schedule to trigger ${local.function_names[count.index]} functions in ${local.env}."
nimrod's avatar
nimrod committed
  schedule_expression = "rate(${var.rate} minutes)"
  tags                = local.common_tags
}

locals {
nimrod's avatar
nimrod committed
  cloudwatch_rule_arns  = aws_cloudwatch_event_rule.schedule.*.arn
  cloudwatch_rule_names = aws_cloudwatch_event_rule.schedule.*.name
nimrod's avatar
nimrod committed
output "cloudwatch_rule_arns" {
  description = "List of ARNs of the CloudWatch schedule rules."
  value       = local.cloudwatch_rule_arns
nimrod's avatar
nimrod committed
output "cloudwatch_rule_names" {
  description = "List of names of the CloudWatch schedule rules."
  value       = local.cloudwatch_rule_names
nimrod's avatar
nimrod committed
}

resource "aws_cloudwatch_event_target" "schedule" {
nimrod's avatar
nimrod committed
  count = length(local.function_alias_arns)
nimrod's avatar
nimrod committed
  arn   = local.function_alias_arns[count.index]
nimrod's avatar
nimrod committed
  rule  = local.cloudwatch_rule_names[count.index]
nimrod's avatar
nimrod committed
  depends_on = [
    aws_lambda_permission.cloudwatch,
  ]
}