Scheduling in AWS using time zone πŸ“…

Jing Ma
PostNL Engineering
Published in
3 min readDec 20, 2022
Scheduling

Sometimes you need to schedule an AWS service based on your local time zone. As of now, that's possible! In this tutorial, we will schedule a lambda using an EventBridge schedule. We will use CDK to create the infrastructure. Check out the architecture πŸ› below.

Architecture

First, we need a lambda. In this example we're using a Node.js lambda.

const func = new NodejsFunction(this, 'func-id', {
runtime: Runtime.NODEJS_18_X
});

Then, we need a role πŸ‘· so the EventBridge schedule can invoke the lambda.

const role = new Role(this, 'role-id', {
managedPolicies: [{ managedPolicyArn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaRole' }],
assumedBy: new ServicePrincipal('scheduler.amazonaws.com')
});

Finally, we need to create an EventBridge schedule πŸ“….

new CfnSchedule(this, 'schedule-id', {
flexibleTimeWindow: {
mode: 'OFF',
},
scheduleExpression: 'cron(0 6,8 ? * MON-FRI *)',
scheduleExpressionTimezone: 'Europe/Amsterdam',
target: {
arn: func.functionArn,
roleArn: role.roleArn,
},
});

Let's go over some of the fields in the CfnSchedule construct.

  • target: here you can find a reference πŸ“™ to both the ARN of the lambda and of the role.
  • scheduleExpressionTimezone: here you can pick your time zone of choice πŸ‘†.
  • scheduleExpression: here you can add a cron schedule. The easiest way to test your cron is to enter it in the AWS console when creating a schedule. You will get instant validation feedback if your cron is not correct ❌.
Cron validation

You will also see the upcoming trigger πŸ”« dates, which allows you to verify your scheduling.

Upcoming trigger dates

Example code

Here's the entire CDK code that's already working. First, the stack.

import { Stack, StackProps } from 'aws-cdk-lib';
import { Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler';
import { Construct } from 'constructs';
import { join } from 'path';

export class SchedulingStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const func = new NodejsFunction(this, 'func-id', {
entry: join(__dirname, '../src/lambda/func.ts'),
runtime: Runtime.NODEJS_18_X,
});
const role = new Role(this, 'role-id', {
managedPolicies: [{ managedPolicyArn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaRole' }],
assumedBy: new ServicePrincipal('scheduler.amazonaws.com'),
});
new CfnSchedule(this, 'schedule-id', {
flexibleTimeWindow: {
mode: 'OFF',
},
scheduleExpression: 'cron(0 6,8 ? * MON-FRI *)',
scheduleExpressionTimezone: 'Europe/Amsterdam',
target: {
arn: func.functionArn,
roleArn: role.roleArn,
},
});
}
}

Then, the CDK lambda code.

export async function handler(): Promise<void> {
console.log('Func-y!');
}

If you execute a cdk deploy, this code should deploy a EventBridge schedule, IAM role and lambda function to your account.

You're all set now! Good luck writing your own time zone based schedule πŸ“….

--

--