Skip to content

Commit

Permalink
Refactor high level AWS ECS constructs into the aws-ecs-patterns module
Browse files Browse the repository at this point in the history
  • Loading branch information
piradeepk committed May 23, 2019
1 parent 9b4db42 commit 12a36e7
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 32 deletions.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,51 @@

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

* Queue Worker Services
* Scheduled Tasks (cron jobs)

## Queue Worker Services

To define a service that creates a queue and reads from that queue, instantiate one of the following:

* `Ec2QueueWorkerService`

```ts
const ecsQueueWorkerService = new Ec2QueueWorkerService(stack, 'Service', {
cluster,
memoryLimitMiB: 1024,
image: ecs.ContainerImage.fromRegistry('test'),
command: ["-c", "4", "amazon.com"],
enableLogging: false,
desiredTaskCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
},
queue,
maxScalingCapacity: 5
});
```

* `FargateQueueWorkerService`

```ts
const fargateQueueWorkerService = new FargateQueueWorkerService(stack, 'Service', {
cluster,
memoryMiB: '512',
image: ecs.ContainerImage.fromRegistry('test'),
command: ["-c", "4", "amazon.com"],
enableLogging: false,
desiredTaskCount: 2,
environment: {
TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value"
},
queue,
maxScalingCapacity: 5
});
```

## Scheduled Tasks

To define a task that runs periodically, instantiate an `ScheduledEc2Task`:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ecs = require('@aws-cdk/aws-ecs');
import cdk = require('@aws-cdk/cdk');
import { Ec2Service } from './ec2/ec2-service';
import { Ec2TaskDefinition } from './ec2/ec2-task-definition';
import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from './queue-worker-service-base';

/**
Expand Down Expand Up @@ -49,7 +48,7 @@ export class Ec2QueueWorkerService extends QueueWorkerServiceBase {
super(scope, id, props);

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

// Create an ECS service with the previously defined Task Definition and configure
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
const ecsService = new Ec2Service(this, 'QueueWorkerService', {
const ecsService = new ecs.Ec2Service(this, 'QueueWorkerService', {
cluster: props.cluster,
desiredCount: this.desiredCount,
taskDefinition
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ecs = require('@aws-cdk/aws-ecs');
import cdk = require('@aws-cdk/cdk');
import { FargateService } from './fargate/fargate-service';
import { FargateTaskDefinition } from './fargate/fargate-task-definition';
import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from './queue-worker-service-base';

/**
Expand Down Expand Up @@ -53,7 +52,7 @@ export class FargateQueueWorkerService extends QueueWorkerServiceBase {
super(scope, id, props);

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

// Create a Fargate service with the previously defined Task Definition and configure
// autoscaling based on cpu utilization and number of messages visible in the SQS queue.
const fargateService = new FargateService(this, 'FargateQueueWorkerService', {
const fargateService = new ecs.FargateService(this, 'FargateQueueWorkerService', {
cluster: props.cluster,
desiredCount: this.desiredCount,
taskDefinition
Expand Down
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
// AWS::ECS-PATTERNS CloudFormation Resources:
export * from './ecs-queue-worker-service';
export * from './fargate-queue-worker-service';
export * from './queue-worker-service-base';

export * from './scheduled-ecs-task';
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import autoscaling = require('@aws-cdk/aws-applicationautoscaling');
import ecs = require('@aws-cdk/aws-ecs');
import sqs = require('@aws-cdk/aws-sqs');
import { IQueue } from '@aws-cdk/aws-sqs';
import cdk = require('@aws-cdk/cdk');
import { BaseService } from './base/base-service';
import { ICluster } from './cluster';
import { ContainerImage } from './container-image';
import { AwsLogDriver } from './log-drivers/aws-log-driver';
import { LogDriver } from './log-drivers/log-driver';

/**
* Properties to define a Query Worker service
Expand All @@ -15,12 +10,12 @@ export interface QueueWorkerServiceBaseProps {
/**
* Cluster where service will be deployed
*/
readonly cluster: ICluster;
readonly cluster: ecs.ICluster;

/**
* The image to start.
*/
readonly image: ContainerImage;
readonly image: ecs.ContainerImage;

/**
* The CMD value to pass to the container. A string with commands delimited by commas.
Expand Down Expand Up @@ -58,7 +53,7 @@ export interface QueueWorkerServiceBaseProps {
*
* @default 'SQSQueue with CloudFormation-generated name'
*/
readonly queue?: IQueue;
readonly queue?: sqs.IQueue;

/**
* Maximum capacity to scale to.
Expand All @@ -85,7 +80,7 @@ export abstract class QueueWorkerServiceBase extends cdk.Construct {
/**
* The SQS queue that the worker service will process from
*/
public readonly sqsQueue: IQueue;
public readonly sqsQueue: sqs.IQueue;

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

constructor(scope: cdk.Construct, id: string, props: QueueWorkerServiceBaseProps) {
super(scope, id);
Expand Down Expand Up @@ -141,7 +136,7 @@ export abstract class QueueWorkerServiceBase extends cdk.Construct {
*
* @param service the ECS/Fargate service for which to apply the autoscaling rules to
*/
protected configureAutoscalingForService(service: BaseService) {
protected configureAutoscalingForService(service: ecs.BaseService) {
const scalingTarget = service.autoScaleTaskCount({ maxCapacity: this.maxCapacity, minCapacity: this.desiredCount });
scalingTarget.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 50,
Expand All @@ -157,7 +152,7 @@ export abstract class QueueWorkerServiceBase extends cdk.Construct {
*
* @param prefix the Cloudwatch logging prefix
*/
private createAwsLogDriver(prefix: string): AwsLogDriver {
return new AwsLogDriver(this, 'QueueWorkerLogging', { streamPrefix: prefix });
private createAwsLogDriver(prefix: string): ecs.AwsLogDriver {
return new ecs.AwsLogDriver(this, 'QueueWorkerLogging', { streamPrefix: prefix });
}
}
4 changes: 4 additions & 0 deletions packages/@aws-cdk/aws-ecs-patterns/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,24 @@
"jest": "^24.7.1"
},
"dependencies": {
"@aws-cdk/aws-applicationautoscaling": "^0.31.0",
"@aws-cdk/aws-ec2": "^0.31.0",
"@aws-cdk/aws-ecs": "^0.31.0",
"@aws-cdk/aws-events": "^0.31.0",
"@aws-cdk/aws-events-targets": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-sqs": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
"homepage": "https://github.com/awslabs/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-applicationautoscaling": "^0.31.0",
"@aws-cdk/aws-ec2": "^0.31.0",
"@aws-cdk/aws-ecs": "^0.31.0",
"@aws-cdk/aws-events": "^0.31.0",
"@aws-cdk/aws-events-targets": "^0.31.0",
"@aws-cdk/aws-iam": "^0.31.0",
"@aws-cdk/aws-sqs": "^0.31.0",
"@aws-cdk/cdk": "^0.31.0"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import ec2 = require('@aws-cdk/aws-ec2');
import ecs = require('@aws-cdk/aws-ecs');
import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import ecs = require('../lib');
import ecsPatterns = require('../lib');

export = {
'test ECS queue worker service construct - with only required props'(test: Test) {
Expand All @@ -14,7 +15,7 @@ export = {
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

// WHEN
new ecs.Ec2QueueWorkerService(stack, 'Service', {
new ecsPatterns.Ec2QueueWorkerService(stack, 'Service', {
cluster,
memoryLimitMiB: 512,
image: ecs.ContainerImage.fromRegistry('test')
Expand Down Expand Up @@ -72,7 +73,7 @@ export = {
const queue = new sqs.Queue(stack, 'ecs-test-queue', { queueName: 'ecs-test-sqs-queue'});

// WHEN
new ecs.Ec2QueueWorkerService(stack, 'Service', {
new ecsPatterns.Ec2QueueWorkerService(stack, 'Service', {
cluster,
memoryLimitMiB: 1024,
image: ecs.ContainerImage.fromRegistry('test'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert';
import ec2 = require('@aws-cdk/aws-ec2');
import ecs = require('@aws-cdk/aws-ecs');
import sqs = require('@aws-cdk/aws-sqs');
import cdk = require('@aws-cdk/cdk');
import { Test } from 'nodeunit';
import ecs = require('../lib');
import ecsPatterns = require('../lib');

export = {
'test fargate queue worker service construct - with only required props'(test: Test) {
Expand All @@ -14,7 +15,7 @@ export = {
cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') });

// WHEN
new ecs.FargateQueueWorkerService(stack, 'Service', {
new ecsPatterns.FargateQueueWorkerService(stack, 'Service', {
cluster,
memoryMiB: '512',
image: ecs.ContainerImage.fromRegistry('test')
Expand Down Expand Up @@ -71,7 +72,7 @@ export = {
const queue = new sqs.Queue(stack, 'fargate-test-queue', { queueName: 'fargate-test-sqs-queue'});

// WHEN
new ecs.FargateQueueWorkerService(stack, 'Service', {
new ecsPatterns.FargateQueueWorkerService(stack, 'Service', {
cluster,
memoryMiB: '512',
image: ecs.ContainerImage.fromRegistry('test'),
Expand Down
4 changes: 0 additions & 4 deletions packages/@aws-cdk/aws-ecs/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export * from './load-balanced-fargate-service';
export * from './load-balanced-ecs-service';
export * from './load-balanced-fargate-service-applet';

export * from './queue-worker-service-base';
export * from './ecs-queue-worker-service';
export * from './fargate-queue-worker-service';

export * from './images/asset-image';
export * from './images/repository';
export * from './images/ecr';
Expand Down

0 comments on commit 12a36e7

Please sign in to comment.