From 4a711aaac39f1a37d12684b79a315d51ecd2cc39 Mon Sep 17 00:00:00 2001 From: Alan Bevier Date: Wed, 19 May 2021 17:04:17 -0400 Subject: [PATCH 1/2] Add support for Docker labels to ECS Patterns --- .../application-load-balanced-service-base.ts | 7 +++ ...ion-multiple-target-groups-service-base.ts | 7 +++ .../network-load-balanced-service-base.ts | 7 +++ ...ork-multiple-target-groups-service-base.ts | 9 ++- .../application-load-balanced-ecs-service.ts | 1 + ...tion-multiple-target-groups-ecs-service.ts | 3 +- .../ecs/network-load-balanced-ecs-service.ts | 1 + ...work-multiple-target-groups-ecs-service.ts | 3 +- ...plication-load-balanced-fargate-service.ts | 1 + ...-multiple-target-groups-fargate-service.ts | 3 +- .../network-load-balanced-fargate-service.ts | 1 + ...-multiple-target-groups-fargate-service.ts | 3 +- .../aws-ecs-patterns/test/ec2/test.l3s-v2.ts | 12 +++- .../aws-ecs-patterns/test/ec2/test.l3s.ts | 10 +++ .../test.load-balanced-fargate-service-v2.ts | 12 +++- .../test.load-balanced-fargate-service.ts | 61 +++++++++++++++++++ 16 files changed, 134 insertions(+), 7 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts index cc0bbfc3427de..0a9606dba0750 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts @@ -325,6 +325,13 @@ export interface ApplicationLoadBalancedTaskImageOptions { * @default - Automatically generated name. */ readonly family?: string; + + /** + * A key/value map of labels to add to the container. + * + * @default - No labels. + */ + readonly dockerLabels?: { [key: string]: string }; } /** diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-multiple-target-groups-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-multiple-target-groups-service-base.ts index ff3fc675fcd69..9d9c21c9e7b5e 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-multiple-target-groups-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-multiple-target-groups-service-base.ts @@ -188,6 +188,13 @@ export interface ApplicationLoadBalancedTaskImageProps { * @default - Automatically generated name. */ readonly family?: string; + + /** + * A key/value map of labels to add to the container. + * + * @default - No labels. + */ + readonly dockerLabels?: { [key: string]: string }; } /** diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts index 5872217a43f22..0857b99ee8cfd 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts @@ -267,6 +267,13 @@ export interface NetworkLoadBalancedTaskImageOptions { * @default - Automatically generated name. */ readonly family?: string; + + /** + * A key/value map of labels to add to the container. + * + * @default - No labels. + */ + readonly dockerLabels?: { [key: string]: string }; } /** diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-multiple-target-groups-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-multiple-target-groups-service-base.ts index 60fd9904b8078..8eb751b07d13e 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-multiple-target-groups-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-multiple-target-groups-service-base.ts @@ -186,6 +186,13 @@ export interface NetworkLoadBalancedTaskImageProps { * @default - Automatically generated name. */ readonly family?: string; + + /** + * A key/value map of labels to add to the container. + * + * @default - No labels. + */ + readonly dockerLabels?: { [key: string]: string }; } /** @@ -452,4 +459,4 @@ export abstract class NetworkMultipleTargetGroupsServiceBase extends CoreConstru }); } } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts index ed606bb72c907..6d1809a68d18b 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts @@ -110,6 +110,7 @@ export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedSe environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, logging: logDriver, + dockerLabels: taskImageOptions.dockerLabels, }); container.addPortMappings({ containerPort: taskImageOptions.containerPort || 80, diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-multiple-target-groups-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-multiple-target-groups-ecs-service.ts index 90f4afdd5fa8f..f77e7e4dccdc3 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-multiple-target-groups-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-multiple-target-groups-ecs-service.ts @@ -104,6 +104,7 @@ export class ApplicationMultipleTargetGroupsEc2Service extends ApplicationMultip environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, logging: this.logDriver, + dockerLabels: taskImageOptions.dockerLabels, }); if (taskImageOptions.containerPorts) { for (const containerPort of taskImageOptions.containerPorts) { @@ -151,4 +152,4 @@ export class ApplicationMultipleTargetGroupsEc2Service extends ApplicationMultip cloudMapOptions: props.cloudMapOptions, }); } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts index 4bae918ca67fc..b8862dfbed338 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts @@ -108,6 +108,7 @@ export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBas environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, logging: logDriver, + dockerLabels: taskImageOptions.dockerLabels, }); container.addPortMappings({ containerPort: taskImageOptions.containerPort || 80, diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-multiple-target-groups-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-multiple-target-groups-ecs-service.ts index f0d3b0a1571ce..12d5b25ce67fd 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-multiple-target-groups-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-multiple-target-groups-ecs-service.ts @@ -103,6 +103,7 @@ export class NetworkMultipleTargetGroupsEc2Service extends NetworkMultipleTarget environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, logging: this.logDriver, + dockerLabels: taskImageOptions.dockerLabels, }); if (taskImageOptions.containerPorts) { for (const containerPort of taskImageOptions.containerPorts) { @@ -151,4 +152,4 @@ export class NetworkMultipleTargetGroupsEc2Service extends NetworkMultipleTarget cloudMapOptions: props.cloudMapOptions, }); } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts index 326a68529272b..19a2501355223 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts @@ -146,6 +146,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc logging: logDriver, environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, + dockerLabels: taskImageOptions.dockerLabels, }); container.addPortMappings({ containerPort: taskImageOptions.containerPort || 80, diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts index 6759e8e001376..8052e0483b16a 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-multiple-target-groups-fargate-service.ts @@ -136,6 +136,7 @@ export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMu logging: this.logDriver, environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, + dockerLabels: taskImageOptions.dockerLabels, }); if (taskImageOptions.containerPorts) { for (const containerPort of taskImageOptions.containerPorts) { @@ -184,4 +185,4 @@ export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMu platformVersion: props.platformVersion, }); } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts index 1f2618bbca314..9095be5cf2cd1 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts @@ -133,6 +133,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic logging: logDriver, environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, + dockerLabels: taskImageOptions.dockerLabels, }); container.addPortMappings({ containerPort: taskImageOptions.containerPort || 80, diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts index 4a4974af7cce3..1a185f642a558 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-multiple-target-groups-fargate-service.ts @@ -136,6 +136,7 @@ export class NetworkMultipleTargetGroupsFargateService extends NetworkMultipleTa logging: this.logDriver, environment: taskImageOptions.environment, secrets: taskImageOptions.secrets, + dockerLabels: taskImageOptions.dockerLabels, }); if (taskImageOptions.containerPorts) { for (const containerPort of taskImageOptions.containerPorts) { @@ -184,4 +185,4 @@ export class NetworkMultipleTargetGroupsFargateService extends NetworkMultipleTa platformVersion: props.platformVersion, }); } -} \ No newline at end of file +} diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s-v2.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s-v2.ts index 77f171425c88a..0946123907fe2 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s-v2.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s-v2.ts @@ -107,6 +107,7 @@ export = { taskRole: new Role(stack, 'TaskRole', { assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'), }), + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, }, cpu: 256, desiredCount: 3, @@ -214,6 +215,10 @@ export = { Protocol: 'tcp', }, ], + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, }, ], ExecutionRoleArn: { @@ -967,6 +972,7 @@ export = { taskRole: new Role(stack, 'TaskRole', { assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'), }), + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, }, cpu: 256, desiredCount: 3, @@ -1079,6 +1085,10 @@ export = { Protocol: 'tcp', }, ], + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, }, ], ExecutionRoleArn: { @@ -1532,4 +1542,4 @@ export = { test.done(); }, }, -}; \ No newline at end of file +}; diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts index a028d34c22157..5ef253d71bdb1 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts @@ -28,6 +28,7 @@ export = { TEST_ENVIRONMENT_VARIABLE1: 'test environment variable 1 value', TEST_ENVIRONMENT_VARIABLE2: 'test environment variable 2 value', }, + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, }, desiredCount: 2, }); @@ -54,6 +55,10 @@ export = { }, ], Memory: 1024, + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, }, ], })); @@ -389,6 +394,7 @@ export = { TEST_ENVIRONMENT_VARIABLE1: 'test environment variable 1 value', TEST_ENVIRONMENT_VARIABLE2: 'test environment variable 2 value', }, + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, }, desiredCount: 2, }); @@ -416,6 +422,10 @@ export = { 'awslogs-region': { Ref: 'AWS::Region' }, }, }, + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, }, ], })); diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts index 81f1711c961c0..51e3c09906210 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service-v2.ts @@ -114,6 +114,7 @@ export = { taskRole: new Role(stack, 'TaskRole', { assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'), }), + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, }, cpu: 256, assignPublicIp: true, @@ -223,6 +224,10 @@ export = { Protocol: 'tcp', }, ], + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, }, ], Cpu: '256', @@ -413,6 +418,7 @@ export = { taskRole: new Role(stack, 'TaskRole', { assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'), }), + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, }, cpu: 256, assignPublicIp: true, @@ -517,6 +523,10 @@ export = { Protocol: 'tcp', }, ], + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, }, ], Cpu: '256', @@ -599,4 +609,4 @@ export = { test.done(); }, }, -}; \ No newline at end of file +}; diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts index a8c588d1187ef..b0f8d0d573d7f 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts @@ -1012,4 +1012,65 @@ export = { }, + 'test ALB load balanced service with docker labels defined'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + + // WHEN + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { + cluster, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'), + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, + }, + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + Image: '/aws/aws-example-app', + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, + }, + ], + })); + + test.done(); + }, + + 'test Network load balanced service with docker labels defined'(test: Test) { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'VPC'); + const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); + + // WHEN + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { + cluster, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'), + dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' }, + }, + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + Image: '/aws/aws-example-app', + DockerLabels: { + label1: 'labelValue1', + label2: 'labelValue2', + }, + }, + ], + })); + + test.done(); + }, }; From c678f2de5188e330bdade47b2caee7ad2d00c6aa Mon Sep 17 00:00:00 2001 From: Alan Bevier Date: Mon, 7 Jun 2021 09:19:44 -0400 Subject: [PATCH 2/2] update README --- packages/@aws-cdk/aws-ecs-patterns/README.md | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index 2293c129ee245..0e49a7ae12cd2 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -523,3 +523,26 @@ new QueueProcessingFargateService(stack, 'QueueProcessingService', { image: new ecs.AssetImage(path.join(__dirname, '..', 'sqs-reader')), }); ``` + +### Deploy application and metrics sidecar + +The following is an example of deploying an application along with a metrics sidecar container that utilizes `dockerLabels` for discovery: + +```ts +const service = new ApplicationLoadBalancedFargateService(stack, 'Service', { + cluster, + vpc, + desiredCount: 1, + taskImageOptions: { + image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), + }, + dockerLabels: { + 'application.label.one': 'first_label' + 'application.label.two': 'second_label' + } +}); + +service.taskDefinition.addContainer('Sidecar', { + image: ContainerImage.fromRegistry('example/metrics-sidecar') +} +```