@@ -3,7 +3,9 @@ import { Vpc } from '@aws-cdk/aws-ec2';
33import * as ecs from '@aws-cdk/aws-ecs' ;
44import { ContainerImage } from '@aws-cdk/aws-ecs' ;
55import { CompositePrincipal , Role , ServicePrincipal } from '@aws-cdk/aws-iam' ;
6- import { Duration , Stack } from '@aws-cdk/core' ;
6+ import { testFutureBehavior , testLegacyBehavior } from '@aws-cdk/cdk-build-tools' ;
7+ import { App , Duration , Stack } from '@aws-cdk/core' ;
8+ import { ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT } from '@aws-cdk/cx-api' ;
79import { ApplicationLoadBalancedFargateService , ApplicationMultipleTargetGroupsFargateService , NetworkLoadBalancedFargateService , NetworkMultipleTargetGroupsFargateService } from '../../lib' ;
810
911describe ( 'When Application Load Balancer' , ( ) => {
@@ -663,9 +665,36 @@ describe('When Network Load Balancer', () => {
663665 } ) . toThrow ( / Y o u m u s t s p e c i f y o n e o f : t a s k D e f i n i t i o n o r i m a g e / ) ;
664666 } ) ;
665667
666- test ( 'test Fargate networkloadbalanced construct with custom Port', ( ) => {
668+ testLegacyBehavior ( ' Fargate neworkloadbalanced construct uses Port 80 for target group when feature flag is not enabled ', App , ( app ) => {
667669 // GIVEN
668- const stack = new Stack ( ) ;
670+ const stack = new Stack ( app ) ;
671+ const vpc = new Vpc ( stack , 'VPC' ) ;
672+ const cluster = new ecs . Cluster ( stack , 'Cluster' , { vpc } ) ;
673+
674+ new NetworkLoadBalancedFargateService ( stack , 'NLBService' , {
675+ cluster : cluster ,
676+ memoryLimitMiB : 1024 ,
677+ cpu : 512 ,
678+ taskImageOptions : {
679+ image : ContainerImage . fromRegistry ( 'amazon/amazon-ecs-sample' ) ,
680+ containerPort : 81 ,
681+ } ,
682+ listenerPort : 8181 ,
683+ } ) ;
684+
685+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ElasticLoadBalancingV2::TargetGroup' , {
686+ Port : 80 ,
687+ Protocol : 'TCP' ,
688+ TargetType : 'ip' ,
689+ VpcId : {
690+ Ref : 'VPCB9E5F0B4' ,
691+ } ,
692+ } ) ;
693+ } ) ;
694+
695+ testFutureBehavior ( 'Fargate networkloadbalanced construct uses custom Port for target group when feature flag is enabled' , { [ ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT ] : true } , App , ( app ) => {
696+ // GIVEN
697+ const stack = new Stack ( app ) ;
669698 const vpc = new Vpc ( stack , 'VPC' ) ;
670699 const cluster = new ecs . Cluster ( stack , 'Cluster' , { vpc } ) ;
671700
@@ -690,9 +719,79 @@ describe('When Network Load Balancer', () => {
690719 } ) ;
691720 } ) ;
692721
693- test ( 'test Fargate multinetworkloadbalanced construct with custom Port ', ( ) => {
722+ testFutureBehavior ( ' Fargate networkloadbalanced construct uses 80 for target group when feature flag is enabled but container port is not provided ', { [ ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT ] : true } , App , ( app ) => {
694723 // GIVEN
695- const stack = new Stack ( ) ;
724+ const stack = new Stack ( app ) ;
725+ const vpc = new Vpc ( stack , 'VPC' ) ;
726+ const cluster = new ecs . Cluster ( stack , 'Cluster' , { vpc } ) ;
727+
728+ new NetworkLoadBalancedFargateService ( stack , 'NLBService' , {
729+ cluster : cluster ,
730+ memoryLimitMiB : 1024 ,
731+ cpu : 512 ,
732+ taskImageOptions : {
733+ image : ContainerImage . fromRegistry ( 'amazon/amazon-ecs-sample' ) ,
734+ } ,
735+ listenerPort : 8181 ,
736+ } ) ;
737+
738+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ElasticLoadBalancingV2::TargetGroup' , {
739+ Port : 80 ,
740+ Protocol : 'TCP' ,
741+ TargetType : 'ip' ,
742+ VpcId : {
743+ Ref : 'VPCB9E5F0B4' ,
744+ } ,
745+ } ) ;
746+ } ) ;
747+
748+ testLegacyBehavior ( 'Fargate multinetworkloadbalanced construct uses Port 80 for target group when feature flag is not enabled' , App , ( app ) => {
749+ // GIVEN
750+ const stack = new Stack ( app ) ;
751+ const vpc = new Vpc ( stack , 'VPC' ) ;
752+ const cluster = new ecs . Cluster ( stack , 'Cluster' , { vpc } ) ;
753+
754+ new NetworkMultipleTargetGroupsFargateService ( stack , 'Service' , {
755+ cluster,
756+ taskImageOptions : {
757+ image : ecs . ContainerImage . fromRegistry ( 'test' ) ,
758+ } ,
759+ } ) ;
760+
761+
762+ new NetworkMultipleTargetGroupsFargateService ( stack , 'NLBService' , {
763+ cluster : cluster ,
764+ memoryLimitMiB : 1024 ,
765+ cpu : 512 ,
766+ taskImageOptions : {
767+ image : ContainerImage . fromRegistry ( 'amazon/amazon-ecs-sample' ) ,
768+ } ,
769+ loadBalancers : [
770+ {
771+ name : 'lb1' ,
772+ listeners : [
773+ { name : 'listener1' , port : 8181 } ,
774+ ] ,
775+ } ,
776+ ] ,
777+ targetGroups : [ {
778+ containerPort : 81 ,
779+ } ] ,
780+ } ) ;
781+
782+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ElasticLoadBalancingV2::TargetGroup' , {
783+ Port : 80 ,
784+ Protocol : 'TCP' ,
785+ TargetType : 'ip' ,
786+ VpcId : {
787+ Ref : 'VPCB9E5F0B4' ,
788+ } ,
789+ } ) ;
790+ } ) ;
791+
792+ testFutureBehavior ( 'test Fargate multinetworkloadbalanced construct uses custom Port for target group when feature flag is enabled' , { [ ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT ] : true } , App , ( app ) => {
793+ // GIVEN
794+ const stack = new Stack ( app ) ;
696795 const vpc = new Vpc ( stack , 'VPC' ) ;
697796 const cluster = new ecs . Cluster ( stack , 'Cluster' , { vpc } ) ;
698797
@@ -733,4 +832,45 @@ describe('When Network Load Balancer', () => {
733832 } ,
734833 } ) ;
735834 } ) ;
835+
836+ testFutureBehavior ( 'test Fargate multinetworkloadbalanced construct uses 80 for target group when feature flag is enabled but container port is not provided' , { [ ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT ] : true } , App , ( app ) => {
837+ // GIVEN
838+ const stack = new Stack ( app ) ;
839+ const vpc = new Vpc ( stack , 'VPC' ) ;
840+ const cluster = new ecs . Cluster ( stack , 'Cluster' , { vpc } ) ;
841+
842+ new NetworkMultipleTargetGroupsFargateService ( stack , 'Service' , {
843+ cluster,
844+ taskImageOptions : {
845+ image : ecs . ContainerImage . fromRegistry ( 'test' ) ,
846+ } ,
847+ } ) ;
848+
849+
850+ new NetworkMultipleTargetGroupsFargateService ( stack , 'NLBService' , {
851+ cluster : cluster ,
852+ memoryLimitMiB : 1024 ,
853+ cpu : 512 ,
854+ taskImageOptions : {
855+ image : ContainerImage . fromRegistry ( 'amazon/amazon-ecs-sample' ) ,
856+ } ,
857+ loadBalancers : [
858+ {
859+ name : 'lb1' ,
860+ listeners : [
861+ { name : 'listener1' , port : 8181 } ,
862+ ] ,
863+ } ,
864+ ] ,
865+ } ) ;
866+
867+ Template . fromStack ( stack ) . hasResourceProperties ( 'AWS::ElasticLoadBalancingV2::TargetGroup' , {
868+ Port : 80 ,
869+ Protocol : 'TCP' ,
870+ TargetType : 'ip' ,
871+ VpcId : {
872+ Ref : 'VPCB9E5F0B4' ,
873+ } ,
874+ } ) ;
875+ } ) ;
736876} ) ;
0 commit comments