Skip to content

Commit

Permalink
fix(events-targets): allow adding same fargate task to multiple rules (
Browse files Browse the repository at this point in the history
…#3576)

* fix(events-targets): allow adding same scheduled fargate task to multiple rules

Also allow adding same scheduled fargate task multiple times to same rule.

Closes #3574

* create only one security group

* add tests
  • Loading branch information
jogold authored and mergify[bot] committed Aug 19, 2019
1 parent d1ee4ba commit 5b109f9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@
]
}
},
"ScheduledFargateTaskScheduledTaskDefPutTargets7149EAB4": {
"ScheduledFargateTaskScheduledTaskDefScheduledEventRuleTarget0PutTargets4F6BAABA": {
"Type": "Custom::AWS",
"Properties": {
"ServiceToken": {
Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-events-targets/lib/ecs-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}

Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@
]
}
},
"TaskDefPutTargetsF699575F": {
"TaskDefRuleTarget0PutTargets77691785": {
"Type": "Custom::AWS",
"Properties": {
"ServiceToken": {
Expand Down

0 comments on commit 5b109f9

Please sign in to comment.