Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(scheduler): disable Schedule on creation #27236

Merged
merged 10 commits into from
Sep 22, 2023
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ export interface ScheduleProps {
/**
* The schedule's group.
*
* @deafult - By default a schedule will be associated with the `default` group.
* @default - By default a schedule will be associated with the `default` group.
*/
readonly group?: IGroup;

/**
* Indicates whether the schedule is enabled.
* @default true
*/
readonly enabled?: boolean;
}

/**
Expand Down Expand Up @@ -95,6 +101,7 @@ export class Schedule extends Resource implements ISchedule {
scheduleExpression: props.schedule.expressionString,
scheduleExpressionTimezone: props.schedule.timeZone?.timezoneName,
groupName: this.group?.groupName,
state: props.enabled == null ? 'ENABLED' : (props.enabled ? 'ENABLED' : 'DISABLED'),
MoartnW marked this conversation as resolved.
Show resolved Hide resolved
target: {
arn: targetConfig.arn,
roleArn: targetConfig.role.roleArn,
Expand Down
63 changes: 63 additions & 0 deletions packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { App, Stack } from 'aws-cdk-lib';

import { Template } from 'aws-cdk-lib/assertions';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { IScheduleTarget, Schedule, ScheduleTargetConfig } from '../lib';
import { ScheduleExpression } from '../lib/schedule-expression';

class SomeLambdaTarget implements IScheduleTarget {
public constructor(private readonly fn: lambda.IFunction, private readonly role: iam.IRole) {
}

public bind(): ScheduleTargetConfig {
return {
arn: this.fn.functionArn,
role: this.role,
};
}
}

describe('Schedule', () => {
let stack: Stack;
let func: lambda.IFunction;
MoartnW marked this conversation as resolved.
Show resolved Hide resolved
const expr = ScheduleExpression.at(new Date(Date.UTC(1969, 10, 20, 0, 0, 0)));

beforeEach(() => {
const app = new App();
stack = new Stack(app, 'Stack', { env: { region: 'us-east-1', account: '123456789012' } });
func = new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_LATEST,
tracing: lambda.Tracing.PASS_THROUGH,
});
});

test('schedule is enabled by default', () => {
// WHEN
const schedule = new Schedule(stack, 'TestSchedule', {
schedule: expr,
target: new SomeLambdaTarget(func, iam.Role.fromRoleArn(stack, 'ImportedRole', 'arn:aws:iam::123456789012:role/someRole')),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Scheduler::Schedule', {
State: 'ENABLED',
});
});

test('schedule can be disabled', () => {
// WHEN
const schedule = new Schedule(stack, 'TestSchedule', {
schedule: expr,
target: new SomeLambdaTarget(func, iam.Role.fromRoleArn(stack, 'ImportedRole', 'arn:aws:iam::123456789012:role/someRole')),
enabled: false,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Scheduler::Schedule', {
State: 'DISABLED',
});
});
});
Loading