Please contact us if you need access to this feature.

You can define a schedule to automatically invoke functions. For example, you could schedule a function to periodically poll an external API for new inventory data. The Functions UI shows schedules created through the UI and through the SDK on each function’s overview page.

Function schedules are independent from function event bindings. Applying a schedule to a function will cause the function to run on the schedule you specify, regardless of events that trigger the event binding.

Configure a schedule

To configure a schedule:

  1. Go to the Functions UI.
  2. Click the function you want to schedule to go to its overview page.
  3. Click Add schedule.
  4. Set the schedule.
    • Use the Basic tab to define a simple schedule that runs your function after a specific number of minutes, hours, or days.
    • Use the Advanced tab to enter a custom cron expression.
  5. Click Create schedule.

Cron expressions (Advanced)

Cron expressions are a powerful way to schedule your function at a specific time or a specific interval. For example, you could use cron expressions to invoke a function at 8:00 AM (UTC+0) every first day of the month. The following table shows example cron expressions.

ExpressionResult
cron(15 4 * * MON-FRI)At 04:15 on every day-of-week from Monday through Friday.
cron(0 12 1 * *)At 12:00 on the first day of every month.
cron(1 8,20 1 * *)At 8:01 and 20:01 on the first day of every month.

Syntax

The cron expression syntax is: cron(minutes hours day-of-month month day-of-week year)

The following table shows the valid values for each field.

FieldValuesWildcards
Minutes0-59, - * /
Hours0-23, - * /
Day-of-month1-31, - * ? / L W
Month`1-12 or JAN-DEC, - * /
Day-of-week`1-7 or SUN-SAT, - * ? L #
Year1970-2199, - * /

Wildcards

  • The , (comma) wildcard includes additional values. In the Month field, JAN, FEB, MAR includes January, February, and March.
  • The - (dash) wildcard specifies ranges. In the Day field, 1-15 includes days 1 through 15 of the specified month.
  • The * (asterisk) wildcard includes all values in the field. In the Hours field, * includes every hour. You can’t use * in both the - Day-of-month and Day-of-week fields. If you use it in one, you must use ? in the other.
  • The / (slash) wildcard specifies increments. In the Minutes field, you could enter 1/10 to specify every tenth minute, starting from the first minute of the hour (for example, the 11th, 21st, and 31st minute, and so on).
  • The ? (question mark) wildcard specifies any. In the Day-of-month field you could enter 7 and if any day of the week was acceptable, you could enter ? in the Day-of-week field.
  • The L wildcard in the Day-of-month or Day-of-week fields specifies the last day of the month or week.
  • The W wildcard in the Day-of-month field specifies a weekday. In the Day-of-month field, 3W specifies the weekday closest to the third day of the month.
  • The # wildcard in the Day-of-week field specifies a certain instance of the specified day of the week within a month. For example, 3#2 would be the second Tuesday of the month: the 3 refers to Tuesday because it is the third day of each week, and the 2 refers to the second day of that type within the month.

Restrictions

  • Cron expressions that result in your function running faster than once per 1 minute are not supported.

Functions SDK

You can also configure function schedules using the Functions SDK. The SDK allows for more configurabilty than the UI, including:

  • Adding more than a single function schedule per Function
  • Specifying a timezone other than UTC
  • Disabling or enabling a Schedule
  • Setting time boundaries.

The following example shows how to set up a functions schedule using the SDK.

import {
  EventsClient,
  CreateFunctionScheduleCommand,
} from '@stedi/sdk-client-events'; // ES Modules import
const client = new EventsClient({
  token: { token: accountToken },
});
const input = {
  functionScheduleName: 'STRING_VALUE', // required
  idempotencyKey: 'STRING_VALUE',
  /** startTime and endTime serve as time boundaries in which your schedule will be active. For example,
   *  you can choose to implement a schedule that runs once a day,
   * but only between the 1st and 31st of March.
   */
  endTime: 'STRING_VALUE',
  functionName: 'STRING_VALUE', // required
  /**
   * JSON input that will be passed to the target when the schedule is invoked.
   */
  functionInput: 'DOCUMENT_VALUE',
  scheduleExpression: 'STRING_VALUE', // required
  /**
   * Ensure your function runs at a certain time in a certain timezone.
   * Accepted values are listed in the IANA Time Zone Database: https://www.iana.org/time-zones
   *
   */
  scheduleExpressionTimezone: 'STRING_VALUE',
  /** startTime and endTime serve as time boundaries in which your schedule will be active. For example,
   *  you can choose to implement a schedule that runs once a day,
   * but only between the 1st and 31st of March.
   */
  startTime: 'STRING_VALUE',
  /**
   * If you would like to keep your functions schedule but want to temporarily disable it, you can use the
   * `state` property. When you are ready to enable the schedule again, use `new
   * UpdateFunctionScheduleCommand` with the `state` property set to `ENABLED`.
   */
  state: 'ENABLED' || 'DISABLED',
};
const command = new CreateFunctionScheduleCommand(input);
const {
  functionScheduleName,
  resourceDetail: { status },
} = await client.send(command);

SDK schedule expressions

The SDK supports both cron and rate expressions.

Rate expressions

Use rate expressions to invoke a function at regular intervals. Intervals can be set to run at a specific number of days, hours, or minutes. The following table shows example rate expressions.

ExpressionResult
rate(1 day)Run the function once a day
rate(36 hours)Run the function every 36 hours
rate(15 minutes)Run the function every fifteen minutes