diff --git a/packages/@aws-cdk/aws-scheduler-alpha/README.md b/packages/@aws-cdk/aws-scheduler-alpha/README.md index 1a3d19bd5da34..d4870ce51a5e1 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/README.md +++ b/packages/@aws-cdk/aws-scheduler-alpha/README.md @@ -257,7 +257,7 @@ const target = new targets.LambdaInvoke(fn, { ## Overriding Target Properties If you wish to reuse the same target in multiple schedules, you can override target properties like `input`, -`maximumRetryAttempts` and `maximumEventAgeInSeconds` when creating a Schedule using the `targetOverrides` parameter: +`retryAttempts` and `maxEventAge` when creating a Schedule using the `targetOverrides` parameter: ```ts declare const target: targets.LambdaInvoke; @@ -266,7 +266,7 @@ const oneTimeSchedule = new Schedule(this, 'Schedule', { schedule: ScheduleExpression.rate(cdk.Duration.hours(12)), target, targetOverrides: { - input: ScheduleTargetInput.fromText("Overriding Target Input"), + input: ScheduleTargetInput.fromText('Overriding Target Input'), maxEventAge: Duration.seconds(180), retryAttempts: 5, }, diff --git a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts index 7a946bce71c20..bd8d1e004a43d 100644 --- a/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts +++ b/packages/@aws-cdk/aws-scheduler-alpha/lib/schedule.ts @@ -217,10 +217,12 @@ export class Schedule extends Resource implements ISchedule { * The schedule group associated with this schedule. */ public readonly group?: IGroup; + /** * The arn of the schedule. */ public readonly scheduleArn: string; + /** * The name of the schedule. */ @@ -231,6 +233,11 @@ export class Schedule extends Resource implements ISchedule { */ readonly key?: kms.IKey; + /** + * A `RetryPolicy` object that includes information about the retry policy settings. + */ + private readonly retryPolicy?: CfnSchedule.RetryPolicyProperty; + constructor(scope: Construct, id: string, props: ScheduleProps) { super(scope, id, { physicalName: props.scheduleName, @@ -245,12 +252,7 @@ export class Schedule extends Resource implements ISchedule { this.key.grantEncryptDecrypt(targetConfig.role); } - const retryPolicy = { - maximumEventAgeInSeconds: props.targetOverrides?.maxEventAge?.toSeconds() ?? targetConfig.retryPolicy?.maximumEventAgeInSeconds, - maximumRetryAttempts: props.targetOverrides?.retryAttempts ?? targetConfig.retryPolicy?.maximumRetryAttempts, - }; - - this.validateRetryPolicy(retryPolicy.maximumEventAgeInSeconds, retryPolicy.maximumRetryAttempts); + this.retryPolicy = targetConfig.retryPolicy; const resource = new CfnSchedule(this, 'Resource', { name: this.physicalName, @@ -267,7 +269,7 @@ export class Schedule extends Resource implements ISchedule { props.targetOverrides?.input?.bind(this) : targetConfig.input?.bind(this), deadLetterConfig: targetConfig.deadLetterConfig, - retryPolicy: retryPolicy.maximumEventAgeInSeconds || retryPolicy.maximumRetryAttempts ? retryPolicy : undefined, + retryPolicy: this.renderRetryPolicy(props.targetOverrides?.maxEventAge?.toSeconds(), props.targetOverrides?.retryAttempts), ecsParameters: targetConfig.ecsParameters, kinesisParameters: targetConfig.kinesisParameters, eventBridgeParameters: targetConfig.eventBridgeParameters, @@ -284,12 +286,24 @@ export class Schedule extends Resource implements ISchedule { }); } - private validateRetryPolicy(maximumEventAgeInSeconds: number | undefined, maximumRetryAttempts: number | undefined) { - if (maximumEventAgeInSeconds && (maximumEventAgeInSeconds < 60 || maximumEventAgeInSeconds > 86400)) { - throw new Error(`maximumEventAgeInSeconds must be between 60 and 86400, got ${maximumEventAgeInSeconds}`); + private renderRetryPolicy( + maximumEventAgeInSeconds?: number, + maximumRetryAttempts?: number, + ): CfnSchedule.RetryPolicyProperty | undefined { + const policy = { + ...this.retryPolicy, + maximumEventAgeInSeconds: maximumEventAgeInSeconds ?? this.retryPolicy?.maximumEventAgeInSeconds, + maximumRetryAttempts: maximumRetryAttempts ?? this.retryPolicy?.maximumRetryAttempts, + }; + + if (policy.maximumEventAgeInSeconds && (policy.maximumEventAgeInSeconds < 60 || policy.maximumEventAgeInSeconds > 86400)) { + throw new Error(`maximumEventAgeInSeconds must be between 60 and 86400, got ${policy.maximumEventAgeInSeconds}`); } - if (maximumRetryAttempts && (maximumRetryAttempts < 0 || maximumRetryAttempts > 185)) { - throw new Error(`maximumRetryAttempts must be between 0 and 185, got ${maximumRetryAttempts}`); + if (policy.maximumRetryAttempts && (policy.maximumRetryAttempts < 0 || policy.maximumRetryAttempts > 185)) { + throw new Error(`maximumRetryAttempts must be between 0 and 185, got ${policy.maximumRetryAttempts}`); } + + const isEmptyPolicy = Object.values(policy).every(value => value === undefined); + return !isEmptyPolicy ? policy : undefined; } } \ No newline at end of file