DEV Community

Markus Toivakka for AWS Community Builders

Posted on • Originally published at puistikko.fi

Timezone aware Lambda cron schedule

The most straightforward way to trigger Lambda function on schedule is to create scheduled Eventbridge Rule. With Eventbridge Rule, one caveat is that all scheduled events are triggered using UTC +0 time zone. In many use cases that does not matter but there are tasks like:

  • Scheduled data loading
  • Backups
  • Environment scans and other "housekeeping tasks"

Where working with static UTC +0 is at least little bit annoying. You know how it goes, Lambda task that used to trigger 8:00 local time is one morning triggering 9:00 local time.

To implement timezone aware cron scheduled Lambda, Systems Manager Maintenance Window can be used. Maintenance Window is a technology overkill for such a simple task and I recommend to also check other features it offers for task scheduling. However, for now we are happy with just basic Lambda triggering.

How to

Full Cloudformation with timezone aware Lambda trigger can be downloaded HERE

Main resources for implementation are described as follows:

  StartWindow:
    Type: AWS::SSM::MaintenanceWindow
    Properties: 
      AllowUnassociatedTargets: true
      Cutoff: 0
      Duration: 1
      Name: CronTrigger
      Schedule: 'cron(0 18 ? * MON-FRI *)'
      ScheduleTimezone: 'Europe/Helsinki'
Enter fullscreen mode Exit fullscreen mode

Minimum precision of Schedule is one minute and scheduled tasks are using ScheduleTimezone (here Europe/Helsinki).

  StartTask:
    Type: AWS::SSM::MaintenanceWindowTask
    Properties: 
      Name: StartTask
      Priority: 5
      TaskArn: [__LAMBDA_FUNCTION_ARN__]
      WindowId: !Ref StartWindow
      ServiceRoleArn: !GetAtt AutomationExecutionRole.Arn
      TaskType: LAMBDA    # also STEP_FUNCTIONS
      TaskInvocationParameters:
        MaintenanceWindowLambdaParameters:
          Payload: !Base64 '{"message": "Hello World!"}'
Enter fullscreen mode Exit fullscreen mode

MaintenanceWindowTask defines a task that is performed on MaintenanceWindow cron schedule. Outside of Systems Manager automations, TaskType covers integrations to LAMBDA and STEP_FUNCTIONS. Other AWS or external services can be triggered with Systems Manager automation runbooks.

  • Lambda function Payload must be Base64 encoded string.

Conclusion

That's it! As Finland sets clocks back one hour in 30.10.2022, so does my Lambda cron schedule.

Top comments (0)