resource "aws_cloudwatch_event_rule" "this" {
  name                = var.name
  description         = "${var.name} scheduler"
  schedule_expression = var.schedule_expression
  is_enabled          = var.schedule_enabled
}

resource "aws_cloudwatch_event_target" "this" {
  rule = aws_cloudwatch_event_rule.this.name
  arn  = aws_lambda_function.this.arn
  input = jsonencode({
    "s3_paths"    = var.s3_paths,
    "check_types" = var.check_types,
  })
}

resource "aws_lambda_permission" "this" {
  statement_id_prefix = "AllowExecutionFromCloudWatch"
  action              = "lambda:InvokeFunction"
  function_name       = var.name
  principal           = "events.amazonaws.com"
  source_arn          = aws_cloudwatch_event_rule.this.arn
}
