From 32c6848ec97553c24fad857e86945b5b11d0cf20 Mon Sep 17 00:00:00 2001 From: "min.y" Date: Mon, 12 Oct 2026 10:54:16 +1300 Subject: [PATCH 1/2] feat: add containerPort to FargateService --- dist/fargate.d.ts | 2 ++ dist/fargate.js | 8 ++++++-- src/fargate.ts | 9 ++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/dist/fargate.d.ts b/dist/fargate.d.ts index 55fe0b6..cf26bd9 100644 --- a/dist/fargate.d.ts +++ b/dist/fargate.d.ts @@ -28,6 +28,8 @@ export type EnvFactory = (stack: StackConfig, defaults: Record) interface FargateServiceProps extends cdk.NestedStackProps { subDomainIncludingDot?: string; healthCheckPath?: string; + /** Container port the task listens on (ALB target group + task port mapping). Default 80. */ + containerPort?: number; stack: StackConfig; cluster: ecs.ICluster; certificate: acm.ICertificate; diff --git a/dist/fargate.js b/dist/fargate.js index 695a918..ef45186 100644 --- a/dist/fargate.js +++ b/dist/fargate.js @@ -58,7 +58,7 @@ const DEFAULT_IMAGE = 'nginxdemos/hello:latest'; class FargateService extends cdk.NestedStack { constructor(scope, id, props) { super(scope, id, props); - const { healthCheckPath = '/health-check', imageVersion = 'latest', subDomainIncludingDot = '', stack, cluster, certificate, zone, repository, taskConfiguration, securityGroup: defaultSecurityGroup, } = props; + const { healthCheckPath = '/health-check', containerPort = 80, imageVersion = 'latest', subDomainIncludingDot = '', stack, cluster, certificate, zone, repository, taskConfiguration, securityGroup: defaultSecurityGroup, } = props; // Compile secrets into list of mapped ecs.Secrets const secrets = {}; const secretValues = taskConfiguration.secrets; @@ -80,7 +80,10 @@ class FargateService extends cdk.NestedStack { // Scaffold default security group if not provided let securityGroup = defaultSecurityGroup; if (!securityGroup) { - const groupStack = new security_group_1.SecurityGroup(this, stack.getResourceID('SecurityGroup'), { vpc: cluster.vpc }); + const groupStack = new security_group_1.SecurityGroup(this, stack.getResourceID('SecurityGroup'), { + vpc: cluster.vpc, + incomingPorts: [...new Set([80, 443, containerPort])], + }); securityGroup = groupStack.securityGroup; } const desiredCount = taskConfiguration?.desiredCount || 1; @@ -95,6 +98,7 @@ class FargateService extends cdk.NestedStack { desiredCount, taskImageOptions: { image, + containerPort, environment: taskConfiguration?.environment || {}, secrets, }, diff --git a/src/fargate.ts b/src/fargate.ts index 71c83d8..a31f636 100644 --- a/src/fargate.ts +++ b/src/fargate.ts @@ -43,6 +43,8 @@ export type EnvFactory = ( interface FargateServiceProps extends cdk.NestedStackProps { subDomainIncludingDot?: string healthCheckPath?: string + /** Container port the task listens on (ALB target group + task port mapping). Default 80. */ + containerPort?: number stack: StackConfig cluster: ecs.ICluster certificate: acm.ICertificate @@ -71,6 +73,7 @@ export class FargateService extends cdk.NestedStack { const { healthCheckPath = '/health-check', + containerPort = 80, imageVersion = 'latest', subDomainIncludingDot = '', stack, @@ -113,7 +116,10 @@ export class FargateService extends cdk.NestedStack { const groupStack = new SecurityGroup( this, stack.getResourceID('SecurityGroup'), - { vpc: cluster.vpc }, + { + vpc: cluster.vpc, + incomingPorts: [...new Set([80, 443, containerPort])], + }, ) securityGroup = groupStack.securityGroup } @@ -134,6 +140,7 @@ export class FargateService extends cdk.NestedStack { desiredCount, taskImageOptions: { image, + containerPort, environment: taskConfiguration?.environment || {}, secrets, }, From 053be6ef48e05d925d6f29b38a4a204cb0553165 Mon Sep 17 00:00:00 2001 From: "min.y" Date: Mon, 12 Oct 2026 15:39:43 +1300 Subject: [PATCH 2/2] fix: do not expose containerPort on public Fargate security group --- dist/fargate.js | 5 +---- src/fargate.ts | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/dist/fargate.js b/dist/fargate.js index ef45186..bab28dd 100644 --- a/dist/fargate.js +++ b/dist/fargate.js @@ -80,10 +80,7 @@ class FargateService extends cdk.NestedStack { // Scaffold default security group if not provided let securityGroup = defaultSecurityGroup; if (!securityGroup) { - const groupStack = new security_group_1.SecurityGroup(this, stack.getResourceID('SecurityGroup'), { - vpc: cluster.vpc, - incomingPorts: [...new Set([80, 443, containerPort])], - }); + const groupStack = new security_group_1.SecurityGroup(this, stack.getResourceID('SecurityGroup'), { vpc: cluster.vpc }); securityGroup = groupStack.securityGroup; } const desiredCount = taskConfiguration?.desiredCount || 1; diff --git a/src/fargate.ts b/src/fargate.ts index a31f636..b5092d4 100644 --- a/src/fargate.ts +++ b/src/fargate.ts @@ -116,10 +116,7 @@ export class FargateService extends cdk.NestedStack { const groupStack = new SecurityGroup( this, stack.getResourceID('SecurityGroup'), - { - vpc: cluster.vpc, - incomingPorts: [...new Set([80, 443, containerPort])], - }, + { vpc: cluster.vpc }, ) securityGroup = groupStack.securityGroup }