Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ export interface NetworkLoadBalancedServiceBaseProps {
*/
readonly loadBalancer?: NetworkLoadBalancer;

/**
* Listener port of the network load balancer that will serve traffic to the service.
*
* [disable-awslint:ref-via-interface]
Comment thread
pahud marked this conversation as resolved.
Outdated
*
* @default - 80
Comment thread
pahud marked this conversation as resolved.
Outdated
*/
readonly listenerPort?: number;
Comment thread
pahud marked this conversation as resolved.

/**
* 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 +254,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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Port } from '@aws-cdk/aws-ec2';
import { FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs';
import { Construct } from '@aws-cdk/core';
import { NetworkLoadBalancedServiceBase, NetworkLoadBalancedServiceBaseProps } from '../base/network-load-balanced-service-base';
Expand Down Expand Up @@ -106,8 +107,8 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic
// Create log driver if logging is enabled
const enableLogging = taskImageOptions.enableLogging !== undefined ? taskImageOptions.enableLogging : true;
const logDriver = taskImageOptions.logDriver !== undefined
? taskImageOptions.logDriver : enableLogging
? this.createAWSLogDriver(this.node.id) : undefined;
? taskImageOptions.logDriver : enableLogging
? this.createAWSLogDriver(this.node.id) : undefined;

const containerName = taskImageOptions.containerName !== undefined ? taskImageOptions.containerName : 'web';
const container = this.taskDefinition.addContainer(containerName, {
Expand All @@ -123,6 +124,10 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic
throw new Error('You must specify one of: taskDefinition or image');
}

// determine the ingress container port
const containerPort = props.taskImageOptions ? props.taskImageOptions.containerPort || 80 :
props.taskDefinition ? props.taskDefinition.defaultContainer!.ingressPort : 80;
Comment thread
pahud marked this conversation as resolved.
Outdated

this.service = new FargateService(this, "Service", {
cluster: this.cluster,
desiredCount: this.desiredCount,
Expand All @@ -135,5 +140,11 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic
cloudMapOptions: props.cloudMapOptions,
});
this.addServiceAsTarget(this.service);

// all public-facing NLB fronted fargate service with assignPublicIp should allow from any IPv4 traffic
const publicNLB = props.publicLoadBalancer !== undefined ? props.publicLoadBalancer : true;
if (publicNLB && props.assignPublicIp) {
this.service.connections.connections.allowFromAnyIpv4(Port.tcp(containerPort));
}
Comment thread
pahud marked this conversation as resolved.
Outdated
}
}
Loading