Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/@aws-cdk/aws-ecs/lib/base/base-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export abstract class BaseService extends Resource

// Open up security groups. For dynamic port mapping, we won't know the port range
// in advance so we need to open up all ports.
const port = targetGroup.targetPort || this.taskDefinition.defaultContainer!.ingressPort;
const port = targetGroup.port || this.taskDefinition.defaultContainer!.ingressPort;
const portRange = port === 0 ? EPHEMERAL_PORT_RANGE : ec2.Port.tcp(port);
targetGroup.registerConnectable(this, portRange);

Expand Down Expand Up @@ -328,7 +328,7 @@ export abstract class BaseService extends Resource
throw new Error("Cannot use a load balancer if NetworkMode is None. Use Bridge, Host or AwsVpc instead.");
}

this.loadBalancers.push(this.makeLoadBalancerProperty(targetGroup.targetGroupArn, targetGroup.targetPort));
this.loadBalancers.push(this.makeLoadBalancerProperty(targetGroup.targetGroupArn, targetGroup.port));

// Service creation can only happen after the load balancer has
// been associated with our target group(s), so add ordering dependency.
Expand All @@ -341,21 +341,21 @@ export abstract class BaseService extends Resource
/**
* Generate a load balancer property given an optional host port
*/
private makeLoadBalancerProperty(targetGroupArn: string, targetPort?: number): CfnService.LoadBalancerProperty {
if (targetPort === undefined) {
private makeLoadBalancerProperty(targetGroupArn: string, port?: number): CfnService.LoadBalancerProperty {
if (port === undefined) {
return {
targetGroupArn,
containerPort: this.taskDefinition.defaultContainer!.containerPort,
containerName: this.taskDefinition.defaultContainer!.containerName,
};
}

const container = this.taskDefinition._findContainerByHostPort(targetPort, Protocol.TCP);
const container = this.taskDefinition._findContainerByHostPort(port, Protocol.TCP);
if (container === undefined) {
throw new Error("Cannot attach a load balancer to an unmapped container port.");
}

const portMapping = container._findPortMapping(targetPort, Protocol.TCP);
const portMapping = container._findPortMapping(port, Protocol.TCP);
return {
targetGroupArn,
containerPort: portMapping!.containerPort,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@
"MinimumHealthyPercent": 50
},
"DesiredCount": 1,
"EnableECSManagedTags": true,
"HealthCheckGracePeriodSeconds": 60,
"LaunchType": "EC2",
"LoadBalancers": [
Expand All @@ -859,9 +860,8 @@
}
}
],
"SchedulingStrategy": "REPLICA",
"PropagateTags": "SERVICE",
"EnableECSManagedTags": true
"SchedulingStrategy": "REPLICA"
},
"DependsOn": [
"LBPublicListenerECSGroupD6A32205",
Expand Down Expand Up @@ -955,7 +955,7 @@
"LBPublicListenerECSGroupD6A32205": {
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"Port": 80,
"Port": 8080,
"Protocol": "HTTP",
"TargetType": "instance",
"VpcId": {
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const service = new ecs.Ec2Service(stack, "Service", {
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc, internetFacing: true });
const listener = lb.addListener('PublicListener', { port: 80, open: true });
listener.addTargets('ECS', {
port: 80,
port: 8080,
targets: [service]
});

Expand Down
28 changes: 6 additions & 22 deletions packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,9 +1025,7 @@ export = {

const lb = new elbv2.ApplicationLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
});
const targetGroup = listener.addTargets("target", { port: 8000 });

// THEN
service.attachToApplicationTargetGroup(targetGroup);
Expand Down Expand Up @@ -1084,10 +1082,7 @@ export = {

const lb = new elbv2.ApplicationLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
targetPort: 8080,
});
const targetGroup = listener.addTargets("target", { port: 8080 });

// THEN
service.attachToApplicationTargetGroup(targetGroup);
Expand Down Expand Up @@ -1124,10 +1119,7 @@ export = {

const lb = new elbv2.ApplicationLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
targetPort: 8080,
});
const targetGroup = listener.addTargets("target", { port: 80 });

// THEN
test.throws(() => {
Expand Down Expand Up @@ -1156,9 +1148,7 @@ export = {

const lb = new elbv2.NetworkLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
});
const targetGroup = listener.addTargets("target", { port: 8000 });

// THEN
service.attachToNetworkTargetGroup(targetGroup);
Expand Down Expand Up @@ -1216,10 +1206,7 @@ export = {

const lb = new elbv2.NetworkLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
targetPort: 8080,
});
const targetGroup = listener.addTargets("target", { port: 8080 });

// THEN
service.attachToNetworkTargetGroup(targetGroup);
Expand Down Expand Up @@ -1253,10 +1240,7 @@ export = {

const lb = new elbv2.NetworkLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
targetPort: 8080,
});
const targetGroup = listener.addTargets("target", { port: 8080 });

// THEN
test.throws(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ export = {
const lb = new elbv2.ApplicationLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
port: 8000,
targets: [service]
});

Expand Down Expand Up @@ -398,7 +398,7 @@ export = {
const lb = new elbv2.ApplicationLoadBalancer(stack, "lb", { vpc });
const listener = lb.addListener("listener", { port: 80 });
const targetGroup = listener.addTargets("target", {
port: 80,
port: 8000,
targets: [service]
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export class ApplicationListener extends BaseListener implements IApplicationLis
stickinessCookieDuration: props.stickinessCookieDuration,
targetGroupName: props.targetGroupName,
targets: props.targets,
targetPort: props.targetPort,
vpc: this.loadBalancer.vpc,
});

Expand Down Expand Up @@ -496,9 +495,9 @@ export interface AddApplicationTargetsProps extends AddRuleProps {
readonly protocol?: ApplicationProtocol;

/**
* The port on which the listener listens for requests.
* The port used to attach to targets.
*
* @default Determined from protocol if known
* @default Determined from target
*/
readonly port?: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
return new ImportedApplicationTargetGroup(scope, id, props);
}

/**
* Port this target group listens on
*
* @default Determined by protocol
*/
public readonly port?: number;

private readonly connectableMembers: ConnectableMember[];
private readonly listeners: IApplicationListener[];
private readonly protocol?: ApplicationProtocol;
private readonly port?: number;

constructor(scope: Construct, id: string, props: ApplicationTargetGroupProps = {}) {
const [protocol, port] = determineProtocolAndPort(props.protocol, props.port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,7 @@ export interface INetworkListener extends IResource {
*/
export interface AddNetworkTargetsProps {
/**
* The port on which the listener listens for requests.
*
* @default Determined from protocol if known
* The port used to attach to targets.
*/
readonly port: number;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ export class NetworkTargetGroup extends TargetGroupBase implements INetworkTarge
this.addTarget(...(props.targets || []));
}

/**
* The port on which the listener listens for requests.
*/
public get port(): number | undefined { return this.defaultPort; }

/**
* Add a load balancing target to this target group
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,11 @@ export interface ITargetGroup extends cdk.IConstruct {
readonly loadBalancerAttached: cdk.IDependable;

/**
* Port target group is listening on
* Port for attaching target group
*
* @default Determined by target
*/
readonly targetPort?: number;
readonly port?: number;
}

/**
Expand Down