All files / src/handlers scheduled.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 1/1
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                                                                                    4x       2x                    
import type { ScheduledEvent, Context } from 'aws-lambda'
 
/**
 * @interface ScheduledRunResult
 *
 * Response returned by {@link scheduledHandler} for scheduled EventBridge invocations.
 * Captures execution metadata including timestamps, request ID, and AWS account/region information.
 * Useful for job confirmation, audit trails, and CloudWatch log visibility.
 *
 * @property {('ok')} status - Fixed status value indicating successful execution
 * @property {string} executedAt - ISO 8601 timestamp of when the handler executed
 * @property {string} requestId - AWS request ID from Lambda context
 * @property {string} source - EventBridge event source (typically 'aws.events')
 * @property {string} account - AWS account ID where the Lambda is running
 * @property {string} region - AWS region where the Lambda is deployed
 *
 * @author Bayu Dwiyan Satria
 * @since 1.0.0
 * @see {@link scheduledHandler}
 */
export interface ScheduledRunResult {
  status: 'ok'
  executedAt: string
  requestId: string
  source: string
  account: string
  region: string
}
 
/**
 * @function scheduledHandler
 *
 * Sample AWS Lambda handler triggered by a scheduled CloudWatch Events / EventBridge rule.
 * No external input beyond the EventBridge event envelope; simply logs execution
 * and returns structured metadata for CloudWatch Logs visibility.
 *
 * Configure the schedule via `serverless.yml` in the functions section:
 * - `rate(5 minutes)` — every 5 minutes
 * - `rate(1 hour)` — every hour
 * - `cron(0 8 * * ? *)` — every day at 08:00 UTC
 * - `cron(0 0 ? * MON *)` — every Monday at midnight UTC
 *
 * The handler receives the raw `ScheduledEvent` envelope from EventBridge detailing
 * the trigger source, account, and region, then returns execution metadata visible
 * in CloudWatch Logs for monitoring and auditing.
 *
 * @param {ScheduledEvent} event - EventBridge scheduled event envelope
 * @param {Context} context - Lambda invocation context
 * @returns {Promise<ScheduledRunResult>} Execution result with status, timestamps, and metadata
 *
 * @example
 * // Scheduled to run every 5 minutes by EventBridge
 * const result = await scheduledHandler(event, context);
 * // {
 * //   status: 'ok',
 * //   executedAt: '2026-05-12T15:23:45.123Z',
 * //   requestId: 'xxxx-xxxx-xxxx',
 * //   source: 'aws.events',
 * //   account: '123456789012',
 * //   region: 'ap-southeast-1'
 * // }
 *
 * @author Bayu Dwiyan Satria
 * @since 1.0.0
 * @see {@link ScheduledRunResult}
 */
export const scheduledHandler = async (
  event: ScheduledEvent,
  context: Context
): Promise<ScheduledRunResult> => {
  return {
    status: 'ok',
    executedAt: new Date().toISOString(),
    requestId: context.awsRequestId,
    source: event.source,
    account: event.account,
    region: event.region
  }
}