Skip to content

Commit 752bd9b

Browse files
authored
fix(scheduler): scheduleName returns undefined when imported from ARN (#36400)
### Issue # (if applicable) Closes #36361. ### Reason for this change When importing a schedule using `Schedule.fromScheduleArn()`, the `scheduleName` property returns `undefined` instead of the actual schedule name. ### Description of changes Fixed the ARN parsing logic in `Schedule.fromScheduleArn()` to correctly extract the schedule name from the resource path. AWS Scheduler ARNs have the format `arn:aws:scheduler:region:account:schedule/GROUP/NAME`, and the previous implementation only captured the group name. ### Describe any new or updated permissions being added No new or updated permissions. ### Description of how you validated changes Added unit tests to verify correct parsing and error handling. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 3485d53 commit 752bd9b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/aws-cdk-lib/aws-scheduler/lib/schedule.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,18 @@ export class Schedule extends Resource implements ISchedule {
255255
* Import an existing schedule using the ARN.
256256
*/
257257
public static fromScheduleArn(scope: Construct, id: string, scheduleArn: string): ISchedule {
258+
// Format: arn:aws:scheduler:region:account:schedule/SCHEDULE-GROUP/SCHEDULE-NAME
259+
const parsedName = Arn.split(scheduleArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName?.split('/')[1];
260+
261+
if (!parsedName) {
262+
throw new UnscopedValidationError(`Invalid schedule ARN format. Expected: arn:<partition>:scheduler:<region>:<account>:schedule/<group-name>/<schedule-name>, got: ${scheduleArn}`);
263+
}
264+
265+
const scheduleName = parsedName;
266+
258267
class Import extends Resource implements ISchedule {
259268
public readonly scheduleArn = scheduleArn;
260-
public readonly scheduleName = Arn.split(scheduleArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!.split('/')[1];
269+
public readonly scheduleName = scheduleName;
261270
public get scheduleRef(): ScheduleReference {
262271
return {
263272
scheduleArn: this.scheduleArn,

packages/aws-cdk-lib/aws-scheduler/test/schedule.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,30 @@ describe('Schedule', () => {
260260
});
261261
});
262262
});
263+
264+
describe('fromScheduleArn', () => {
265+
test('correctly parses schedule name from ARN', () => {
266+
// WHEN
267+
const importedSchedule = Schedule.fromScheduleArn(
268+
stack,
269+
'ImportedSchedule',
270+
'arn:aws:scheduler:us-east-1:123456789012:schedule/my-schedule-group/my-schedule-name',
271+
);
272+
273+
// THEN
274+
expect(importedSchedule.scheduleName).toEqual('my-schedule-name');
275+
expect(importedSchedule.scheduleArn).toEqual('arn:aws:scheduler:us-east-1:123456789012:schedule/my-schedule-group/my-schedule-name');
276+
});
277+
278+
test('throws error for invalid ARN format', () => {
279+
// WHEN/THEN
280+
expect(() => {
281+
Schedule.fromScheduleArn(
282+
stack,
283+
'ImportedSchedule',
284+
'arn:aws:scheduler:us-east-1:123456789012:schedule/invalid',
285+
);
286+
}).toThrow('Invalid schedule ARN format. Expected: arn:<partition>:scheduler:<region>:<account>:schedule/<group-name>/<schedule-name>, got: arn:aws:scheduler:us-east-1:123456789012:schedule/invalid');
287+
});
288+
});
263289
});

0 commit comments

Comments
 (0)