From 6fa74cfb61318370dff54fb3b83aaeae6b2f0e98 Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Thu, 23 May 2019 10:49:04 -0700 Subject: [PATCH] Refactor high level AWS ECS constructs into the aws-ecs-patterns module --- .../aws-ecs-autoscaling-queue-worker.md | 0 .../aws-ecs-priv-registry-support.md | 0 .../aws-ecs-service-discovery-integration.md | 0 packages/@aws-cdk/aws-ecs-patterns/README.md | 43 +++++++++++++++++++ .../lib/base}/load-balanced-service-base.ts | 10 ++--- .../lib/base}/queue-worker-service-base.ts | 23 ++++------ .../lib/ecs}/ecs-queue-worker-service.ts | 9 ++-- .../lib/ecs}/load-balanced-ecs-service.ts | 11 +++-- .../lib/{ => ecs}/scheduled-ecs-task.ts | 0 .../fargate}/fargate-queue-worker-service.ts | 9 ++-- .../load-balanced-fargate-service-applet.ts | 3 +- .../fargate}/load-balanced-fargate-service.ts | 16 +++---- .../@aws-cdk/aws-ecs-patterns/lib/index.ts | 12 +++++- .../@aws-cdk/aws-ecs-patterns/package.json | 12 ++++++ .../test/demo-image/Dockerfile | 5 +++ .../aws-ecs-patterns/test/demo-image/index.py | 33 ++++++++++++++ .../fargate/integ.asset-image.expected.json | 0 .../test/fargate/integ.asset-image.ts | 5 ++- .../test/fargate/integ.l3.expected.json | 0 .../test/fargate/integ.l3.ts | 5 ++- ...nteg.scheduled-ecs-task.lit.expected.json} | 0 ...lit.ts => integ.scheduled-ecs-task.lit.ts} | 0 .../test/test.ecs-worker-service.ts | 7 +-- .../test/test.fargate-worker-service.ts | 7 +-- .../test/test.l3s.ts | 21 ++++----- .../test.load-balanced-fargate-service.ts | 11 ++--- packages/@aws-cdk/aws-ecs/lib/index.ts | 9 ---- 27 files changed, 168 insertions(+), 83 deletions(-) rename design/{ => aws-ecs}/aws-ecs-autoscaling-queue-worker.md (100%) rename design/{ => aws-ecs}/aws-ecs-priv-registry-support.md (100%) rename design/{ => aws-ecs}/aws-ecs-service-discovery-integration.md (100%) rename packages/@aws-cdk/{aws-ecs/lib => aws-ecs-patterns/lib/base}/load-balanced-service-base.ts (93%) rename packages/@aws-cdk/{aws-ecs/lib => aws-ecs-patterns/lib/base}/queue-worker-service-base.ts (87%) rename packages/@aws-cdk/{aws-ecs/lib => aws-ecs-patterns/lib/ecs}/ecs-queue-worker-service.ts (88%) rename packages/@aws-cdk/{aws-ecs/lib => aws-ecs-patterns/lib/ecs}/load-balanced-ecs-service.ts (86%) rename packages/@aws-cdk/aws-ecs-patterns/lib/{ => ecs}/scheduled-ecs-task.ts (100%) rename packages/@aws-cdk/{aws-ecs/lib => aws-ecs-patterns/lib/fargate}/fargate-queue-worker-service.ts (88%) rename packages/@aws-cdk/{aws-ecs/lib => aws-ecs-patterns/lib/fargate}/load-balanced-fargate-service-applet.ts (98%) rename packages/@aws-cdk/{aws-ecs/lib => aws-ecs-patterns/lib/fargate}/load-balanced-fargate-service.ts (87%) create mode 100644 packages/@aws-cdk/aws-ecs-patterns/test/demo-image/Dockerfile create mode 100644 packages/@aws-cdk/aws-ecs-patterns/test/demo-image/index.py rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/fargate/integ.asset-image.expected.json (100%) rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/fargate/integ.asset-image.ts (80%) rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/fargate/integ.l3.expected.json (100%) rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/fargate/integ.l3.ts (74%) rename packages/@aws-cdk/aws-ecs-patterns/test/{integ.event-task.lit.expected.json => integ.scheduled-ecs-task.lit.expected.json} (100%) rename packages/@aws-cdk/aws-ecs-patterns/test/{integ.event-task.lit.ts => integ.scheduled-ecs-task.lit.ts} (100%) rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/test.ecs-worker-service.ts (94%) rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/test.fargate-worker-service.ts (94%) rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/test.l3s.ts (92%) rename packages/@aws-cdk/{aws-ecs => aws-ecs-patterns}/test/test.load-balanced-fargate-service.ts (79%) diff --git a/design/aws-ecs-autoscaling-queue-worker.md b/design/aws-ecs/aws-ecs-autoscaling-queue-worker.md similarity index 100% rename from design/aws-ecs-autoscaling-queue-worker.md rename to design/aws-ecs/aws-ecs-autoscaling-queue-worker.md diff --git a/design/aws-ecs-priv-registry-support.md b/design/aws-ecs/aws-ecs-priv-registry-support.md similarity index 100% rename from design/aws-ecs-priv-registry-support.md rename to design/aws-ecs/aws-ecs-priv-registry-support.md diff --git a/design/aws-ecs-service-discovery-integration.md b/design/aws-ecs/aws-ecs-service-discovery-integration.md similarity index 100% rename from design/aws-ecs-service-discovery-integration.md rename to design/aws-ecs/aws-ecs-service-discovery-integration.md diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index 3e175e1f4920a..48bd7507098c0 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -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`: diff --git a/packages/@aws-cdk/aws-ecs/lib/load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts similarity index 93% rename from packages/@aws-cdk/aws-ecs/lib/load-balanced-service-base.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts index 5bf79162891a1..9eaf2396b6c53 100644 --- a/packages/@aws-cdk/aws-ecs/lib/load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts @@ -1,9 +1,7 @@ import { ICertificate } from '@aws-cdk/aws-certificatemanager'; +import ecs = require('@aws-cdk/aws-ecs'); import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2'); import cdk = require('@aws-cdk/cdk'); -import { BaseService } from './base/base-service'; -import { ICluster } from './cluster'; -import { ContainerImage } from './container-image'; export enum LoadBalancerType { Application, @@ -14,12 +12,12 @@ export interface LoadBalancedServiceBaseProps { /** * The cluster where your service will be deployed */ - readonly cluster: ICluster; + readonly cluster: ecs.ICluster; /** * The image to start. */ - readonly image: ContainerImage; + readonly image: ecs.ContainerImage; /** * 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 { new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName }); } - protected addServiceAsTarget(service: BaseService) { + protected addServiceAsTarget(service: ecs.BaseService) { if (this.loadBalancerType === LoadBalancerType.Application) { (this.targetGroup as elbv2.ApplicationTargetGroup).addTarget(service); } else { diff --git a/packages/@aws-cdk/aws-ecs/lib/queue-worker-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-worker-service-base.ts similarity index 87% rename from packages/@aws-cdk/aws-ecs/lib/queue-worker-service-base.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-worker-service-base.ts index f59b54bad9509..f8ea4e2e3c545 100644 --- a/packages/@aws-cdk/aws-ecs/lib/queue-worker-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-worker-service-base.ts @@ -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 @@ -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. @@ -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. @@ -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. @@ -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); @@ -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, @@ -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 }); } } diff --git a/packages/@aws-cdk/aws-ecs/lib/ecs-queue-worker-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/ecs-queue-worker-service.ts similarity index 88% rename from packages/@aws-cdk/aws-ecs/lib/ecs-queue-worker-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/ecs/ecs-queue-worker-service.ts index 140d9719c0663..68fabe4d39d4c 100644 --- a/packages/@aws-cdk/aws-ecs/lib/ecs-queue-worker-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/ecs-queue-worker-service.ts @@ -1,7 +1,6 @@ +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'; +import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from '../base/queue-worker-service-base'; /** * Properties to define an Ec2 query worker service @@ -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, @@ -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 diff --git a/packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts similarity index 86% rename from packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts index 9dcf49af944bd..6512718f45b07 100644 --- a/packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts @@ -1,7 +1,6 @@ +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 { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from './load-balanced-service-base'; +import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base'; /** * Properties for a LoadBalancedEc2Service @@ -42,12 +41,12 @@ export class LoadBalancedEc2Service extends LoadBalancedServiceBase { /** * The ECS service in this construct */ - public readonly service: Ec2Service; + public readonly service: ecs.Ec2Service; constructor(scope: cdk.Construct, id: string, props: LoadBalancedEc2ServiceProps) { super(scope, id, props); - const taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', {}); + const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', {}); const container = taskDefinition.addContainer('web', { image: props.image, @@ -60,7 +59,7 @@ export class LoadBalancedEc2Service extends LoadBalancedServiceBase { containerPort: props.containerPort || 80 }); - const service = new Ec2Service(this, "Service", { + const service = new ecs.Ec2Service(this, "Service", { cluster: props.cluster, desiredCount: props.desiredCount || 1, taskDefinition diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts similarity index 100% rename from packages/@aws-cdk/aws-ecs-patterns/lib/scheduled-ecs-task.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts diff --git a/packages/@aws-cdk/aws-ecs/lib/fargate-queue-worker-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/fargate-queue-worker-service.ts similarity index 88% rename from packages/@aws-cdk/aws-ecs/lib/fargate-queue-worker-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/fargate/fargate-queue-worker-service.ts index cb5d361f2faed..b5a21c5867cdc 100644 --- a/packages/@aws-cdk/aws-ecs/lib/fargate-queue-worker-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/fargate-queue-worker-service.ts @@ -1,7 +1,6 @@ +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'; +import { QueueWorkerServiceBase, QueueWorkerServiceBaseProps } from '../base/queue-worker-service-base'; /** * Properties to define a Fargate queue worker service @@ -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', }); @@ -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 diff --git a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service-applet.ts similarity index 98% rename from packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service-applet.ts index 0793390f85e9e..abd7389b08217 100644 --- a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service-applet.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service-applet.ts @@ -1,9 +1,8 @@ import { Certificate } from '@aws-cdk/aws-certificatemanager'; import { Vpc } from '@aws-cdk/aws-ec2'; +import { Cluster, ContainerImage } from '@aws-cdk/aws-ecs'; import { HostedZoneProvider } from '@aws-cdk/aws-route53'; import cdk = require('@aws-cdk/cdk'); -import { Cluster } from './cluster'; -import { ContainerImage } from './container-image'; import { LoadBalancedFargateService } from './load-balanced-fargate-service'; /** diff --git a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts similarity index 87% rename from packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts index ca4ad80497024..0b2ddba2634f2 100644 --- a/packages/@aws-cdk/aws-ecs/lib/load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts @@ -1,10 +1,8 @@ +import ecs = require('@aws-cdk/aws-ecs'); import { AliasRecord, IHostedZone } from '@aws-cdk/aws-route53'; import targets = require('@aws-cdk/aws-route53-targets'); import cdk = require('@aws-cdk/cdk'); -import { FargateService } from './fargate/fargate-service'; -import { FargateTaskDefinition } from './fargate/fargate-task-definition'; -import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from './load-balanced-service-base'; -import { AwsLogDriver } from './log-drivers/aws-log-driver'; +import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base'; /** * Properties for a LoadBalancedEcsService @@ -84,12 +82,12 @@ export class LoadBalancedFargateService extends LoadBalancedServiceBase { /** * The Fargate service in this construct */ - public readonly service: FargateService; + public readonly service: ecs.FargateService; constructor(scope: cdk.Construct, id: string, props: LoadBalancedFargateServiceProps) { super(scope, id, props); - const taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { + const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { memoryMiB: props.memoryMiB, cpu: props.cpu }); @@ -107,7 +105,7 @@ export class LoadBalancedFargateService extends LoadBalancedServiceBase { }); const assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; - const service = new FargateService(this, "Service", { + const service = new ecs.FargateService(this, "Service", { cluster: props.cluster, desiredCount: props.desiredCount || 1, taskDefinition, @@ -130,7 +128,7 @@ export class LoadBalancedFargateService extends LoadBalancedServiceBase { } } - private createAWSLogDriver(prefix: string): AwsLogDriver { - return new AwsLogDriver(this, 'Logging', { streamPrefix: prefix }); + private createAWSLogDriver(prefix: string): ecs.AwsLogDriver { + return new ecs.AwsLogDriver(this, 'Logging', { streamPrefix: prefix }); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts index f420a3d61b9ff..7024ffd79b2ff 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts @@ -1,2 +1,10 @@ -// AWS::ECS-PATTERNS CloudFormation Resources: -export * from './scheduled-ecs-task'; +export * from './ecs/ecs-queue-worker-service'; +export * from './fargate/fargate-queue-worker-service'; +export * from './base/queue-worker-service-base'; + +export * from './ecs/load-balanced-ecs-service'; +export * from './fargate/load-balanced-fargate-service'; +export * from './fargate/load-balanced-fargate-service-applet'; +export * from './base/load-balanced-service-base'; + +export * from './ecs/scheduled-ecs-task'; diff --git a/packages/@aws-cdk/aws-ecs-patterns/package.json b/packages/@aws-cdk/aws-ecs-patterns/package.json index ca80eefb7499a..bce0c7be70721 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/package.json +++ b/packages/@aws-cdk/aws-ecs-patterns/package.json @@ -66,20 +66,32 @@ "jest": "^24.7.1" }, "dependencies": { + "@aws-cdk/aws-applicationautoscaling": "^0.31.0", + "@aws-cdk/aws-certificatemanager": "^0.31.0", "@aws-cdk/aws-ec2": "^0.31.0", "@aws-cdk/aws-ecs": "^0.31.0", + "@aws-cdk/aws-elasticloadbalancingv2": "^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-route53": "^0.31.0", + "@aws-cdk/aws-route53-targets": "^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-certificatemanager": "^0.31.0", "@aws-cdk/aws-ec2": "^0.31.0", "@aws-cdk/aws-ecs": "^0.31.0", + "@aws-cdk/aws-elasticloadbalancingv2": "^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-route53": "^0.31.0", + "@aws-cdk/aws-route53-targets": "^0.31.0", + "@aws-cdk/aws-sqs": "^0.31.0", "@aws-cdk/cdk": "^0.31.0" }, "engines": { diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/demo-image/Dockerfile b/packages/@aws-cdk/aws-ecs-patterns/test/demo-image/Dockerfile new file mode 100644 index 0000000000000..123b5670febc8 --- /dev/null +++ b/packages/@aws-cdk/aws-ecs-patterns/test/demo-image/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.6 +EXPOSE 8000 +WORKDIR /src +ADD . /src +CMD python3 index.py diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/demo-image/index.py b/packages/@aws-cdk/aws-ecs-patterns/test/demo-image/index.py new file mode 100644 index 0000000000000..2ccedfce3ab76 --- /dev/null +++ b/packages/@aws-cdk/aws-ecs-patterns/test/demo-image/index.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +import sys +import textwrap +import http.server +import socketserver + +PORT = 8000 + + +class Handler(http.server.SimpleHTTPRequestHandler): + def do_GET(self): + self.send_response(200) + self.send_header('Content-Type', 'text/html') + self.end_headers() + self.wfile.write(textwrap.dedent('''\ + + It works + +

Hello from the integ test container

+

This container got built and started as part of the integ test.

+ + + ''').encode('utf-8')) + + +def main(): + httpd = http.server.HTTPServer(("", PORT), Handler) + print("serving at port", PORT) + httpd.serve_forever() + + +if __name__ == '__main__': + main() diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.expected.json similarity index 100% rename from packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.expected.json rename to packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.expected.json diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.ts similarity index 80% rename from packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.ts rename to packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.ts index e01601395cc6a..d370f0cb4c2df 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.ts @@ -1,7 +1,8 @@ import ec2 = require('@aws-cdk/aws-ec2'); +import ecs = require('@aws-cdk/aws-ecs'); import cdk = require('@aws-cdk/cdk'); import path = require('path'); -import ecs = require('../../lib'); +import ecsPatterns = require('../../lib'); const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-ecs-integ'); @@ -13,7 +14,7 @@ Array.isArray(cluster); Array.isArray(path); // Instantiate Fargate Service with just cluster and image -const fargateService = new ecs.LoadBalancedFargateService(stack, "FargateService", { +const fargateService = new ecsPatterns.LoadBalancedFargateService(stack, "FargateService", { cluster, containerPort: 8000, image: new ecs.AssetImage(stack, 'Image', { diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/integ.l3.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.expected.json similarity index 100% rename from packages/@aws-cdk/aws-ecs/test/fargate/integ.l3.expected.json rename to packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.expected.json diff --git a/packages/@aws-cdk/aws-ecs/test/fargate/integ.l3.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.ts similarity index 74% rename from packages/@aws-cdk/aws-ecs/test/fargate/integ.l3.ts rename to packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.ts index a9faa6e434284..223b55621cd22 100644 --- a/packages/@aws-cdk/aws-ecs/test/fargate/integ.l3.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.ts @@ -1,6 +1,7 @@ import ec2 = require('@aws-cdk/aws-ec2'); +import ecs = require('@aws-cdk/aws-ecs'); import cdk = require('@aws-cdk/cdk'); -import ecs = require('../../lib'); +import ecsPatterns = require('../../lib'); const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-ecs-integ'); @@ -9,7 +10,7 @@ const vpc = new ec2.Vpc(stack, 'Vpc', { maxAZs: 2 }); const cluster = new ecs.Cluster(stack, 'FargateCluster', { vpc }); -new ecs.LoadBalancedFargateService(stack, 'L3', { +new ecsPatterns.LoadBalancedFargateService(stack, 'L3', { cluster, memoryMiB: '1GB', cpu: '512', diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/integ.event-task.lit.expected.json b/packages/@aws-cdk/aws-ecs-patterns/test/integ.scheduled-ecs-task.lit.expected.json similarity index 100% rename from packages/@aws-cdk/aws-ecs-patterns/test/integ.event-task.lit.expected.json rename to packages/@aws-cdk/aws-ecs-patterns/test/integ.scheduled-ecs-task.lit.expected.json diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/integ.event-task.lit.ts b/packages/@aws-cdk/aws-ecs-patterns/test/integ.scheduled-ecs-task.lit.ts similarity index 100% rename from packages/@aws-cdk/aws-ecs-patterns/test/integ.event-task.lit.ts rename to packages/@aws-cdk/aws-ecs-patterns/test/integ.scheduled-ecs-task.lit.ts diff --git a/packages/@aws-cdk/aws-ecs/test/test.ecs-worker-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/test.ecs-worker-service.ts similarity index 94% rename from packages/@aws-cdk/aws-ecs/test/test.ecs-worker-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/test/test.ecs-worker-service.ts index 9da38e86c195f..ddd3bd9b82f00 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.ecs-worker-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/test.ecs-worker-service.ts @@ -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) { @@ -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') @@ -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'), diff --git a/packages/@aws-cdk/aws-ecs/test/test.fargate-worker-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/test.fargate-worker-service.ts similarity index 94% rename from packages/@aws-cdk/aws-ecs/test/test.fargate-worker-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/test/test.fargate-worker-service.ts index f558212763b71..11f8ecd6ca94f 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.fargate-worker-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/test.fargate-worker-service.ts @@ -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) { @@ -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') @@ -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'), diff --git a/packages/@aws-cdk/aws-ecs/test/test.l3s.ts b/packages/@aws-cdk/aws-ecs-patterns/test/test.l3s.ts similarity index 92% rename from packages/@aws-cdk/aws-ecs/test/test.l3s.ts rename to packages/@aws-cdk/aws-ecs-patterns/test/test.l3s.ts index 28e0dc9702fc0..627c7832d5e4e 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.l3s.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/test.l3s.ts @@ -1,10 +1,11 @@ import { expect, haveResource, haveResourceLike } from '@aws-cdk/assert'; import { Certificate } from '@aws-cdk/aws-certificatemanager'; import ec2 = require('@aws-cdk/aws-ec2'); +import ecs = require('@aws-cdk/aws-ecs'); import { PublicHostedZone } from '@aws-cdk/aws-route53'; import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; -import ecs = require('../lib'); +import ecsPatterns = require('../lib'); export = { 'test ECS loadbalanced construct'(test: Test) { @@ -15,7 +16,7 @@ export = { cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); // WHEN - new ecs.LoadBalancedEc2Service(stack, 'Service', { + new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { cluster, memoryLimitMiB: 1024, image: ecs.ContainerImage.fromRegistry('test'), @@ -63,7 +64,7 @@ export = { cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); // WHEN - new ecs.LoadBalancedEc2Service(stack, 'Service', { + new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { cluster, memoryReservationMiB: 1024, image: ecs.ContainerImage.fromRegistry('test') @@ -90,7 +91,7 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecs.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), desiredCount: 2, @@ -146,7 +147,7 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecs.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), desiredCount: 2, @@ -193,7 +194,7 @@ export = { const zone = new PublicHostedZone(stack, 'HostedZone', { zoneName: 'example.com' }); // WHEN - new ecs.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), domainName: 'api.example.com', @@ -239,7 +240,7 @@ export = { // THEN test.throws(() => { - new ecs.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), domainName: 'api.example.com' @@ -252,7 +253,7 @@ export = { 'test Fargateloadbalanced applet'(test: Test) { // WHEN const app = new cdk.App(); - const stack = new ecs.LoadBalancedFargateServiceApplet(app, 'Service', { + const stack = new ecsPatterns.LoadBalancedFargateServiceApplet(app, 'Service', { image: 'test', desiredCount: 2, environment: { @@ -292,7 +293,7 @@ export = { 'test Fargateloadbalanced applet with TLS'(test: Test) { // WHEN const app = new cdk.App(); - const stack = new ecs.LoadBalancedFargateServiceApplet(app, 'Service', { + const stack = new ecsPatterns.LoadBalancedFargateServiceApplet(app, 'Service', { image: 'test', desiredCount: 2, domainName: 'api.example.com', @@ -331,7 +332,7 @@ export = { "errors when setting domainName but not domainZone on applet"(test: Test) { // THEN test.throws(() => { - new ecs.LoadBalancedFargateServiceApplet(new cdk.App(), 'Service', { + new ecsPatterns.LoadBalancedFargateServiceApplet(new cdk.App(), 'Service', { image: 'test', domainName: 'api.example.com' }); diff --git a/packages/@aws-cdk/aws-ecs/test/test.load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/test.load-balanced-fargate-service.ts similarity index 79% rename from packages/@aws-cdk/aws-ecs/test/test.load-balanced-fargate-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/test/test.load-balanced-fargate-service.ts index 6d52b4fb612de..a83f6aa1ab05c 100644 --- a/packages/@aws-cdk/aws-ecs/test/test.load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/test.load-balanced-fargate-service.ts @@ -1,9 +1,10 @@ import { expect, haveResourceLike } from '@aws-cdk/assert'; import { Certificate } from '@aws-cdk/aws-certificatemanager'; import ec2 = require('@aws-cdk/aws-ec2'); +import ecs = require('@aws-cdk/aws-ecs'); import cdk = require('@aws-cdk/cdk'); import { Test } from 'nodeunit'; -import ecs = require('../lib'); +import ecsPatterns = require('../lib'); export = { 'certificate requires an application load balancer'(test: Test) { @@ -15,10 +16,10 @@ export = { // WHEN const cert = new Certificate(stack, 'Cert', { domainName: '*.example.com' }); const toThrow = () => { - new ecs.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { cluster, certificate: cert, - loadBalancerType: ecs.LoadBalancerType.Network, + loadBalancerType: ecsPatterns.LoadBalancerType.Network, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") }); }; @@ -35,9 +36,9 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecs.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecs.LoadBalancerType.Network, + loadBalancerType: ecsPatterns.LoadBalancerType.Network, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") }); diff --git a/packages/@aws-cdk/aws-ecs/lib/index.ts b/packages/@aws-cdk/aws-ecs/lib/index.ts index 101ffc2d817e8..1e6e4e2038572 100644 --- a/packages/@aws-cdk/aws-ecs/lib/index.ts +++ b/packages/@aws-cdk/aws-ecs/lib/index.ts @@ -14,15 +14,6 @@ export * from './fargate/fargate-service'; export * from './fargate/fargate-task-definition'; export * from './linux-parameters'; -export * from './load-balanced-service-base'; -export * from './load-balanced-ecs-service'; -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';