Skip to content

Commit cfa2d76

Browse files
authored
fix(scheduler): schedule not added to group with unspecified name (#27927)
Closes #27885. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7447594 commit cfa2d76

File tree

7 files changed

+307
-15
lines changed

7 files changed

+307
-15
lines changed

packages/@aws-cdk/aws-scheduler-alpha/lib/group.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
22
import * as iam from 'aws-cdk-lib/aws-iam';
33
import { CfnScheduleGroup } from 'aws-cdk-lib/aws-scheduler';
4-
import { Arn, ArnFormat, Aws, IResource, PhysicalName, RemovalPolicy, Resource, Stack } from 'aws-cdk-lib/core';
4+
import { Arn, ArnFormat, Aws, IResource, Names, RemovalPolicy, Resource, Stack } from 'aws-cdk-lib/core';
55
import { Construct } from 'constructs';
66

77
export interface GroupProps {
@@ -338,21 +338,23 @@ export class Group extends GroupBase {
338338
public readonly groupArn: string;
339339

340340
public constructor(scope: Construct, id: string, props: GroupProps) {
341-
super(scope, id, {
342-
physicalName: props.groupName ?? PhysicalName.GENERATE_IF_NEEDED,
341+
super(scope, id);
342+
343+
this.groupName = props.groupName ?? Names.uniqueResourceName(this, {
344+
maxLength: 64,
345+
separator: '-',
343346
});
344347

345348
const group = new CfnScheduleGroup(this, 'Resource', {
346-
name: this.physicalName,
349+
name: this.groupName,
347350
});
348351

349352
group.applyRemovalPolicy(props.removalPolicy);
350353

351354
this.groupArn = this.getResourceArnAttribute(group.attrArn, {
352355
service: 'scheduler',
353356
resource: 'schedule-group',
354-
resourceName: this.physicalName,
357+
resourceName: this.groupName,
355358
});
356-
this.groupName = this.physicalName;
357359
}
358-
}
360+
}

packages/@aws-cdk/aws-scheduler-alpha/test/group.test.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,31 @@ describe('Schedule Group', () => {
136136
});
137137
});
138138

139+
test('adds schedules to the group with unspecified name', () => {
140+
const group = new Group(stack, 'TestGroup', {});
141+
const role = iam.Role.fromRoleArn(stack, 'ImportedRole', 'arn:aws:iam::123456789012:role/someRole');
142+
143+
const schedule1 = new Schedule(stack, 'MyScheduleDummy1', {
144+
schedule: expr,
145+
group: group,
146+
target: new SomeLambdaTarget(func, role),
147+
});
148+
const schedule2 = new Schedule(stack, 'MyScheduleDummy2', {
149+
schedule: expr,
150+
group: group,
151+
target: new SomeLambdaTarget(func, role),
152+
});
153+
154+
expect(schedule1.group).toEqual(group);
155+
expect(schedule2.group).toEqual(group);
156+
157+
Template.fromStack(stack).hasResource('AWS::Scheduler::Schedule', {
158+
Properties: {
159+
GroupName: group.groupName,
160+
},
161+
});
162+
});
163+
139164
test('grantReadSchedules', () => {
140165
// GIVEN
141166
const props: GroupProps = {
@@ -285,4 +310,4 @@ describe('Schedule Group', () => {
285310
Namespace: 'AWS/Scheduler',
286311
});
287312
});
288-
});
313+
});

packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.assets.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/aws-cdk-scheduler-schedule.template.json

+78
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@
9898
]
9999
}
100100
},
101+
"NamedGroupA3ABC879": {
102+
"Type": "AWS::Scheduler::ScheduleGroup",
103+
"Properties": {
104+
"Name": "TestGroup"
105+
},
106+
"UpdateReplacePolicy": "Retain",
107+
"DeletionPolicy": "Retain"
108+
},
109+
"UnnamedGroupBE3E48EE": {
110+
"Type": "AWS::Scheduler::ScheduleGroup",
111+
"Properties": {
112+
"Name": "awscdkschedulerschedule-UnnamedGroup-97DBE50D"
113+
},
114+
"UpdateReplacePolicy": "Retain",
115+
"DeletionPolicy": "Retain"
116+
},
101117
"DefaultSchedule597B0B2C": {
102118
"Type": "AWS::Scheduler::Schedule",
103119
"Properties": {
@@ -128,6 +144,68 @@
128144
}
129145
}
130146
},
147+
"NamedGroupScheduleD7EEFEBC": {
148+
"Type": "AWS::Scheduler::Schedule",
149+
"Properties": {
150+
"FlexibleTimeWindow": {
151+
"Mode": "OFF"
152+
},
153+
"GroupName": "TestGroup",
154+
"ScheduleExpression": "rate(12 hours)",
155+
"ScheduleExpressionTimezone": "Etc/UTC",
156+
"State": "ENABLED",
157+
"Target": {
158+
"Arn": {
159+
"Fn::GetAtt": [
160+
"Function76856677",
161+
"Arn"
162+
]
163+
},
164+
"Input": "\"Input Text\"",
165+
"RetryPolicy": {
166+
"MaximumEventAgeInSeconds": 180,
167+
"MaximumRetryAttempts": 3
168+
},
169+
"RoleArn": {
170+
"Fn::GetAtt": [
171+
"Role1ABCC5F0",
172+
"Arn"
173+
]
174+
}
175+
}
176+
}
177+
},
178+
"UnnamedGroupSchedule19260E9B": {
179+
"Type": "AWS::Scheduler::Schedule",
180+
"Properties": {
181+
"FlexibleTimeWindow": {
182+
"Mode": "OFF"
183+
},
184+
"GroupName": "awscdkschedulerschedule-UnnamedGroup-97DBE50D",
185+
"ScheduleExpression": "rate(12 hours)",
186+
"ScheduleExpressionTimezone": "Etc/UTC",
187+
"State": "ENABLED",
188+
"Target": {
189+
"Arn": {
190+
"Fn::GetAtt": [
191+
"Function76856677",
192+
"Arn"
193+
]
194+
},
195+
"Input": "\"Input Text\"",
196+
"RetryPolicy": {
197+
"MaximumEventAgeInSeconds": 180,
198+
"MaximumRetryAttempts": 3
199+
},
200+
"RoleArn": {
201+
"Fn::GetAtt": [
202+
"Role1ABCC5F0",
203+
"Arn"
204+
]
205+
}
206+
}
207+
}
208+
},
131209
"DisabledScheduleA1DF7F0F": {
132210
"Type": "AWS::Scheduler::Schedule",
133211
"Properties": {

packages/@aws-cdk/aws-scheduler-alpha/test/integ.schedule.js.snapshot/manifest.json

+25-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)