diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.scheduled-fargate-task.lit.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.scheduled-fargate-task.lit.expected.json index f1eac0a73c85c..981eb3df2d63f 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.scheduled-fargate-task.lit.expected.json +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.scheduled-fargate-task.lit.expected.json @@ -602,7 +602,7 @@ ] } }, - "ScheduledFargateTaskScheduledTaskDefPutTargets7149EAB4": { + "ScheduledFargateTaskScheduledTaskDefScheduledEventRuleTarget0PutTargets4F6BAABA": { "Type": "Custom::AWS", "Properties": { "ServiceToken": { diff --git a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts index 67aff8edd376b..82735b8378675 100644 --- a/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts +++ b/packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts @@ -70,7 +70,8 @@ export class EcsTask implements events.IRuleTarget { this.taskCount = props.taskCount !== undefined ? props.taskCount : 1; if (this.taskDefinition.networkMode === ecs.NetworkMode.AWS_VPC) { - this.securityGroup = props.securityGroup || new ec2.SecurityGroup(this.taskDefinition, 'SecurityGroup', { vpc: this.props.cluster.vpc }); + const securityGroup = props.securityGroup || this.taskDefinition.node.tryFindChild('SecurityGroup') as ec2.ISecurityGroup; + this.securityGroup = securityGroup || new ec2.SecurityGroup(this.taskDefinition, 'SecurityGroup', { vpc: this.props.cluster.vpc }); } } @@ -117,7 +118,7 @@ export class EcsTask implements events.IRuleTarget { const subnetSelection = this.props.subnetSelection || { subnetType: ec2.SubnetType.PRIVATE }; const assignPublicIp = subnetSelection.subnetType === ec2.SubnetType.PRIVATE ? 'DISABLED' : 'ENABLED'; - new custom.AwsCustomResource(this.taskDefinition, 'PutTargets', { + new custom.AwsCustomResource(this.taskDefinition, `${rule.node.id}${id}PutTargets`, { // `onCreate´ defaults to `onUpdate` and we don't need an `onDelete` here // because the rule/target will be owned by CF anyway. onUpdate: { diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts index 3184ce249da5c..d7758bdefebff 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts @@ -184,5 +184,70 @@ test("Can use Fargate taskdef as EventRule target", () => { } ] }); +}); + +test("Can use same fargate taskdef with multiple rules", () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 1 }); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); + taskDefinition.addContainer('TheContainer', { + image: ecs.ContainerImage.fromRegistry('henk'), + }); + + const scheduledRule = new events.Rule(stack, 'ScheduleRule', { + schedule: events.Schedule.expression('rate(1 min)') + }); + + const patternRule = new events.Rule(stack, 'PatternRule', { + eventPattern: { + detail: ['test'] + } + }); + + scheduledRule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition, + })); + + expect(() => patternRule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition + }))).not.toThrow(); +}); + +test("Can use same fargate taskdef multiple times in a rule", () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 1 }); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef'); + taskDefinition.addContainer('TheContainer', { + image: ecs.ContainerImage.fromRegistry('henk'), + }); + + const rule = new events.Rule(stack, 'ScheduleRule', { + schedule: events.Schedule.expression('rate(1 min)') + }); + + rule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition, + containerOverrides: [{ + containerName: 'TheContainer', + command: ['echo', events.EventField.fromPath('$.detail.a')], + }] + })); + + expect(() => rule.addTarget(new targets.EcsTask({ + cluster, + taskDefinition, + containerOverrides: [{ + containerName: 'TheContainer', + command: ['echo', events.EventField.fromPath('$.detail.b')], + }] + }))).not.toThrow(); }); diff --git a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json index 8ff9da4e62423..ffdf409e2f7ec 100644 --- a/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json +++ b/packages/@aws-cdk/aws-events-targets/test/ecs/integ.event-fargate-task.expected.json @@ -565,7 +565,7 @@ ] } }, - "TaskDefPutTargetsF699575F": { + "TaskDefRuleTarget0PutTargets77691785": { "Type": "Custom::AWS", "Properties": { "ServiceToken": {