Skip to content

Commit 5879178

Browse files
rix0rrrskinny85
authored andcommitted
fix(events): allow adding same target to rule multiple times (#3353)
As rule targets can have different input configurations adding the same target multiple times must be possible. Move rule target id generation to `aws-events` where it's easy to simply increment a counter. This id is passed as an argument to `bind()` for targets that need to know their given id (e.g. `EcsTask`). Add `permissionsNode` on `IFunction` to handle permission checks for both functions and singleton functions. Fixes #3173
1 parent 9ec57af commit 5879178

File tree

47 files changed

+186
-114
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+186
-114
lines changed

packages/@aws-cdk/aws-cloudtrail/test/test.cloudtrail.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ export = {
200200
trail.onCloudTrailEvent('DoEvents', {
201201
target: {
202202
bind: () => ({
203+
id: '',
203204
arn: 'arn',
204-
id: 'myid'
205205
})
206206
}
207207
});
@@ -217,7 +217,7 @@ export = {
217217
Targets: [
218218
{
219219
Arn: "arn",
220-
Id: "myid"
220+
Id: "Target0"
221221
}
222222
]
223223
}));

packages/@aws-cdk/aws-codecommit/test/integ.codecommit-events.expected.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"Arn": {
3838
"Ref": "MyTopic86869434"
3939
},
40-
"Id": "MyTopic"
40+
"Id": "Target0"
4141
}
4242
]
4343
}
@@ -46,4 +46,4 @@
4646
"Type": "AWS::SNS::Topic"
4747
}
4848
}
49-
}
49+
}

packages/@aws-cdk/aws-codecommit/test/integ.codecommit-events.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repo.onReferenceCreated('OnReferenceCreated', {
1616
target: {
1717
bind: () => ({
1818
arn: topic.topicArn,
19-
id: 'MyTopic'
19+
id: ''
2020
})
2121
}
2222
});

packages/@aws-cdk/aws-codepipeline-actions/test/integ.lambda-deployed-through-codepipeline.lit.expected.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@
11961196
]
11971197
]
11981198
},
1199-
"Id": "PipelineStackPipeline9DB740AF",
1199+
"Id": "Target0",
12001200
"RoleArn": {
12011201
"Fn::GetAtt": [
12021202
"PipelineEventsRole46BEEA7C",
@@ -1268,7 +1268,7 @@
12681268
]
12691269
]
12701270
},
1271-
"Id": "PipelineStackPipeline9DB740AF",
1271+
"Id": "Target0",
12721272
"RoleArn": {
12731273
"Fn::GetAtt": [
12741274
"PipelineEventsRole46BEEA7C",

packages/@aws-cdk/aws-codepipeline-actions/test/integ.lambda-pipeline.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@
638638
]
639639
]
640640
},
641-
"Id": "awscdkcodepipelinelambdaPipeline87A4B3D3",
641+
"Id": "Target0",
642642
"RoleArn": {
643643
"Fn::GetAtt": [
644644
"PipelineEventsRole46BEEA7C",

packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-build-multiple-inputs-outputs.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
]
6262
]
6363
},
64-
"Id": "awscdkcodepipelinecodebuildmultipleinputsoutputsPipeline314D3A85",
64+
"Id": "Target0",
6565
"RoleArn": {
6666
"Fn::GetAtt": [
6767
"PipelineEventsRole46BEEA7C",

packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-code-commit.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
]
6262
]
6363
},
64-
"Id": "awscdkcodepipelinecodecommitPipelineF780CA18",
64+
"Id": "Target0",
6565
"RoleArn": {
6666
"Fn::GetAtt": [
6767
"PipelineEventsRole46BEEA7C",

packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-ecr-source.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@
423423
]
424424
]
425425
},
426-
"Id": "awscdkcodepipelineecrsourceMyPipeline63CF3194",
426+
"Id": "Target0",
427427
"RoleArn": {
428428
"Fn::GetAtt": [
429429
"MyPipelineEventsRoleFAB99F32",

packages/@aws-cdk/aws-codepipeline-actions/test/integ.pipeline-events.expected.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@
510510
"Arn": {
511511
"Ref": "MyTopic86869434"
512512
},
513-
"Id": "awscdkpipelineeventtargetMyTopic8D32776A"
513+
"Id": "Target0"
514514
}
515515
]
516516
}
@@ -562,7 +562,7 @@
562562
"Arn": {
563563
"Ref": "MyTopic86869434"
564564
},
565-
"Id": "awscdkpipelineeventtargetMyTopic8D32776A"
565+
"Id": "Target0"
566566
}
567567
]
568568
}
@@ -671,7 +671,7 @@
671671
"Arn": {
672672
"Ref": "MyTopic86869434"
673673
},
674-
"Id": "awscdkpipelineeventtargetMyTopic8D32776A",
674+
"Id": "Target0",
675675
"InputTransformer": {
676676
"InputPathsMap": {
677677
"detail-pipeline": "$.detail.pipeline",

packages/@aws-cdk/aws-codepipeline-actions/test/test.pipeline.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export = {
365365
"Arn": {
366366
"Ref": "TopicBFC7AF6E"
367367
},
368-
"Id": "Topic"
368+
"Id": "Target0"
369369
}
370370
]
371371
}));

packages/@aws-cdk/aws-config/test/integ.rule.lit.expected.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
"Arn": {
207207
"Ref": "ComplianceTopic0229448B"
208208
},
209-
"Id": "awscdkconfigruleintegComplianceTopic55CAF01A"
209+
"Id": "Target0"
210210
}
211211
]
212212
}
@@ -251,4 +251,4 @@
251251
}
252252
}
253253
}
254-
}
254+
}

packages/@aws-cdk/aws-ecr/test/test.repository.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export = {
294294

295295
repo.onCloudTrailImagePushed('EventRule', {
296296
target: {
297-
bind: () => ({ arn: 'ARN', id: 'ID' })
297+
bind: () => ({ arn: 'ARN', id: '' })
298298
}
299299
});
300300

packages/@aws-cdk/aws-ecs-patterns/test/ec2/integ.scheduled-ecs-task.lit.expected.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@
866866
"Ref": "ScheduledEc2TaskScheduledTaskDef56328BA4"
867867
}
868868
},
869-
"Id": "awsecsintegecsScheduledEc2TaskScheduledTaskDef18FB4348",
869+
"Id": "Target0",
870870
"Input": "{}",
871871
"RoleArn": {
872872
"Fn::GetAtt": [

packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.scheduled-ecs-task.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export = {
3232
TaskCount: 1,
3333
TaskDefinitionArn: { Ref: "ScheduledEc2TaskScheduledTaskDef56328BA4" }
3434
},
35-
Id: "ScheduledEc2TaskScheduledTaskDef1EA607E3",
35+
Id: "Target0",
3636
Input: "{}",
3737
RoleArn: { "Fn::GetAtt": ["ScheduledEc2TaskScheduledTaskDefEventsRole64113C5F", "Arn"] }
3838
}
@@ -98,7 +98,7 @@ export = {
9898
TaskCount: 2,
9999
TaskDefinitionArn: { Ref: "ScheduledEc2TaskScheduledTaskDef56328BA4" }
100100
},
101-
Id: "ScheduledEc2TaskScheduledTaskDef1EA607E3",
101+
Id: "Target0",
102102
Input: "{}",
103103
RoleArn: { "Fn::GetAtt": ["ScheduledEc2TaskScheduledTaskDefEventsRole64113C5F", "Arn"] }
104104
}

packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.scheduled-fargate-task.lit.expected.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
"Ref": "ScheduledFargateTaskScheduledTaskDef521FA675"
213213
}
214214
},
215-
"Id": "awsfargateintegScheduledFargateTaskScheduledTaskDefB0AD4F70",
215+
"Id": "Target0",
216216
"Input": "{}",
217217
"RoleArn": {
218218
"Fn::GetAtt": [
@@ -685,7 +685,7 @@
685685
"Arn"
686686
]
687687
},
688-
"Id": "awsfargateintegScheduledFargateTaskScheduledTaskDefB0AD4F70",
688+
"Id": "Target0",
689689
"EcsParameters": {
690690
"TaskDefinitionArn": {
691691
"Ref": "ScheduledFargateTaskScheduledTaskDef521FA675"
@@ -762,7 +762,7 @@
762762
"Arn"
763763
]
764764
},
765-
"Id": "awsfargateintegScheduledFargateTaskScheduledTaskDefB0AD4F70",
765+
"Id": "Target0",
766766
"EcsParameters": {
767767
"TaskDefinitionArn": {
768768
"Ref": "ScheduledFargateTaskScheduledTaskDef521FA675"

packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.scheduled-fargate-task.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export = {
2929
TaskCount: 1,
3030
TaskDefinitionArn: { Ref: "ScheduledFargateTaskScheduledTaskDef521FA675" }
3131
},
32-
Id: "ScheduledFargateTaskScheduledTaskDef4D131A6E",
32+
Id: "Target0",
3333
Input: "{}",
3434
RoleArn: { "Fn::GetAtt": ["ScheduledFargateTaskScheduledTaskDefEventsRole6CE19522", "Arn"] }
3535
}
@@ -91,7 +91,7 @@ export = {
9191
TaskCount: 2,
9292
TaskDefinitionArn: { Ref: "ScheduledFargateTaskScheduledTaskDef521FA675" }
9393
},
94-
Id: "ScheduledFargateTaskScheduledTaskDef4D131A6E",
94+
Id: "Target0",
9595
Input: "{}",
9696
RoleArn: { "Fn::GetAtt": ["ScheduledFargateTaskScheduledTaskDefEventsRole6CE19522", "Arn"] }
9797
}

packages/@aws-cdk/aws-events-targets/lib/codebuild.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export class CodeBuildProject implements events.IRuleTarget {
1313
/**
1414
* Allows using build projects as event rule targets.
1515
*/
16-
public bind(_rule: events.IRule): events.RuleTargetConfig {
16+
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
1717
return {
18-
id: this.project.node.uniqueId,
18+
id: '',
1919
arn: this.project.projectArn,
2020
role: singletonEventRole(this.project, [new iam.PolicyStatement({
2121
actions: ['codebuild:StartBuild'],

packages/@aws-cdk/aws-events-targets/lib/codepipeline.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export class CodePipeline implements events.IRuleTarget {
1010
constructor(private readonly pipeline: codepipeline.IPipeline) {
1111
}
1212

13-
public bind(_rule: events.IRule): events.RuleTargetConfig {
13+
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
1414
return {
15-
id: this.pipeline.node.uniqueId,
15+
id: '',
1616
arn: this.pipeline.pipelineArn,
1717
role: singletonEventRole(this.pipeline, [new iam.PolicyStatement({
1818
resources: [this.pipeline.pipelineArn],

packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class EcsTask implements events.IRuleTarget {
7777
/**
7878
* Allows using tasks as target of CloudWatch events
7979
*/
80-
public bind(rule: events.IRule): events.RuleTargetConfig {
80+
public bind(rule: events.IRule, id?: string): events.RuleTargetConfig {
8181
const policyStatements = [new iam.PolicyStatement({
8282
actions: ['ecs:RunTask'],
8383
resources: [this.taskDefinition.taskDefinitionArn],
@@ -103,7 +103,6 @@ export class EcsTask implements events.IRuleTarget {
103103
}));
104104
}
105105

106-
const id = this.taskDefinition.node.uniqueId;
107106
const arn = this.cluster.clusterArn;
108107
const role = singletonEventRole(this.taskDefinition, policyStatements);
109108
const containerOverrides = this.props.containerOverrides && this.props.containerOverrides
@@ -148,7 +147,7 @@ export class EcsTask implements events.IRuleTarget {
148147
}
149148
]
150149
},
151-
physicalResourceId: id,
150+
physicalResourceId: this.taskDefinition.node.uniqueId,
152151
},
153152
policyStatements: [ // Cannot use automatic policy statements because we need iam:PassRole
154153
new iam.PolicyStatement({
@@ -164,7 +163,7 @@ export class EcsTask implements events.IRuleTarget {
164163
}
165164

166165
return {
167-
id,
166+
id: '',
168167
arn,
169168
role,
170169
ecsParameters: {

packages/@aws-cdk/aws-events-targets/lib/lambda.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export class LambdaFunction implements events.IRuleTarget {
2828
* Returns a RuleTarget that can be used to trigger this Lambda as a
2929
* result from a CloudWatch event.
3030
*/
31-
public bind(rule: events.IRule): events.RuleTargetConfig {
31+
public bind(rule: events.IRule, _id?: string): events.RuleTargetConfig {
3232
const permissionId = `AllowEventRule${rule.node.uniqueId}`;
33-
if (!this.handler.node.tryFindChild(permissionId)) {
33+
if (!this.handler.permissionsNode.tryFindChild(permissionId)) {
3434
this.handler.addPermission(permissionId, {
3535
action: 'lambda:InvokeFunction',
3636
principal: new iam.ServicePrincipal('events.amazonaws.com'),
@@ -39,7 +39,7 @@ export class LambdaFunction implements events.IRuleTarget {
3939
}
4040

4141
return {
42-
id: this.handler.node.uniqueId,
42+
id: '',
4343
arn: this.handler.functionArn,
4444
input: this.props.event,
4545
};

packages/@aws-cdk/aws-events-targets/lib/sns.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ export class SnsTopic implements events.IRuleTarget {
3434
*
3535
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/resource-based-policies-cwe.html#sns-permissions
3636
*/
37-
public bind(_rule: events.IRule): events.RuleTargetConfig {
37+
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
3838
// deduplicated automatically
3939
this.topic.grantPublish(new iam.ServicePrincipal('events.amazonaws.com'));
4040

4141
return {
42-
id: this.topic.node.uniqueId,
42+
id: '',
4343
arn: this.topic.topicArn,
4444
input: this.props.message,
4545
};

packages/@aws-cdk/aws-events-targets/lib/sqs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class SqsQueue implements events.IRuleTarget {
4848
*
4949
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/resource-based-policies-cwe.html#sqs-permissions
5050
*/
51-
public bind(rule: events.IRule): events.RuleTargetConfig {
51+
public bind(rule: events.IRule, _id?: string): events.RuleTargetConfig {
5252
// deduplicated automatically
5353
this.queue.grantSendMessages(new iam.ServicePrincipal('events.amazonaws.com',
5454
{
@@ -59,7 +59,7 @@ export class SqsQueue implements events.IRuleTarget {
5959
);
6060

6161
const result = {
62-
id: this.queue.node.uniqueId,
62+
id: '',
6363
arn: this.queue.queueArn,
6464
input: this.props.message,
6565
};

packages/@aws-cdk/aws-events-targets/lib/state-machine.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export class SfnStateMachine implements events.IRuleTarget {
2727
*
2828
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/resource-based-policies-cwe.html#sns-permissions
2929
*/
30-
public bind(_rule: events.IRule): events.RuleTargetConfig {
30+
public bind(_rule: events.IRule, _id?: string): events.RuleTargetConfig {
3131
return {
32-
id: this.machine.node.uniqueId,
32+
id: '',
3333
arn: this.machine.stateMachineArn,
3434
role: singletonEventRole(this.machine, [new iam.PolicyStatement({
3535
actions: ['states:StartExecution'],

packages/@aws-cdk/aws-events-targets/test/codebuild/codebuild.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('use codebuild project as an eventrule target', () => {
2525
"Arn"
2626
]
2727
},
28-
Id: "MyProject",
28+
Id: "Target0",
2929
RoleArn: {
3030
"Fn::GetAtt": [
3131
"MyProjectEventsRole5B7D93F5",

0 commit comments

Comments
 (0)