Skip to content

Commit

Permalink
feat(schedule-alpha): support target properties override
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Oct 19, 2023
1 parent c445b8c commit dd7ed9d
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 13 deletions.
16 changes: 15 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,22 @@ const target = new targets.LambdaInvoke(fn, {

## Overriding Target Properties

TODO: Not yet implemented. See section in [L2 Event Bridge Scheduler RFC](https://github.com/aws/aws-cdk-rfcs/blob/master/text/0474-event-bridge-scheduler-l2.md)
If you wish to reuse the same target in multiple schedules, you can override target properties like `input`,
`maximumRetryAttempts` and `maximumEventAge` when creating a Schedule using the `targetOverrides` parameter:

```ts
declare const target: targets.LambdaInvoke;

const oneTimeSchedule = new Schedule(this, 'Schedule', {
schedule: ScheduleExpression.rate(cdk.Duration.hours(12)),
target,
targetOverrides: {
input: ScheduleTargetInput.fromText("Overriding Target Input"),
maximumEventAge: Duration.seconds(180),
maximumRetryAttempts: 5,
},
});
```

## Monitoring

Expand Down
54 changes: 51 additions & 3 deletions packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { IResource, Resource } from 'aws-cdk-lib';
import { Duration, IResource, Resource } from 'aws-cdk-lib';
import { CfnSchedule } from 'aws-cdk-lib/aws-scheduler';
import { Construct } from 'constructs';
import { IGroup } from './group';
import { ScheduleTargetInput } from './input';
import { ScheduleExpression } from './schedule-expression';
import { IScheduleTarget } from './target';

Expand All @@ -23,6 +24,30 @@ export interface ISchedule extends IResource {
readonly scheduleArn: string;
}

export interface ScheduleTargetProps {
/**
* The text, or well-formed JSON, passed to the target.
*
* If you are configuring a templated Lambda, AWS Step Functions, or Amazon EventBridge target,
* the input must be a well-formed JSON. For all other target types, a JSON is not required.
*
* @default - The target's input is used.
*/
readonly input?: ScheduleTargetInput;
/**
* The maximum amount of time, in seconds, to continue to make retry attempts.
*
* @default - The target's maximumEventAgeInSeconds is used.
*/
readonly maximumEventAge?: Duration;
/**
* The maximum number of retry attempts to make before the request fails.
*
* @default - The target's maximumRetryAttempts is used.
*/
readonly maximumRetryAttempts?: number;
}

/**
* Construction properties for `Schedule`.
*/
Expand All @@ -38,6 +63,11 @@ export interface ScheduleProps {
*/
readonly target: IScheduleTarget;

/**
* Allows to override target properties when creating a new schedule.
*/
readonly targetOverrides?: ScheduleTargetProps;

/**
* The name of the schedule.
*
Expand Down Expand Up @@ -94,6 +124,13 @@ export class Schedule extends Resource implements ISchedule {

const targetConfig = props.target.bind(this);

const retryPolicy = {
maximumEventAgeInSeconds: props.targetOverrides?.maximumEventAge?.toSeconds() ?? targetConfig.retryPolicy?.maximumEventAgeInSeconds,
maximumRetryAttempts: props.targetOverrides?.maximumRetryAttempts ?? targetConfig.retryPolicy?.maximumRetryAttempts,
};

this.validateRetryPolicy(retryPolicy.maximumEventAgeInSeconds, retryPolicy.maximumRetryAttempts);

const resource = new CfnSchedule(this, 'Resource', {
name: this.physicalName,
flexibleTimeWindow: { mode: 'OFF' },
Expand All @@ -104,9 +141,11 @@ export class Schedule extends Resource implements ISchedule {
target: {
arn: targetConfig.arn,
roleArn: targetConfig.role.roleArn,
input: targetConfig.input?.bind(this),
input: props.targetOverrides?.input ?
props.targetOverrides?.input?.bind(this) :
targetConfig.input?.bind(this),
deadLetterConfig: targetConfig.deadLetterConfig,
retryPolicy: targetConfig.retryPolicy,
retryPolicy: retryPolicy.maximumEventAgeInSeconds || retryPolicy.maximumRetryAttempts ? retryPolicy : undefined,
ecsParameters: targetConfig.ecsParameters,
kinesisParameters: targetConfig.kinesisParameters,
eventBridgeParameters: targetConfig.eventBridgeParameters,
Expand All @@ -122,4 +161,13 @@ export class Schedule extends Resource implements ISchedule {
resourceName: `${this.group?.groupName ?? 'default'}/${this.physicalName}`,
});
}

private validateRetryPolicy(maximumEventAgeInSeconds: number | undefined, maximumRetryAttempts: number | undefined) {
if (maximumEventAgeInSeconds && (maximumEventAgeInSeconds < 60 || maximumEventAgeInSeconds > 900)) {
throw new Error(`maximumEventAgeInSeconds must be between 60 and 900, got ${maximumEventAgeInSeconds}`);
}
if (maximumRetryAttempts && (maximumRetryAttempts < 0 || maximumRetryAttempts > 185)) {
throw new Error(`maximumRetryAttempts must be between 0 and 185, got ${maximumRetryAttempts}`);
}
}
}
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-scheduler-alpha/test/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,24 @@ describe('schedule target input', () => {
},
});
});

test('can override target input', () => {
// WHEN
const input = ScheduleTargetInput.fromText('Original Input');
new Schedule(stack, 'TestSchedule', {
schedule: expr,
target: new SomeLambdaTarget(func, input),
targetOverrides: {
input: ScheduleTargetInput.fromText('Overridden Input'),
},
enabled: false,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Scheduler::Schedule', {
Target: {
Input: '"Overridden Input"',
},
});
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
"Arn"
]
},
"Input": "\"Input Text\"",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 180,
"MaximumRetryAttempts": 3
},
"RoleArn": {
"Fn::GetAtt": [
"Role1ABCC5F0",
Expand All @@ -108,6 +113,41 @@
"Arn"
]
},
"Input": "\"Input Text\"",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 180,
"MaximumRetryAttempts": 3
},
"RoleArn": {
"Fn::GetAtt": [
"Role1ABCC5F0",
"Arn"
]
}
}
}
},
"TargetOverrideScheduleFF8CB184": {
"Type": "AWS::Scheduler::Schedule",
"Properties": {
"FlexibleTimeWindow": {
"Mode": "OFF"
},
"ScheduleExpression": "rate(12 hours)",
"ScheduleExpressionTimezone": "Etc/UTC",
"State": "ENABLED",
"Target": {
"Arn": {
"Fn::GetAtt": [
"Function76856677",
"Arn"
]
},
"Input": "\"Changed Text\"",
"RetryPolicy": {
"MaximumEventAgeInSeconds": 360,
"MaximumRetryAttempts": 5
},
"RoleArn": {
"Fn::GetAtt": [
"Role1ABCC5F0",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// !cdk-integ aws-cdk-scheduler-schedule
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
import * as cdk from 'aws-cdk-lib';
import * as iam from 'aws-cdk-lib/aws-iam';
Expand All @@ -12,6 +13,11 @@ class SomeLambdaTarget implements scheduler.IScheduleTarget {
return {
arn: this.fn.functionArn,
role: this.role,
input: scheduler.ScheduleTargetInput.fromText('Input Text'),
retryPolicy: {
maximumEventAgeInSeconds: 180,
maximumRetryAttempts: 3,
},
};
}
}
Expand Down Expand Up @@ -42,6 +48,18 @@ new scheduler.Schedule(stack, 'DisabledSchedule', {
enabled: false,
});

new scheduler.Schedule(stack, 'TargetOverrideSchedule', {
schedule: expression,
target: target,
targetOverrides: {
input: scheduler.ScheduleTargetInput.fromText('Changed Text'),
maximumEventAge: cdk.Duration.seconds(360),
maximumRetryAttempts: 5,
},
});

new IntegTest(app, 'integtest-schedule', {
testCases: [stack],
});
});

app.synth();
Loading

0 comments on commit dd7ed9d

Please sign in to comment.