Skip to content

Commit

Permalink
fix(ecs-patterns): feature flag missing for breaking change passing c…
Browse files Browse the repository at this point in the history
…ontainer port for target group port

This re-implements #20284, which was reverted in #20430 due to the feature flag tests.

PR #18157 results in a new TargetGroup being created from NetworkLoadBalancedEc2Service, NetworkLoadBalancedFargateService, NetworkMultipleTargetGroupsEc2Service,
and NetworkMultipleTargerGroupsFargateService even when no change is made because we are now passing through the containerPort props to TargetGroups's Port.

For existing services, this is a breaking change so a feature flag is needed. This PR adds that feature flag.

Closes #19411.
  • Loading branch information
TheRealAmazonKendra committed Jul 6, 2022
1 parent 9c93ba7 commit aaf5162
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { INetworkLoadBalancer, NetworkListener, NetworkLoadBalancer, NetworkTarg
import { IRole } from '@aws-cdk/aws-iam';
import { ARecord, CnameRecord, IHostedZone, RecordTarget } from '@aws-cdk/aws-route53';
import { LoadBalancerTarget } from '@aws-cdk/aws-route53-targets';
import * as cdk from '@aws-cdk/core';
import { CfnOutput, Duration, FeatureFlags, Stack } from '@aws-cdk/core';
import { ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT } from '@aws-cdk/cx-api';
import { Construct } from 'constructs';

/**
Expand Down Expand Up @@ -99,7 +100,7 @@ export interface NetworkLoadBalancedServiceBaseProps {
*
* @default - defaults to 60 seconds if at least one load balancer is in-use and it is not already set
*/
readonly healthCheckGracePeriod?: cdk.Duration;
readonly healthCheckGracePeriod?: Duration;

/**
* The maximum number of tasks, specified as a percentage of the Amazon ECS
Expand Down Expand Up @@ -350,7 +351,7 @@ export abstract class NetworkLoadBalancedServiceBase extends Construct {
const loadBalancer = props.loadBalancer ?? new NetworkLoadBalancer(this, 'LB', lbProps);
const listenerPort = props.listenerPort ?? 80;
const targetProps = {
port: props.taskImageOptions?.containerPort ?? 80,
port: FeatureFlags.of(this).isEnabled(ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT) ? props.taskImageOptions?.containerPort ?? 80 : 80,
};

this.listener = loadBalancer.addListener('PublicListener', { port: listenerPort });
Expand Down Expand Up @@ -387,7 +388,7 @@ export abstract class NetworkLoadBalancedServiceBase extends Construct {
}

if (props.loadBalancer === undefined) {
new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName });
new CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName });
}
}

Expand All @@ -397,7 +398,7 @@ export abstract class NetworkLoadBalancedServiceBase extends Construct {
protected getDefaultCluster(scope: Construct, vpc?: IVpc): Cluster {
// magic string to avoid collision with user-defined constructs
const DEFAULT_CLUSTER_ID = `EcsDefaultClusterMnL3mNNYN${vpc ? vpc.node.id : ''}`;
const stack = cdk.Stack.of(scope);
const stack = Stack.of(scope);
return stack.node.tryFindChild(DEFAULT_CLUSTER_ID) as Cluster || new Cluster(stack, DEFAULT_CLUSTER_ID, { vpc });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { NetworkListener, NetworkLoadBalancer, NetworkTargetGroup } from '@aws-c
import { IRole } from '@aws-cdk/aws-iam';
import { ARecord, IHostedZone, RecordTarget } from '@aws-cdk/aws-route53';
import { LoadBalancerTarget } from '@aws-cdk/aws-route53-targets';
import { CfnOutput, Duration, Stack } from '@aws-cdk/core';
import { CfnOutput, Duration, FeatureFlags, Stack } from '@aws-cdk/core';
import { ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT } from '@aws-cdk/cx-api';
import { Construct } from 'constructs';

/**
Expand Down Expand Up @@ -377,7 +378,7 @@ export abstract class NetworkMultipleTargetGroupsServiceBase extends Construct {
protected registerECSTargets(service: BaseService, container: ContainerDefinition, targets: NetworkTargetProps[]): NetworkTargetGroup {
for (const targetProps of targets) {
const targetGroup = this.findListener(targetProps.listener).addTargets(`ECSTargetGroup${container.containerName}${targetProps.containerPort}`, {
port: targetProps.containerPort ?? 80,
port: FeatureFlags.of(this).isEnabled(ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT) ? targetProps.containerPort ?? 80 : 80,
targets: [
service.loadBalancerTarget({
containerName: container.containerName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Vpc } from '@aws-cdk/aws-ec2';
import * as ecs from '@aws-cdk/aws-ecs';
import { ContainerImage } from '@aws-cdk/aws-ecs';
import { CompositePrincipal, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
import { Duration, Stack } from '@aws-cdk/core';
import { App, Duration, Stack } from '@aws-cdk/core';
import { ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT } from '@aws-cdk/cx-api';
import { ApplicationLoadBalancedFargateService, ApplicationMultipleTargetGroupsFargateService, NetworkLoadBalancedFargateService, NetworkMultipleTargetGroupsFargateService } from '../../lib';


Expand Down Expand Up @@ -771,7 +772,7 @@ describe('When Network Load Balancer', () => {
}).toThrow(/You must specify one of: taskDefinition or image/);
});

test('test Fargate networkloadbalanced construct with custom Port', () => {
test('Fargate neworkloadbalanced construct uses Port 80 for target group when feature flag is not enabled', () => {
// GIVEN
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
Expand All @@ -788,6 +789,37 @@ describe('When Network Load Balancer', () => {
listenerPort: 8181,
});

Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: 'TCP',
TargetType: 'ip',
VpcId: {
Ref: 'VPCB9E5F0B4',
},
});
});

test('Fargate networkloadbalanced construct uses custom Port for target group when feature flag is enabled', () => {
const app = new App({
context: {
[ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT]: true,
},
});
const stack = new Stack(app);
const vpc = new Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

new NetworkLoadBalancedFargateService(stack, 'NLBService', {
cluster: cluster,
memoryLimitMiB: 1024,
cpu: 512,
taskImageOptions: {
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
containerPort: 81,
},
listenerPort: 8181,
});

Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 81,
Protocol: 'TCP',
Expand All @@ -798,8 +830,37 @@ describe('When Network Load Balancer', () => {
});
});

test('test Fargate multinetworkloadbalanced construct with custom Port', () => {
// GIVEN
test('Fargate networkloadbalanced construct uses 80 for target group when feature flag is enabled but container port is not provided', () => {
const app = new App({
context: {
[ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT]: true,
},
});
const stack = new Stack(app);
const vpc = new Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

new NetworkLoadBalancedFargateService(stack, 'NLBService', {
cluster: cluster,
memoryLimitMiB: 1024,
cpu: 512,
taskImageOptions: {
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
listenerPort: 8181,
});

Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: 'TCP',
TargetType: 'ip',
VpcId: {
Ref: 'VPCB9E5F0B4',
},
});
});

test('Fargate multinetworkloadbalanced construct uses Port 80 for target group when feature flag is not enabled', () => {
const stack = new Stack();
const vpc = new Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });
Expand All @@ -812,6 +873,55 @@ describe('When Network Load Balancer', () => {
});


new NetworkMultipleTargetGroupsFargateService(stack, 'NLBService', {
cluster: cluster,
memoryLimitMiB: 1024,
cpu: 512,
taskImageOptions: {
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
loadBalancers: [
{
name: 'lb1',
listeners: [
{ name: 'listener1', port: 8181 },
],
},
],
targetGroups: [{
containerPort: 81,
}],
});

Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: 'TCP',
TargetType: 'ip',
VpcId: {
Ref: 'VPCB9E5F0B4',
},
});
});

test('Fargate multinetworkloadbalanced construct uses custom Port for target group when feature flag is enabled', () => {
// GIVEN
const app = new App({
context: {
[ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT]: true,
},
});
const stack = new Stack(app);
const vpc = new Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

new NetworkMultipleTargetGroupsFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
},
});


new NetworkMultipleTargetGroupsFargateService(stack, 'NLBService', {
cluster: cluster,
memoryLimitMiB: 1024,
Expand Down Expand Up @@ -841,4 +951,50 @@ describe('When Network Load Balancer', () => {
},
});
});

test('Fargate multinetworkloadbalanced construct uses 80 for target group when feature flag is enabled but container port is not provided', () => {
// GIVEN
const app = new App({
context: {
[ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT]: true,
},
});
const stack = new Stack(app);
const vpc = new Vpc(stack, 'VPC');
const cluster = new ecs.Cluster(stack, 'Cluster', { vpc });

new NetworkMultipleTargetGroupsFargateService(stack, 'Service', {
cluster,
taskImageOptions: {
image: ecs.ContainerImage.fromRegistry('test'),
},
});


new NetworkMultipleTargetGroupsFargateService(stack, 'NLBService', {
cluster: cluster,
memoryLimitMiB: 1024,
cpu: 512,
taskImageOptions: {
image: ContainerImage.fromRegistry('amazon/amazon-ecs-sample'),
},
loadBalancers: [
{
name: 'lb1',
listeners: [
{ name: 'listener1', port: 8181 },
],
},
],
});

Template.fromStack(stack).hasResourceProperties('AWS::ElasticLoadBalancingV2::TargetGroup', {
Port: 80,
Protocol: 'TCP',
TargetType: 'ip',
VpcId: {
Ref: 'VPCB9E5F0B4',
},
});
});
});
10 changes: 10 additions & 0 deletions packages/@aws-cdk/cx-api/lib/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ export const CODEPIPELINE_CROSS_ACCOUNT_KEY_ALIAS_STACK_SAFE_RESOURCE_NAME = '@a
*/
export const S3_CREATE_DEFAULT_LOGGING_POLICY = '@aws-cdk/aws-s3:createDefaultLoggingPolicy';

/**
* Enable this feature flag to pass through the `NetworkLoadBalanced<Ec2|Fargate>ServiceProps.taskImageOptions.containerPort`
* and the `NetworkMultipleTargetGroups<Ec2|Fargate>ServiceProps.targetGroups[X].containerPort` to the generated
* `ElasticLoadBalancingV2::TargetGroup`'s `Port` property.
*
* This is a feature flag because updating `Port` causes a replacement of the target groups, which is a breaking change.
*/
export const ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT = '@aws-cdk/aws-ecs-patterns:containerPortToTargetGroupPort';

/**
* Flag values that should apply for new projects
*
Expand Down Expand Up @@ -313,6 +322,7 @@ export const FUTURE_FLAGS: { [key: string]: boolean } = {
[VALIDATE_SNAPSHOT_REMOVAL_POLICY]: true,
[CODEPIPELINE_CROSS_ACCOUNT_KEY_ALIAS_STACK_SAFE_RESOURCE_NAME]: true,
[S3_CREATE_DEFAULT_LOGGING_POLICY]: true,
[ECS_PATTERNS_TARGET_GROUP_PORT_FROM_CONTAINER_PORT]: true,
};

/**
Expand Down

0 comments on commit aaf5162

Please sign in to comment.