Skip to content

Commit f901313

Browse files
piradeepkrix0rrr
authored andcommitted
fix(aws-ecs): move high level ECS constructs into aws-ecs-patterns (#2623)
Refactor all high level constructs from the aws-ecs module into the aws-ecs-patterns module as discussed offline. BREAKING CHANGE: These changes move all L3 and higher constructs out of the aws-ecs module into the aws-ecs-patterns module. The following constructs have been moved into the aws-ecs-patterns module: `EcsQueueWorkerService`, `FargateQueueWorkerService`, `LoadBalancedEcsService`, `LoadBalancedFargateService` and `LoadBalancedFargateServiceApplets`.
1 parent 8904c3e commit f901313

28 files changed

+202
-86
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/@aws-cdk/aws-ecs-patterns/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,82 @@
22

33
This library provides higher-level ECS constructs which follow common architectural patterns. It contains:
44

5+
* Load Balanced Services
6+
* Queue Worker Services
57
* Scheduled Tasks (cron jobs)
68

9+
## Load Balanced Services
10+
11+
To define a service that is behind a load balancer, instantiate one of the following:
12+
13+
* `LoadBalancedEc2Service`
14+
15+
```ts
16+
const loadBalancedEcsService = new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', {
17+
cluster,
18+
memoryLimitMiB: 1024,
19+
image: ecs.ContainerImage.fromRegistry('test'),
20+
desiredCount: 2,
21+
environment: {
22+
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
23+
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
24+
}
25+
});
26+
```
27+
28+
* `LoadBalancedFargateService`
29+
30+
```ts
31+
const loadBalancedFargateService = new ecsPatterns.LoadBalancedFargateService(stack, 'Service', {
32+
cluster,
33+
memoryMiB: '1GB',
34+
cpu: '512',
35+
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"),
36+
});
37+
```
38+
39+
## Queue Worker Services
40+
41+
To define a service that creates a queue and reads from that queue, instantiate one of the following:
42+
43+
* `Ec2QueueWorkerService`
44+
45+
```ts
46+
const ecsQueueWorkerService = new Ec2QueueWorkerService(stack, 'Service', {
47+
cluster,
48+
memoryLimitMiB: 1024,
49+
image: ecs.ContainerImage.fromRegistry('test'),
50+
command: ["-c", "4", "amazon.com"],
51+
enableLogging: false,
52+
desiredTaskCount: 2,
53+
environment: {
54+
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
55+
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
56+
},
57+
queue,
58+
maxScalingCapacity: 5
59+
});
60+
```
61+
62+
* `FargateQueueWorkerService`
63+
64+
```ts
65+
const fargateQueueWorkerService = new FargateQueueWorkerService(stack, 'Service', {
66+
cluster,
67+
memoryMiB: '512',
68+
image: ecs.ContainerImage.fromRegistry('test'),
69+
command: ["-c", "4", "amazon.com"],
70+
enableLogging: false,
71+
desiredTaskCount: 2,
72+
environment: {
73+
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
74+
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
75+
},
76+
queue,
77+
maxScalingCapacity: 5
78+
});
79+
```
80+
781
## Scheduled Tasks
882

983
To define a task that runs periodically, instantiate an `ScheduledEc2Task`:

packages/@aws-cdk/aws-ecs/lib/load-balanced-service-base.ts renamed to packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { ICertificate } from '@aws-cdk/aws-certificatemanager';
2+
import ecs = require('@aws-cdk/aws-ecs');
23
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
34
import cdk = require('@aws-cdk/cdk');
4-
import { BaseService } from './base/base-service';
5-
import { ICluster } from './cluster';
6-
import { ContainerImage } from './container-image';
75

86
export enum LoadBalancerType {
97
Application,
@@ -14,12 +12,12 @@ export interface LoadBalancedServiceBaseProps {
1412
/**
1513
* The cluster where your service will be deployed
1614
*/
17-
readonly cluster: ICluster;
15+
readonly cluster: ecs.ICluster;
1816

1917
/**
2018
* The image to start.
2119
*/
22-
readonly image: ContainerImage;
20+
readonly image: ecs.ContainerImage;
2321

2422
/**
2523
* The container port of the application load balancer attached to your Fargate service. Corresponds to container port mapping.
@@ -127,7 +125,7 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct {
127125
new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName });
128126
}
129127

130-
protected addServiceAsTarget(service: BaseService) {
128+
protected addServiceAsTarget(service: ecs.BaseService) {
131129
if (this.loadBalancerType === LoadBalancerType.Application) {
132130
(this.targetGroup as elbv2.ApplicationTargetGroup).addTarget(service);
133131
} else {

packages/@aws-cdk/aws-ecs/lib/queue-worker-service-base.ts renamed to packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-worker-service-base.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import autoscaling = require('@aws-cdk/aws-applicationautoscaling');
2+
import ecs = require('@aws-cdk/aws-ecs');
23
import sqs = require('@aws-cdk/aws-sqs');
3-
import { IQueue } from '@aws-cdk/aws-sqs';
44
import cdk = require('@aws-cdk/cdk');
5-
import { BaseService } from './base/base-service';
6-
import { ICluster } from './cluster';
7-
import { ContainerImage } from './container-image';
8-
import { AwsLogDriver } from './log-drivers/aws-log-driver';
9-
import { LogDriver } from './log-drivers/log-driver';
105

116
/**
127
* Properties to define a Query Worker service
@@ -15,12 +10,12 @@ export interface QueueWorkerServiceBaseProps {
1510
/**
1611
* Cluster where service will be deployed
1712
*/
18-
readonly cluster: ICluster;
13+
readonly cluster: ecs.ICluster;
1914

2015
/**
2116
* The image to start.
2217
*/
23-
readonly image: ContainerImage;
18+
readonly image: ecs.ContainerImage;
2419

2520
/**
2621
* The CMD value to pass to the container. A string with commands delimited by commas.
@@ -58,7 +53,7 @@ export interface QueueWorkerServiceBaseProps {
5853
*
5954
* @default 'SQSQueue with CloudFormation-generated name'
6055
*/
61-
readonly queue?: IQueue;
56+
readonly queue?: sqs.IQueue;
6257

6358
/**
6459
* Maximum capacity to scale to.
@@ -85,7 +80,7 @@ export abstract class QueueWorkerServiceBase extends cdk.Construct {
8580
/**
8681
* The SQS queue that the worker service will process from
8782
*/
88-
public readonly sqsQueue: IQueue;
83+
public readonly sqsQueue: sqs.IQueue;
8984

9085
// Properties that have defaults defined. The Queue Worker will handle assigning undefined properties with default
9186
// values so that derived classes do not need to maintain the same logic.
@@ -109,7 +104,7 @@ export abstract class QueueWorkerServiceBase extends cdk.Construct {
109104
/**
110105
* The AwsLogDriver to use for logging if logging is enabled.
111106
*/
112-
public readonly logDriver?: LogDriver;
107+
public readonly logDriver?: ecs.LogDriver;
113108

114109
constructor(scope: cdk.Construct, id: string, props: QueueWorkerServiceBaseProps) {
115110
super(scope, id);
@@ -141,7 +136,7 @@ export abstract class QueueWorkerServiceBase extends cdk.Construct {
141136
*
142137
* @param service the ECS/Fargate service for which to apply the autoscaling rules to
143138
*/
144-
protected configureAutoscalingForService(service: BaseService) {
139+
protected configureAutoscalingForService(service: ecs.BaseService) {
145140
const scalingTarget = service.autoScaleTaskCount({ maxCapacity: this.maxCapacity, minCapacity: this.desiredCount });
146141
scalingTarget.scaleOnCpuUtilization('CpuScaling', {
147142
targetUtilizationPercent: 50,
@@ -157,7 +152,7 @@ export abstract class QueueWorkerServiceBase extends cdk.Construct {
157152
*
158153
* @param prefix the Cloudwatch logging prefix
159154
*/
160-
private createAwsLogDriver(prefix: string): AwsLogDriver {
161-
return new AwsLogDriver(this, 'QueueWorkerLogging', { streamPrefix: prefix });
155+
private createAwsLogDriver(prefix: string): ecs.AwsLogDriver {
156+
return new ecs.AwsLogDriver(this, 'QueueWorkerLogging', { streamPrefix: prefix });
162157
}
163158
}

packages/@aws-cdk/aws-ecs/lib/ecs-queue-worker-service.ts renamed to packages/@aws-cdk/aws-ecs-patterns/lib/ecs/ecs-queue-worker-service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import ecs = require('@aws-cdk/aws-ecs');
12
import cdk = require('@aws-cdk/cdk');
2-
import { Ec2Service } from './ec2/ec2-service';
3-
import { Ec2TaskDefinition } from './ec2/ec2-task-definition';
4-
import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from './queue-worker-service-base';
3+
import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from '../base/queue-worker-service-base';
54

65
/**
76
* Properties to define an Ec2 query worker service
@@ -49,7 +48,7 @@ export class Ec2QueueWorkerService extends QueueWorkerServiceBase {
4948
super(scope, id, props);
5049

5150
// Create a Task Definition for the container to start
52-
const taskDefinition = new Ec2TaskDefinition(this, 'QueueWorkerTaskDef');
51+
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'QueueWorkerTaskDef');
5352
taskDefinition.addContainer('QueueWorkerContainer', {
5453
image: props.image,
5554
memoryLimitMiB: props.memoryLimitMiB,
@@ -62,7 +61,7 @@ export class Ec2QueueWorkerService extends QueueWorkerServiceBase {
6261

6362
// Create an ECS service with the previously defined Task Definition and configure
6463
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
65-
const ecsService = new Ec2Service(this, 'QueueWorkerService', {
64+
const ecsService = new ecs.Ec2Service(this, 'QueueWorkerService', {
6665
cluster: props.cluster,
6766
desiredCount: this.desiredCount,
6867
taskDefinition

packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts renamed to packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import ecs = require('@aws-cdk/aws-ecs');
12
import cdk = require('@aws-cdk/cdk');
2-
import { Ec2Service } from './ec2/ec2-service';
3-
import { Ec2TaskDefinition } from './ec2/ec2-task-definition';
4-
import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from './load-balanced-service-base';
3+
import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base';
54

65
/**
76
* Properties for a LoadBalancedEc2Service
@@ -42,12 +41,12 @@ export class LoadBalancedEc2Service extends LoadBalancedServiceBase {
4241
/**
4342
* The ECS service in this construct
4443
*/
45-
public readonly service: Ec2Service;
44+
public readonly service: ecs.Ec2Service;
4645

4746
constructor(scope: cdk.Construct, id: string, props: LoadBalancedEc2ServiceProps) {
4847
super(scope, id, props);
4948

50-
const taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', {});
49+
const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', {});
5150

5251
const container = taskDefinition.addContainer('web', {
5352
image: props.image,
@@ -60,7 +59,7 @@ export class LoadBalancedEc2Service extends LoadBalancedServiceBase {
6059
containerPort: props.containerPort || 80
6160
});
6261

63-
const service = new Ec2Service(this, "Service", {
62+
const service = new ecs.Ec2Service(this, "Service", {
6463
cluster: props.cluster,
6564
desiredCount: props.desiredCount || 1,
6665
taskDefinition

packages/@aws-cdk/aws-ecs/lib/fargate-queue-worker-service.ts renamed to packages/@aws-cdk/aws-ecs-patterns/lib/fargate/fargate-queue-worker-service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import ecs = require('@aws-cdk/aws-ecs');
12
import cdk = require('@aws-cdk/cdk');
2-
import { FargateService } from './fargate/fargate-service';
3-
import { FargateTaskDefinition } from './fargate/fargate-task-definition';
4-
import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from './queue-worker-service-base';
3+
import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from '../base/queue-worker-service-base';
54

65
/**
76
* Properties to define a Fargate queue worker service
@@ -53,7 +52,7 @@ export class FargateQueueWorkerService extends QueueWorkerServiceBase {
5352
super(scope, id, props);
5453

5554
// Create a Task Definition for the container to start
56-
const taskDefinition = new FargateTaskDefinition(this, 'QueueWorkerTaskDef', {
55+
const taskDefinition = new ecs.FargateTaskDefinition(this, 'QueueWorkerTaskDef', {
5756
memoryMiB: props.memoryMiB !== undefined ? props.memoryMiB : '512',
5857
cpu: props.cpu !== undefined ? props.cpu : '256',
5958
});
@@ -66,7 +65,7 @@ export class FargateQueueWorkerService extends QueueWorkerServiceBase {
6665

6766
// Create a Fargate service with the previously defined Task Definition and configure
6867
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
69-
const fargateService = new FargateService(this, 'FargateQueueWorkerService', {
68+
const fargateService = new ecs.FargateService(this, 'FargateQueueWorkerService', {
7069
cluster: props.cluster,
7170
desiredCount: this.desiredCount,
7271
taskDefinition

0 commit comments

Comments
 (0)