Skip to content

Commit

Permalink
feat(ecs-patterns): Add support for Docker labels to ECS Patterns (aw…
Browse files Browse the repository at this point in the history
…s#14783)

I have the need to pass docker labels to containers created with the ECS Patterns L3 construct.  There wasn't an easy way to add them after construction and passing an entire TaskDefinition seemed suboptimal.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ABevier authored and hollanddd committed Aug 26, 2021
1 parent 2039b61 commit 5334020
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 6 deletions.
23 changes: 23 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,29 @@ new QueueProcessingFargateService(stack, 'QueueProcessingService', {
});
```

### 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')
}
```
### Select specific load balancer name ApplicationLoadBalancedFargateService
```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,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 };
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down Expand Up @@ -452,4 +459,4 @@ export abstract class NetworkMultipleTargetGroupsServiceBase extends CoreConstru
});
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -151,4 +152,4 @@ export class ApplicationMultipleTargetGroupsEc2Service extends ApplicationMultip
cloudMapOptions: props.cloudMapOptions,
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -151,4 +152,4 @@ export class NetworkMultipleTargetGroupsEc2Service extends NetworkMultipleTarget
cloudMapOptions: props.cloudMapOptions,
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -184,4 +185,4 @@ export class ApplicationMultipleTargetGroupsFargateService extends ApplicationMu
platformVersion: props.platformVersion,
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -184,4 +185,4 @@ export class NetworkMultipleTargetGroupsFargateService extends NetworkMultipleTa
platformVersion: props.platformVersion,
});
}
}
}
12 changes: 11 additions & 1 deletion packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -214,6 +215,10 @@ export = {
Protocol: 'tcp',
},
],
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
},
],
ExecutionRoleArn: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1079,6 +1085,10 @@ export = {
Protocol: 'tcp',
},
],
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
},
],
ExecutionRoleArn: {
Expand Down Expand Up @@ -1532,4 +1542,4 @@ export = {
test.done();
},
},
};
};
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -54,6 +55,10 @@ export = {
},
],
Memory: 1024,
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
},
],
}));
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -416,6 +422,10 @@ export = {
'awslogs-region': { Ref: 'AWS::Region' },
},
},
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
},
],
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -223,6 +224,10 @@ export = {
Protocol: 'tcp',
},
],
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
},
],
Cpu: '256',
Expand Down Expand Up @@ -490,6 +495,7 @@ export = {
taskRole: new Role(stack, 'TaskRole', {
assumedBy: new ServicePrincipal('ecs-tasks.amazonaws.com'),
}),
dockerLabels: { label1: 'labelValue1', label2: 'labelValue2' },
},
cpu: 256,
assignPublicIp: true,
Expand Down Expand Up @@ -594,6 +600,10 @@ export = {
Protocol: 'tcp',
},
],
DockerLabels: {
label1: 'labelValue1',
label2: 'labelValue2',
},
},
],
Cpu: '256',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
};

0 comments on commit 5334020

Please sign in to comment.