# grada addon: cron (Lambda target) # Generated by `grada add cron` on a --target lambda project. Do not # edit by hand unless you know what you are doing — re-running with --force # overwrites this file. # # Billing: $0/mo fixed baseline (first 14M EventBridge Scheduler # invocations/mo free); billed only for Lambda invocations while the job runs. # # NOTE: unlike the ECS variant, the scheduler invokes the web function # directly instead of running a container command. Handle the scheduled event # in application code (the payload below carries the configured command), or # point the schedule at a dedicated function. # --- IAM Role assumed by EventBridge Scheduler --- resource "aws_iam_role" "scheduler_cron_role" { name = "${local.app_name}-cron-scheduler-role" permissions_boundary = local.preview_boundary_arn assume_role_policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Principal = { Service = "scheduler.amazonaws.com" } Action = "sts:AssumeRole" } ] }) } # --- Least-privilege permission to invoke the function --- resource "aws_iam_role_policy" "scheduler_cron_policy" { name = "${local.app_name}-cron-scheduler-policy" role = aws_iam_role.scheduler_cron_role.id policy = jsonencode({ Version = "2012-10-17" Statement = [ { Effect = "Allow" Action = ["lambda:InvokeFunction"] Resource = [ aws_lambda_function.app.arn, "${aws_lambda_function.app.arn}:*" ] } ] }) } # --- Scheduled invocation (single schedule per project) --- resource "aws_scheduler_schedule" "cron" { name = "${local.app_name}-cron-{{CRON_NAME}}" schedule_expression = "{{SCHEDULE_EXPRESSION}}" schedule_expression_timezone = "{{SCHEDULE_TIMEZONE}}" # Sleep guard: stays disabled while grada sleep's auto-vars are active # so a raw apply never re-enables a paused schedule. state = var.asleep ? "DISABLED" : "ENABLED" flexible_time_window { mode = "OFF" } target { arn = aws_lambda_function.app.arn role_arn = aws_iam_role.scheduler_cron_role.arn input = jsonencode({ source = "grada-cron" command = {{CRON_COMMAND_JSON}} }) } } # --- Outputs --- output "cron_schedule_name" { value = aws_scheduler_schedule.cron.name } output "cron_schedule_arn" { value = aws_scheduler_schedule.cron.arn }