Implementing DST to cron triggered Lambda

Posted on Oct 28, 2022

The most straightforward way to trigger Lambda function on schedule is to create scheduled Eventbridge Rule. All scheduled events are triggered using UTC +0 time zone. In many use cases that is enough 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 technology overkill for such a simple task and I recommend to also check other features it offers for task scheduling. 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'

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!"}'

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.