payments-dashboard/terraform/events.tf

48 lines
1.5 KiB
Terraform
Raw Normal View History

# EventBridge schedules. Keep schedules_enabled=false until Slack Request URLs
# and the Stampli uploader point at this stack.
locals {
schedules = {
daily = {
description = "Fetch BoA previous day transactions at 9am ET (13:00 UTC)"
schedule = "cron(0 13 ? * MON-FRI *)"
function_key = "fetch_boa"
input = null
}
intraday = {
description = "Intraday BoA current-day sweep (~12pm/3pm/6pm ET)"
schedule = "cron(0 16,19,22 ? * MON-FRI *)"
function_key = "fetch_boa"
input = jsonencode({ endpoint = "current-day" })
}
}
}
resource "aws_cloudwatch_event_rule" "schedule" {
for_each = local.schedules
name = "${local.project}-${each.key}"
description = each.value.description
schedule_expression = each.value.schedule
state = var.schedules_enabled ? "ENABLED" : "DISABLED"
}
resource "aws_cloudwatch_event_target" "schedule" {
for_each = local.schedules
rule = aws_cloudwatch_event_rule.schedule[each.key].name
target_id = "${local.project}-${each.key}"
arn = aws_lambda_function.this[each.value.function_key].arn
input = each.value.input
}
resource "aws_lambda_permission" "schedule" {
for_each = local.schedules
statement_id = "AllowEventBridgeInvoke-${each.key}"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.this[each.value.function_key].function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.schedule[each.key].arn
}