Skip to content

Commit

Permalink
feat(ecs-patterns): add listener port as a property for network/appli…
Browse files Browse the repository at this point in the history
…cation load balanced services (#4825)

* - feat(ecs-patterns): allow to specify different NLB listener port other than the default 80(fix #4793)
- feat(ecs-patterns): public facing NLB fronted fargate tasks with assignPublicIp enabled should allow all ipv4 traffic to the ingress port on the fargate task

* - support ApplicationLoadBalancedServiceBase as well
- remove non-related logic to another PR

* minor fix

* minor fix

* minor fix

* add the testing

* add more tests:

✔ setting NLB special listener port to create the listener
✔ setting ALB special listener port to create the listener
✔ setting ALB HTTPS protocol to create the listener on 443
✔ setting ALB HTTP protocol to create the listener on 80
✔ setting ALB without any protocol or listenerPort to create the listener on 80

* minor lint fix

* minor fix
  • Loading branch information
pahud authored and mergify[bot] committed Nov 7, 2019
1 parent b7297fa commit 20b8e5d
Show file tree
Hide file tree
Showing 5 changed files with 1,152 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface ApplicationLoadBalancedServiceBaseProps {
* @default HTTP. If a certificate is specified, the protocol will be
* set by default to HTTPS.
*/
readonly protocol?: ApplicationProtocol;
readonly protocol?: ApplicationProtocol;

/**
* The domain name for the service, e.g. "api.example.com."
Expand Down Expand Up @@ -108,6 +108,13 @@ export interface ApplicationLoadBalancedServiceBaseProps {
*/
readonly loadBalancer?: ApplicationLoadBalancer;

/**
* Listener port of the application load balancer that will serve traffic to the service.
*
* @default 80
*/
readonly listenerPort?: number;

/**
* Specifies whether to propagate the tags from the task definition or the service to the tasks in the service.
* Tags can only be propagated to the tasks within the service during service creation.
Expand Down Expand Up @@ -273,17 +280,19 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct {

this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer : new ApplicationLoadBalancer(this, 'LB', lbProps);

const targetProps = {
port: 80
};

if (props.certificate !== undefined && props.protocol !== undefined && props.protocol !== ApplicationProtocol.HTTPS) {
throw new Error('The HTTPS protocol must be used when a certificate is given');
}
const protocol = props.protocol !== undefined ? props.protocol : (props.certificate ? ApplicationProtocol.HTTPS : ApplicationProtocol.HTTP);

const targetProps = {
port: props.listenerPort !== undefined ? props.listenerPort : 80,
protocol
};

this.listener = this.loadBalancer.addListener('PublicListener', {
protocol,
port: props.listenerPort,
open: true
});
this.targetGroup = this.listener.addTargets('ECS', targetProps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ export interface NetworkLoadBalancedServiceBaseProps {
*/
readonly loadBalancer?: NetworkLoadBalancer;

/**
* Listener port of the network load balancer that will serve traffic to the service.
*
* @default 80
*/
readonly listenerPort?: number;

/**
* Specifies whether to propagate the tags from the task definition or the service to the tasks in the service.
* Tags can only be propagated to the tasks within the service during service creation.
Expand Down Expand Up @@ -245,11 +252,13 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct {

this.loadBalancer = props.loadBalancer !== undefined ? props.loadBalancer : new NetworkLoadBalancer(this, 'LB', lbProps);

const listenerPort = props.listenerPort !== undefined ? props.listenerPort : 80;

const targetProps = {
port: 80
port: listenerPort
};

this.listener = this.loadBalancer.addListener('PublicListener', { port: 80 });
this.listener = this.loadBalancer.addListener('PublicListener', { port: listenerPort });
this.targetGroup = this.listener.addTargets('ECS', targetProps);

if (typeof props.domainName !== 'undefined') {
Expand Down
Loading

0 comments on commit 20b8e5d

Please sign in to comment.