| 1 | import type * as scheduler from "@distilled.cloud/aws/scheduler"; |
| 2 | import type * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import type { Role } from "../IAM/Role.ts"; |
| 5 | import type { ScheduleGroup } from "./ScheduleGroup.ts"; |
| 6 | |
| 7 | /** |
| 8 | * The schedule target passed at runtime. `RoleArn` defaults to the execution |
| 9 | * role the binding was constructed with, so callers normally omit it. |
| 10 | */ |
| 11 | export interface CreateScheduleTarget extends Omit< |
| 12 | scheduler.Target, |
| 13 | "RoleArn" |
| 14 | > { |
| 15 | /** |
| 16 | * Execution role EventBridge Scheduler assumes to invoke the target. |
| 17 | * @default the execution role bound via `CreateSchedule(role)` |
| 18 | */ |
| 19 | RoleArn?: string; |
| 20 | } |
| 21 | |
| 22 | export interface CreateScheduleRequest extends Omit< |
| 23 | scheduler.CreateScheduleInput, |
| 24 | "GroupName" | "Target" | "FlexibleTimeWindow" |
| 25 | > { |
| 26 | /** |
| 27 | * Target the schedule invokes when it fires. The bound execution role is |
| 28 | * injected as `RoleArn` unless the request overrides it. |
| 29 | */ |
| 30 | Target: CreateScheduleTarget; |
| 31 | /** |
| 32 | * Flexible time window configuration. |
| 33 | * @default { Mode: "OFF" } |
| 34 | */ |
| 35 | FlexibleTimeWindow?: scheduler.FlexibleTimeWindow; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Runtime binding for `scheduler:CreateSchedule` — THE dynamic-scheduling |
| 40 | * pattern (per-user reminders, delayed callbacks): a deployed Lambda mints |
| 41 | * one-shot `at(...)` or recurring schedules at runtime. |
| 42 | * |
| 43 | * The binding is constructed with the schedule **execution role** (the IAM |
| 44 | * role EventBridge Scheduler assumes to invoke the target) and, optionally, a |
| 45 | * `ScheduleGroup` that scopes which schedules the host may create. At deploy |
| 46 | * time it contributes BOTH `scheduler:CreateSchedule` on the group's schedule |
| 47 | * ARN pattern AND `iam:PassRole` on the execution role — without the PassRole |
| 48 | * statement schedule creation fails only at runtime. |
| 49 | * ### Creating Schedules At Runtime |
| 50 | * **Example:** Mint A One-Shot Schedule From A Lambda |
| 51 | * ```typescript |
| 52 | * // deploy time: pre-create the execution role Scheduler will assume |
| 53 | * const role = yield* AWS.IAM.Role("ReminderRole", { |
| 54 | * assumeRolePolicyDocument: { |
| 55 | * Version: "2012-10-17", |
| 56 | * Statement: [ |
| 57 | * { |
| 58 | * Effect: "Allow", |
| 59 | * Principal: { Service: "scheduler.amazonaws.com" }, |
| 60 | * Action: ["sts:AssumeRole"], |
| 61 | * }, |
| 62 | * ], |
| 63 | * }, |
| 64 | * inlinePolicies: { |
| 65 | * SendReminder: { |
| 66 | * Version: "2012-10-17", |
| 67 | * Statement: [ |
| 68 | * { |
| 69 | * Effect: "Allow", |
| 70 | * Action: ["sqs:SendMessage"], |
| 71 | * Resource: [queue.queueArn], |
| 72 | * }, |
| 73 | * ], |
| 74 | * }, |
| 75 | * }, |
| 76 | * }); |
| 77 | * const createSchedule = yield* AWS.Scheduler.CreateSchedule(role); |
| 78 | * |
| 79 | * // runtime: schedule a one-shot delivery in 15 minutes |
| 80 | * const queueArn = yield* queue.queueArn; |
| 81 | * yield* createSchedule({ |
| 82 | * Name: `reminder-${userId}`, |
| 83 | * ScheduleExpression: "at(2026-01-01T00:00:00)", |
| 84 | * ActionAfterCompletion: "DELETE", |
| 85 | * Target: { |
| 86 | * Arn: yield* queueArn, |
| 87 | * Input: JSON.stringify({ userId }), |
| 88 | * }, |
| 89 | * }); |
| 90 | * ``` |
| 91 | * |
| 92 | * **Example:** Scope Creation To A Schedule Group |
| 93 | * ```typescript |
| 94 | * const group = yield* AWS.Scheduler.ScheduleGroup("Reminders", {}); |
| 95 | * const createSchedule = yield* AWS.Scheduler.CreateSchedule(role, group); |
| 96 | * // runtime calls create schedules inside the group only |
| 97 | * ``` |
| 98 | * |
| 99 | * @binding |
| 100 | */ |
| 101 | export interface CreateSchedule extends Binding.Service< |
| 102 | CreateSchedule, |
| 103 | "AWS.Scheduler.CreateSchedule", |
| 104 | <R extends Role>( |
| 105 | executionRole: R, |
| 106 | group?: ScheduleGroup, |
| 107 | ) => Effect.Effect< |
| 108 | ( |
| 109 | request: CreateScheduleRequest, |
| 110 | ) => Effect.Effect< |
| 111 | scheduler.CreateScheduleOutput, |
| 112 | scheduler.CreateScheduleError |
| 113 | > |
| 114 | > |
| 115 | > {} |
| 116 | export const CreateSchedule = Binding.Service<CreateSchedule>( |
| 117 | "AWS.Scheduler.CreateSchedule", |
| 118 | ); |