Monitoring scheduled AWS Lambda functions (EventBridge)
Scheduled Lambdas (EventBridge Scheduler or an EventBridge rule) fail quietly in ways a metric alarm can miss. The schedule gets disabled or deleted in a deploy, the function times out before finishing, a permissions change stops the invocation, or the code catches an exception and returns normally. A heartbeat checks the one thing you care about: did the job finish on time?
Python runtime
import os
import urllib.request
PING = os.environ["PING_URL"]
def ping(suffix=""):
try:
urllib.request.urlopen(PING + suffix, timeout=5).close()
except Exception as exc: # never let monitoring break the job
print(f"heartbeat ping failed: {exc}")
def handler(event, context):
ping("/start")
try:
do_the_work()
except Exception:
ping("/fail")
raise
ping()
Node.js runtime (18+)
const ping = (s = "") =>
fetch(process.env.PING_URL + s, { signal: AbortSignal.timeout(5000) }).catch(() => {});
export const handler = async () => {
await ping("/start");
try {
await doTheWork();
} catch (err) {
await ping("/fail");
throw err;
}
await ping();
};
Match the schedule
EventBridge writes cron with six fields, for example cron(0 3 * * ? *). In CronPup, use the standard
five-field form, 0 3 * * *. EventBridge rules run in UTC, so set the monitor's timezone to UTC.
EventBridge Scheduler lets you pick a timezone, so use the same one in CronPup.
Timeouts and retries
- If the function hits its timeout, it never sends the success ping, so the monitor goes down once the grace period passes. Set the grace period a little above the function timeout.
- Asynchronous invocations are retried by Lambda (up to two retries by default). A retry that succeeds pings success and marks the monitor up again.
- The function needs outbound internet access. Inside a VPC, that means a NAT gateway.
Monitor your scheduled Lambdas. 10 monitors are free.