Skip to content

Commit 8bbc7e6

Browse files
authored
fix(events): fix ECS target in Isolated subnet (#3786)
ECS targets used to request `assignPublicIp: true` in `Isolated` subnets, which obviously is not going to work. Make sure that it will only use request IP addresses in Public subnets.
1 parent 013cab6 commit 8bbc7e6

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class EcsTask implements events.IRuleTarget {
116116
// when using awsvpc network mode.
117117
if (this.taskDefinition.networkMode === ecs.NetworkMode.AWS_VPC) {
118118
const subnetSelection = this.props.subnetSelection || { subnetType: ec2.SubnetType.PRIVATE };
119-
const assignPublicIp = subnetSelection.subnetType === ec2.SubnetType.PRIVATE ? 'DISABLED' : 'ENABLED';
119+
const assignPublicIp = subnetSelection.subnetType === ec2.SubnetType.PUBLIC ? 'ENABLED' : 'DISABLED';
120120

121121
new custom.AwsCustomResource(this.taskDefinition, `${rule.node.id}${id}PutTargets`, {
122122
// `onCreate´ defaults to `onUpdate` and we don't need an `onDelete` here

packages/@aws-cdk/aws-events-targets/test/ecs/event-rule-target.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,60 @@ test("Can use same fargate taskdef multiple times in a rule", () => {
251251
}]
252252
}))).not.toThrow();
253253
});
254+
255+
test("Isolated subnet does not have AssignPublicIp=true", () => {
256+
// GIVEN
257+
const stack = new cdk.Stack();
258+
const vpc = new ec2.Vpc(stack, 'Vpc', {
259+
maxAzs: 1,
260+
subnetConfiguration: [{
261+
subnetType: ec2.SubnetType.ISOLATED,
262+
name: 'Isolated'
263+
}]
264+
});
265+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
266+
267+
const taskDefinition = new ecs.FargateTaskDefinition(stack, 'TaskDef');
268+
taskDefinition.addContainer('TheContainer', {
269+
image: ecs.ContainerImage.fromRegistry('henk'),
270+
});
271+
272+
const rule = new events.Rule(stack, 'Rule', {
273+
schedule: events.Schedule.expression('rate(1 min)')
274+
});
275+
276+
// WHEN
277+
rule.addTarget(new targets.EcsTask({
278+
cluster,
279+
taskDefinition,
280+
taskCount: 1,
281+
subnetSelection: { subnetType: ec2.SubnetType.ISOLATED },
282+
containerOverrides: [{
283+
containerName: 'TheContainer',
284+
command: ['echo', 'yay'],
285+
}]
286+
}));
287+
288+
// THEN
289+
expect(stack).toHaveResourceLike('Custom::AWS', {
290+
Update: {
291+
service: "CloudWatchEvents",
292+
apiVersion: "2015-10-07",
293+
action: "putTargets",
294+
parameters: {
295+
Targets: [
296+
{
297+
EcsParameters: {
298+
LaunchType: "FARGATE",
299+
NetworkConfiguration: {
300+
awsvpcConfiguration: {
301+
AssignPublicIp: "DISABLED",
302+
}
303+
},
304+
},
305+
}
306+
]
307+
}
308+
}
309+
});
310+
});

0 commit comments

Comments
 (0)