Skip to content

Commit 1a8306f

Browse files
authored
fix(scheduler-targets-alpha): imported target resources as schedule target throws synth error (#32105)
### Issue # (if applicable) Tracking #31785 ### Reason for this change Similar to what was done in this PR for the Lambda Invoke target: #31837. Reasoning is explained in this [comment](#29615 (comment)): > At synth time, there are cases where CDK does not know about the actual environment (e.g. when the environment is using Tokens). In other words, the environment check works in some scenarios and not others. This creates more confusion than benefit. IF cross env target support is added in the future, this check will become a blocker for CDK customers to set up cross env target. ### Description of changes Remove synth-time error that checks for same account and region for all targets to allow use of imported resources as target. ### Description of how you validated changes All tests are passing. ### 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 dcb5cf1 commit 1a8306f

24 files changed

+57
-615
lines changed

packages/@aws-cdk/aws-scheduler-targets-alpha/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ const eventEntry: targets.EventBridgePutEventsEntry = {
207207

208208
new Schedule(this, 'Schedule', {
209209
schedule: ScheduleExpression.rate(Duration.hours(1)),
210-
target: new targets.EventBridgePutEvents(eventEntry, {}),
210+
target: new targets.EventBridgePutEvents(eventEntry),
211211
});
212212
```
213213

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/codebuild-start-build.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1-
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
1+
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
32
import { IProject } from 'aws-cdk-lib/aws-codebuild';
43
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
75

86
/**
97
* Use an AWS CodeBuild as a target for AWS EventBridge Scheduler.
108
*/
119
export class CodeBuildStartBuild extends ScheduleTargetBase implements IScheduleTarget {
1210
constructor(
1311
private readonly project: IProject,
14-
private readonly props: ScheduleTargetBaseProps = {},
12+
props: ScheduleTargetBaseProps = {},
1513
) {
1614
super(props, project.projectArn);
1715
}
1816

19-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20-
if (!sameEnvDimension(this.project.env.region, schedule.env.region)) {
21-
throw new Error(`Cannot assign project in region ${this.project.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the project must be in the same region.`);
22-
}
23-
24-
if (!sameEnvDimension(this.project.env.account, schedule.env.account)) {
25-
throw new Error(`Cannot assign project in account ${this.project.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the project must be in the same account.`);
26-
}
27-
28-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, schedule.env.account)) {
29-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.account}. Both the target and the execution role must be in the same account.`);
30-
}
31-
17+
protected addTargetActionToRole(role: IRole): void {
3218
role.addToPrincipalPolicy(new PolicyStatement({
3319
actions: ['codebuild:StartBuild'],
3420
resources: [this.project.projectArn],

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/codepipeline-start-pipeline-execution.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
1+
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
32
import { IPipeline } from 'aws-cdk-lib/aws-codepipeline';
43
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
75

86
/**
97
* Use an AWS CodePipeline pipeline as a target for AWS EventBridge Scheduler.
108
*/
119
export class CodePipelineStartPipelineExecution extends ScheduleTargetBase implements IScheduleTarget {
1210
constructor(
1311
private readonly pipeline: IPipeline,
14-
private readonly props: ScheduleTargetBaseProps = {},
12+
props: ScheduleTargetBaseProps = {},
1513
) {
1614
super(props, pipeline.pipelineArn);
1715
}
1816

19-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20-
const region = this.pipeline.env.region ?? '';
21-
const account = this.pipeline.env.account ?? '';
22-
23-
if (!sameEnvDimension(region, schedule.env.region)) {
24-
throw new Error(`Cannot assign pipeline in region ${region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the pipeline must be in the same region.`);
25-
}
26-
27-
if (!sameEnvDimension(account, schedule.env.account)) {
28-
throw new Error(`Cannot assign pipeline in account ${account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the pipeline must be in the same account.`);
29-
}
30-
31-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, account)) {
32-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.pipeline.node)} in account ${account}. Both the target and the execution role must be in the same account.`);
33-
}
34-
17+
protected addTargetActionToRole(role: IRole): void {
3518
role.addToPrincipalPolicy(new PolicyStatement({
3619
actions: ['codepipeline:StartPipelineExecution'],
3720
resources: [this.pipeline.pipelineArn],

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/event-bridge-put-events.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { IScheduleTarget, ISchedule, ScheduleTargetInput, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
32
import * as events from 'aws-cdk-lib/aws-events';
43
import { IRole } from 'aws-cdk-lib/aws-iam';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
7-
85
/**
96
* An entry to be sent to EventBridge
107
*
@@ -52,28 +49,16 @@ export interface EventBridgePutEventsEntry {
5249
export class EventBridgePutEvents extends ScheduleTargetBase implements IScheduleTarget {
5350
constructor(
5451
private readonly entry: EventBridgePutEventsEntry,
55-
private readonly props: ScheduleTargetBaseProps,
52+
private readonly props: ScheduleTargetBaseProps = {},
5653
) {
5754
super(props, entry.eventBus.eventBusArn);
5855
if (this.props.input) {
5956
throw new Error('ScheduleTargetBaseProps.input is not supported for EventBridgePutEvents. Please use entry.detail instead.');
6057
}
6158
}
6259

63-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
60+
protected addTargetActionToRole(role: IRole): void {
6461
const eventBus = this.entry.eventBus;
65-
const eventBusEnv = eventBus.env;
66-
if (!sameEnvDimension(eventBusEnv.region, schedule.env.region)) {
67-
throw new Error(`Cannot assign eventBus in region ${eventBusEnv.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the eventBus must be in the same region.`);
68-
}
69-
70-
if (!sameEnvDimension(eventBusEnv.account, schedule.env.account)) {
71-
throw new Error(`Cannot assign eventBus in account ${eventBusEnv.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the eventBus must be in the same account.`);
72-
}
73-
74-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, eventBusEnv.account)) {
75-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(eventBus.node)} in account ${eventBusEnv.account}. Both the target and the execution role must be in the same account.`);
76-
}
7762

7863
eventBus.grantPutEventsTo(role);
7964
}
Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
1-
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
1+
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
32
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
43
import { CfnAssessmentTemplate } from 'aws-cdk-lib/aws-inspector';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
75

86
/**
97
* Use an Amazon Inspector as a target for AWS EventBridge Scheduler.
108
*/
119
export class InspectorStartAssessmentRun extends ScheduleTargetBase implements IScheduleTarget {
1210
constructor(
13-
private readonly template: CfnAssessmentTemplate,
14-
private readonly props: ScheduleTargetBaseProps = {},
11+
template: CfnAssessmentTemplate,
12+
props: ScheduleTargetBaseProps = {},
1513
) {
1614
super(props, template.attrArn);
1715
}
1816

19-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20-
if (!sameEnvDimension(this.template.stack.region, schedule.env.region)) {
21-
throw new Error(`Cannot assign assessment template in region ${this.template.stack.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the assessment template must be in the same region.`);
22-
}
23-
24-
if (!sameEnvDimension(this.template.stack.account, schedule.env.account)) {
25-
throw new Error(`Cannot assign assessment template in account ${this.template.stack.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the assessment template must be in the same account.`);
26-
}
27-
28-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.template.stack.account)) {
29-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.template.node)} in account ${this.template.stack.account}. Both the target and the execution role must be in the same account.`);
30-
}
31-
17+
protected addTargetActionToRole(role: IRole): void {
3218
role.addToPrincipalPolicy(new PolicyStatement({
3319
actions: ['inspector:StartAssessmentRun'],
3420
resources: ['*'],
3521
}));
3622
}
37-
}
23+
}
Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
1-
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
1+
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
32
import { IRole, PolicyStatement } from 'aws-cdk-lib/aws-iam';
43
import { CfnDeliveryStream } from 'aws-cdk-lib/aws-kinesisfirehose';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
75

86
/**
97
* Use an Amazon Kinesis Data Firehose as a target for AWS EventBridge Scheduler.
108
*/
119
export class KinesisDataFirehosePutRecord extends ScheduleTargetBase implements IScheduleTarget {
1210
constructor(
1311
private readonly deliveryStream: CfnDeliveryStream,
14-
private readonly props: ScheduleTargetBaseProps = {},
12+
props: ScheduleTargetBaseProps = {},
1513
) {
1614
super(props, deliveryStream.attrArn);
1715
}
1816

19-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20-
if (!sameEnvDimension(this.deliveryStream.stack.region, schedule.env.region)) {
21-
throw new Error(`Cannot assign the Firehose delivery stream in region ${this.deliveryStream.stack.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the Firehose delivery stream must be in the same region.`);
22-
}
23-
24-
if (!sameEnvDimension(this.deliveryStream.stack.account, schedule.env.account)) {
25-
throw new Error(`Cannot assign the Firehose delivery stream in account ${this.deliveryStream.stack.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the Firehose delivery stream must be in the same account.`);
26-
}
27-
28-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.deliveryStream.stack.account)) {
29-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.deliveryStream.node)} in account ${this.deliveryStream.stack.account}. Both the target and the execution role must be in the same account.`);
30-
}
17+
protected addTargetActionToRole(role: IRole): void {
3118

3219
role.addToPrincipalPolicy(new PolicyStatement({
3320
actions: ['firehose:PutRecord'],
3421
resources: [this.deliveryStream.attrArn],
3522
}));
3623
}
37-
}
24+
}

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/kinesis-stream-put-record.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { ISchedule, IScheduleTarget, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names, Token } from 'aws-cdk-lib';
2+
import { Token } from 'aws-cdk-lib';
33
import { IRole } from 'aws-cdk-lib/aws-iam';
44
import * as kinesis from 'aws-cdk-lib/aws-kinesis';
55
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
76

87
/**
98
* Properties for a Kinesis Data Streams Target
@@ -34,19 +33,7 @@ export class KinesisStreamPutRecord extends ScheduleTargetBase implements ISched
3433
}
3534
}
3635

37-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
38-
if (!sameEnvDimension(this.stream.env.region, schedule.env.region)) {
39-
throw new Error(`Cannot assign stream in region ${this.stream.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the stream must be in the same region.`);
40-
}
41-
42-
if (!sameEnvDimension(this.stream.env.account, schedule.env.account)) {
43-
throw new Error(`Cannot assign stream in account ${this.stream.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the stream must be in the same account.`);
44-
}
45-
46-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.stream.env.account)) {
47-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.stream.node)} in account ${this.stream.env.account}. Both the target and the execution role must be in the same account.`);
48-
}
49-
36+
protected addTargetActionToRole(role: IRole): void {
5037
this.stream.grantWrite(role);
5138
}
5239

@@ -58,4 +45,4 @@ export class KinesisStreamPutRecord extends ScheduleTargetBase implements ISched
5845
},
5946
};
6047
}
61-
}
48+
}

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/lambda-invoke.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
1+
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
22
import { IRole } from 'aws-cdk-lib/aws-iam';
33
import * as lambda from 'aws-cdk-lib/aws-lambda';
44
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
@@ -17,7 +17,7 @@ export class LambdaInvoke extends ScheduleTargetBase implements IScheduleTarget
1717
this.func = func;
1818
}
1919

20-
protected addTargetActionToRole(_schedule: ISchedule, role: IRole): void {
20+
protected addTargetActionToRole(role: IRole): void {
2121
this.func.grantInvoke(role);
2222
}
23-
}
23+
}

packages/@aws-cdk/aws-scheduler-targets-alpha/lib/sage-maker-start-pipeline-execution.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { ISchedule, IScheduleTarget, ScheduleTargetConfig } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
32
import { IRole } from 'aws-cdk-lib/aws-iam';
43
import { IPipeline } from 'aws-cdk-lib/aws-sagemaker';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
75

86
/**
97
* Properties for a pipeline parameter
@@ -51,19 +49,7 @@ export class SageMakerStartPipelineExecution extends ScheduleTargetBase implemen
5149
}
5250
}
5351

54-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
55-
if (!sameEnvDimension(this.pipeline.stack.region, schedule.env.region)) {
56-
throw new Error(`Cannot assign pipeline in region ${this.pipeline.stack.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the pipeline must be in the same region.`);
57-
}
58-
59-
if (!sameEnvDimension(this.pipeline.stack.account, schedule.env.account)) {
60-
throw new Error(`Cannot assign pipeline in account ${this.pipeline.stack.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${schedule.env.region}. Both the schedule and the pipeline must be in the same account.`);
61-
}
62-
63-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.pipeline.stack.account)) {
64-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to invoke target ${Names.nodeUniqueId(this.pipeline.node)} in account ${this.pipeline.stack.account}. Both the target and the execution role must be in the same account.`);
65-
}
66-
52+
protected addTargetActionToRole(role: IRole): void {
6753
this.pipeline.grantStartPipelineExecution(role);
6854
}
6955

@@ -81,4 +67,4 @@ export class SageMakerStartPipelineExecution extends ScheduleTargetBase implemen
8167
sageMakerPipelineParameters,
8268
};
8369
}
84-
}
70+
}
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,20 @@
1-
import { ISchedule, IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
2-
import { Names } from 'aws-cdk-lib';
1+
import { IScheduleTarget } from '@aws-cdk/aws-scheduler-alpha';
32
import { IRole } from 'aws-cdk-lib/aws-iam';
43
import * as sns from 'aws-cdk-lib/aws-sns';
54
import { ScheduleTargetBase, ScheduleTargetBaseProps } from './target';
6-
import { sameEnvDimension } from './util';
75

86
/**
97
* Use an Amazon SNS topic as a target for AWS EventBridge Scheduler.
108
*/
119
export class SnsPublish extends ScheduleTargetBase implements IScheduleTarget {
1210
constructor(
1311
private readonly topic: sns.ITopic,
14-
private readonly props: ScheduleTargetBaseProps = {},
12+
props: ScheduleTargetBaseProps = {},
1513
) {
1614
super(props, topic.topicArn);
1715
}
1816

19-
protected addTargetActionToRole(schedule: ISchedule, role: IRole): void {
20-
// Check if target and schedule are in the region
21-
if (!sameEnvDimension(this.topic.env.region, schedule.env.region)) {
22-
throw new Error(`Cannot assign topic in region ${this.topic.env.region} to the schedule ${Names.nodeUniqueId(schedule.node)} in region ${schedule.env.region}. Both the schedule and the topic must be in the same region.`);
23-
}
24-
25-
// Check if target and schedule are in the same account
26-
if (!sameEnvDimension(this.topic.env.account, schedule.env.account)) {
27-
throw new Error(`Cannot assign topic in account ${this.topic.env.account} to the schedule ${Names.nodeUniqueId(schedule.node)} in account ${role.env.account}. Both the schedule and the topic must be in the same account.`);
28-
}
29-
30-
// Check if target and role are in the same account
31-
if (this.props.role && !sameEnvDimension(this.props.role.env.account, this.topic.env.account)) {
32-
throw new Error(`Cannot grant permission to execution role in account ${this.props.role.env.account} to publish to target ${Names.nodeUniqueId(this.topic.node)} in account ${this.topic.env.account}. Both the target and the execution role must be in the same account.`);
33-
}
34-
17+
protected addTargetActionToRole(role: IRole): void {
3518
this.topic.grantPublish(role);
3619
}
3720
}

0 commit comments

Comments
 (0)