diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts index 1c43b51c3b7f9..635aab7943841 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts @@ -1,9 +1,11 @@ import { ICertificate } from '@aws-cdk/aws-certificatemanager'; -import ec2 = require('@aws-cdk/aws-ec2'); -import ecs = require('@aws-cdk/aws-ecs'); -import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2'); +import { IVpc } from '@aws-cdk/aws-ec2'; +import { AwsLogDriver, BaseService, Cluster, ContainerImage, ICluster, LogDriver, Secret } from '@aws-cdk/aws-ecs'; +import { ApplicationListener, ApplicationLoadBalancer, ApplicationTargetGroup, BaseLoadBalancer, NetworkListener, + NetworkLoadBalancer, NetworkTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2'; +import { IRole } from '@aws-cdk/aws-iam'; import { AddressRecordTarget, ARecord, IHostedZone } from '@aws-cdk/aws-route53'; -import route53targets = require('@aws-cdk/aws-route53-targets'); +import { LoadBalancerTarget } from '@aws-cdk/aws-route53-targets'; import cdk = require('@aws-cdk/core'); export enum LoadBalancerType { @@ -12,7 +14,7 @@ export enum LoadBalancerType { } /** - * Base properties for load-balanced Fargate and ECS services + * The properties for the base LoadBalancedEc2Service or LoadBalancedFargateService service. */ export interface LoadBalancedServiceBaseProps { /** @@ -21,7 +23,7 @@ export interface LoadBalancedServiceBaseProps { * * @default - create a new cluster; if you do not specify a cluster nor a vpc, a new VPC will be created for you as well */ - readonly cluster?: ecs.ICluster; + readonly cluster?: ICluster; /** * VPC that the cluster instances or tasks are running in @@ -29,12 +31,12 @@ export interface LoadBalancedServiceBaseProps { * * @default - use vpc of cluster or create a new one */ - readonly vpc?: ec2.IVpc; + readonly vpc?: IVpc; /** * The image to start. */ - readonly image: ecs.ContainerImage; + readonly image: ContainerImage; /** * The container port of the application load balancer attached to your Fargate service. Corresponds to container port mapping. @@ -84,7 +86,7 @@ export interface LoadBalancedServiceBaseProps { * * @default - No secret environment variables. */ - readonly secrets?: { [key: string]: ecs.Secret }; + readonly secrets?: { [key: string]: Secret }; /** * Whether to create an AWS log driver @@ -113,24 +115,59 @@ export interface LoadBalancedServiceBaseProps { * @default - No Route53 hosted domain zone. */ readonly domainZone?: IHostedZone; + + /** + * Override for the Fargate Task Definition execution role + * + * @default - No value + */ + readonly executionRole?: IRole; + + /** + * Override for the Fargate Task Definition task role + * + * @default - No value + */ + readonly taskRole?: IRole; + + /** + * Override value for the container name + * + * @default - No value + */ + readonly containerName?: string; + + /** + * Override value for the service name + * + * @default CloudFormation-generated name + */ + readonly serviceName?: string; } /** - * Base class for load-balanced Fargate and ECS services + * The base class for LoadBalancedEc2Service and LoadBalancedFargateService services. */ export abstract class LoadBalancedServiceBase extends cdk.Construct { + public readonly assignPublicIp: boolean; + + public readonly desiredCount: number; + public readonly loadBalancerType: LoadBalancerType; - public readonly loadBalancer: elbv2.BaseLoadBalancer; + public readonly loadBalancer: BaseLoadBalancer; - public readonly listener: elbv2.ApplicationListener | elbv2.NetworkListener; + public readonly listener: ApplicationListener | NetworkListener; - public readonly targetGroup: elbv2.ApplicationTargetGroup | elbv2.NetworkTargetGroup; + public readonly targetGroup: ApplicationTargetGroup | NetworkTargetGroup; - public readonly cluster: ecs.ICluster; + public readonly cluster: ICluster; - public readonly logDriver?: ecs.LogDriver; + public readonly logDriver?: LogDriver; + /** + * Constructs a new instance of the LoadBalancedServiceBase class. + */ constructor(scope: cdk.Construct, id: string, props: LoadBalancedServiceBaseProps) { super(scope, id); @@ -143,6 +180,9 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { const enableLogging = props.enableLogging !== undefined ? props.enableLogging : true; this.logDriver = enableLogging ? this.createAWSLogDriver(this.node.id) : undefined; + this.assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; + this.desiredCount = props.desiredCount || 1; + // Load balancer this.loadBalancerType = props.loadBalancerType !== undefined ? props.loadBalancerType : LoadBalancerType.APPLICATION; @@ -158,9 +198,9 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { }; if (this.loadBalancerType === LoadBalancerType.APPLICATION) { - this.loadBalancer = new elbv2.ApplicationLoadBalancer(this, 'LB', lbProps); + this.loadBalancer = new ApplicationLoadBalancer(this, 'LB', lbProps); } else { - this.loadBalancer = new elbv2.NetworkLoadBalancer(this, 'LB', lbProps); + this.loadBalancer = new NetworkLoadBalancer(this, 'LB', lbProps); } const targetProps = { @@ -173,7 +213,7 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { } if (this.loadBalancerType === LoadBalancerType.APPLICATION) { - this.listener = (this.loadBalancer as elbv2.ApplicationLoadBalancer).addListener('PublicListener', { + this.listener = (this.loadBalancer as ApplicationLoadBalancer).addListener('PublicListener', { port: hasCertificate ? 443 : 80, open: true }); @@ -183,7 +223,7 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { this.listener.addCertificateArns('Arns', [props.certificate.certificateArn]); } } else { - this.listener = (this.loadBalancer as elbv2.NetworkLoadBalancer).addListener('PublicListener', { port: 80 }); + this.listener = (this.loadBalancer as NetworkLoadBalancer).addListener('PublicListener', { port: 80 }); this.targetGroup = this.listener.addTargets('ECS', targetProps); } @@ -195,29 +235,29 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { new ARecord(this, "DNS", { zone: props.domainZone, recordName: props.domainName, - target: AddressRecordTarget.fromAlias(new route53targets.LoadBalancerTarget(this.loadBalancer)), + target: AddressRecordTarget.fromAlias(new LoadBalancerTarget(this.loadBalancer)), }); } new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName }); } - protected getDefaultCluster(scope: cdk.Construct, vpc?: ec2.IVpc): ecs.Cluster { + protected getDefaultCluster(scope: cdk.Construct, vpc?: IVpc): Cluster { // magic string to avoid collision with user-defined constructs const DEFAULT_CLUSTER_ID = `EcsDefaultClusterMnL3mNNYN${vpc ? vpc.node.id : ''}`; const stack = cdk.Stack.of(scope); - return stack.node.tryFindChild(DEFAULT_CLUSTER_ID) as ecs.Cluster || new ecs.Cluster(stack, DEFAULT_CLUSTER_ID, { vpc }); + return stack.node.tryFindChild(DEFAULT_CLUSTER_ID) as Cluster || new Cluster(stack, DEFAULT_CLUSTER_ID, { vpc }); } - protected addServiceAsTarget(service: ecs.BaseService) { + protected addServiceAsTarget(service: BaseService) { if (this.loadBalancerType === LoadBalancerType.APPLICATION) { - (this.targetGroup as elbv2.ApplicationTargetGroup).addTarget(service); + (this.targetGroup as ApplicationTargetGroup).addTarget(service); } else { - (this.targetGroup as elbv2.NetworkTargetGroup).addTarget(service); + (this.targetGroup as NetworkTargetGroup).addTarget(service); } } - private createAWSLogDriver(prefix: string): ecs.AwsLogDriver { - return new ecs.AwsLogDriver({ streamPrefix: prefix }); + private createAWSLogDriver(prefix: string): AwsLogDriver { + return new AwsLogDriver({ streamPrefix: prefix }); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts index fd23c6209a477..a6f06ec7fae6d 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/queue-processing-service-base.ts @@ -1,31 +1,37 @@ -import autoscaling = require('@aws-cdk/aws-applicationautoscaling'); -import ecs = require('@aws-cdk/aws-ecs'); -import sqs = require('@aws-cdk/aws-sqs'); -import cdk = require('@aws-cdk/core'); +import { ScalingInterval } from '@aws-cdk/aws-applicationautoscaling'; +import { AwsLogDriver, BaseService, ContainerImage, ICluster, LogDriver, Secret } from '@aws-cdk/aws-ecs'; +import { IQueue, Queue } from '@aws-cdk/aws-sqs'; +import { CfnOutput, Construct } from '@aws-cdk/core'; /** - * Properties to define a queue processing service + * The properties for the base QueueProcessingEc2Service or QueueProcessingFargateService service. */ export interface QueueProcessingServiceBaseProps { /** - * Cluster where service will be deployed + * The name of the cluster that hosts the service. */ - readonly cluster: ecs.ICluster; + readonly cluster: ICluster; /** - * The image to start. + * The image used to start a container. + * + * This string is passed directly to the Docker daemon. + * Images in the Docker Hub registry are available by default. + * Other repositories are specified with either repository-url/image:tag or repository-url/image@digest. */ - readonly image: ecs.ContainerImage; + readonly image: ContainerImage; /** - * The CMD value to pass to the container. A string with commands delimited by commas. + * The command that is passed to the container. + * + * If you provide a shell command as a single string, you have to quote command-line arguments. * - * @default none + * @default - CMD value built into container image. */ readonly command?: string[]; /** - * Number of desired copies of running tasks + * The desired number of instantiations of the task definition to keep running on the service. * * @default 1 */ @@ -53,7 +59,7 @@ export interface QueueProcessingServiceBaseProps { * * @default - No secret environment variables. */ - readonly secrets?: { [key: string]: ecs.Secret }; + readonly secrets?: { [key: string]: Secret }; /** * A queue for which to process items from. @@ -63,7 +69,7 @@ export interface QueueProcessingServiceBaseProps { * * @default 'SQSQueue with CloudFormation-generated name' */ - readonly queue?: sqs.IQueue; + readonly queue?: IQueue; /** * Maximum capacity to scale to. @@ -80,17 +86,17 @@ export interface QueueProcessingServiceBaseProps { * * @default [{ upper: 0, change: -1 },{ lower: 100, change: +1 },{ lower: 500, change: +5 }] */ - readonly scalingSteps?: autoscaling.ScalingInterval[]; + readonly scalingSteps?: ScalingInterval[]; } /** - * Base class for a Fargate and ECS queue processing service + * The base class for QueueProcessingEc2Service and QueueProcessingFargateService services. */ -export abstract class QueueProcessingServiceBase extends cdk.Construct { +export abstract class QueueProcessingServiceBase extends Construct { /** * The SQS queue that the service will process from */ - public readonly sqsQueue: sqs.IQueue; + public readonly sqsQueue: IQueue; // Properties that have defaults defined. The Queue Processing Service will handle assigning undefined properties with default // values so that derived classes do not need to maintain the same logic. @@ -103,7 +109,7 @@ export abstract class QueueProcessingServiceBase extends cdk.Construct { /** * Secret environment variables */ - public readonly secrets?: { [key: string]: ecs.Secret }; + public readonly secrets?: { [key: string]: Secret }; /** * The minimum number of tasks to run @@ -118,18 +124,20 @@ export abstract class QueueProcessingServiceBase extends cdk.Construct { /** * The scaling interval for autoscaling based off an SQS Queue size */ - public readonly scalingSteps: autoscaling.ScalingInterval[]; - + public readonly scalingSteps: ScalingInterval[]; /** * The AwsLogDriver to use for logging if logging is enabled. */ - public readonly logDriver?: ecs.LogDriver; + public readonly logDriver?: LogDriver; - constructor(scope: cdk.Construct, id: string, props: QueueProcessingServiceBaseProps) { + /** + * Constructs a new instance of the QueueProcessingServiceBase class. + */ + constructor(scope: Construct, id: string, props: QueueProcessingServiceBaseProps) { super(scope, id); // Create the SQS queue if one is not provided - this.sqsQueue = props.queue !== undefined ? props.queue : new sqs.Queue(this, 'EcsProcessingQueue', {}); + this.sqsQueue = props.queue !== undefined ? props.queue : new Queue(this, 'EcsProcessingQueue', {}); // Setup autoscaling scaling intervals const defaultScalingSteps = [{ upper: 0, change: -1 }, { lower: 100, change: +1 }, { lower: 500, change: +5 }]; @@ -147,8 +155,8 @@ export abstract class QueueProcessingServiceBase extends cdk.Construct { this.desiredCount = props.desiredTaskCount || 1; this.maxCapacity = props.maxScalingCapacity || (2 * this.desiredCount); - new cdk.CfnOutput(this, 'SQSQueue', { value: this.sqsQueue.queueName }); - new cdk.CfnOutput(this, 'SQSQueueArn', { value: this.sqsQueue.queueArn }); + new CfnOutput(this, 'SQSQueue', { value: this.sqsQueue.queueName }); + new CfnOutput(this, 'SQSQueueArn', { value: this.sqsQueue.queueArn }); } /** @@ -156,7 +164,7 @@ export abstract class QueueProcessingServiceBase extends cdk.Construct { * * @param service the ECS/Fargate service for which to apply the autoscaling rules to */ - protected configureAutoscalingForService(service: ecs.BaseService) { + protected configureAutoscalingForService(service: BaseService) { const scalingTarget = service.autoScaleTaskCount({ maxCapacity: this.maxCapacity, minCapacity: this.desiredCount }); scalingTarget.scaleOnCpuUtilization('CpuScaling', { targetUtilizationPercent: 50, @@ -172,7 +180,7 @@ export abstract class QueueProcessingServiceBase extends cdk.Construct { * * @param prefix the Cloudwatch logging prefix */ - private createAWSLogDriver(prefix: string): ecs.AwsLogDriver { - return new ecs.AwsLogDriver({ streamPrefix: prefix }); + private createAWSLogDriver(prefix: string): AwsLogDriver { + return new AwsLogDriver({ streamPrefix: prefix }); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts index 4227aacc597c4..152ec4e70f066 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/scheduled-task-base.ts @@ -1,19 +1,26 @@ -import ecs = require('@aws-cdk/aws-ecs'); -import { ICluster } from '@aws-cdk/aws-ecs'; -import events = require('@aws-cdk/aws-events'); -import eventsTargets = require('@aws-cdk/aws-events-targets'); -import cdk = require('@aws-cdk/core'); +import { Schedule } from "@aws-cdk/aws-applicationautoscaling"; +import { AwsLogDriver, ContainerImage, ICluster, Secret, TaskDefinition } from "@aws-cdk/aws-ecs"; +import { Rule } from "@aws-cdk/aws-events"; +import { EcsTask } from "@aws-cdk/aws-events-targets"; +import { Construct } from "@aws-cdk/core"; +/** + * The properties for the base ScheduledEc2Task or ScheduledFargateTask task. + */ export interface ScheduledTaskBaseProps { /** - * The cluster where your service will be deployed. + * The name of the cluster that hosts the service. */ - readonly cluster: ecs.ICluster; + readonly cluster: ICluster; /** - * The image to start. + * The image used to start a container. + * + * This string is passed directly to the Docker daemon. + * Images in the Docker Hub registry are available by default. + * Other repositories are specified with either repository-url/image:tag or repository-url/image@digest. */ - readonly image: ecs.ContainerImage; + readonly image: ContainerImage; /** * The schedule or rate (frequency) that determines when CloudWatch Events @@ -22,17 +29,19 @@ export interface ScheduledTaskBaseProps { * * @see http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html */ - readonly schedule: events.Schedule; + readonly schedule: Schedule; /** - * The CMD value to pass to the container. A string with commands delimited by commas. + * The command that is passed to the container. * - * @default none + * If you provide a shell command as a single string, you have to quote command-line arguments. + * + * @default - CMD value built into container image. */ readonly command?: string[]; /** - * Number of desired copies of running tasks. + * The desired number of instantiations of the task definition to keep running on the service. * * @default 1 */ @@ -50,37 +59,46 @@ export interface ScheduledTaskBaseProps { * * @default - No secret environment variables. */ - readonly secrets?: { [key: string]: ecs.Secret }; + readonly secrets?: { [key: string]: Secret }; } /** - * A scheduled task base that will be initiated off of cloudwatch events. + * The base class for ScheduledEc2Task and ScheduledFargateTask tasks. */ -export abstract class ScheduledTaskBase extends cdk.Construct { +export abstract class ScheduledTaskBase extends Construct { + /** + * The name of the cluster that hosts the service. + */ public readonly cluster: ICluster; + /** + * The desired number of instantiations of the task definition to keep running on the service. + */ public readonly desiredTaskCount: number; - public readonly eventRule: events.Rule; + public readonly eventRule: Rule; - constructor(scope: cdk.Construct, id: string, props: ScheduledTaskBaseProps) { + /** + * Constructs a new instance of the ScheduledTaskBase class. + */ + constructor(scope: Construct, id: string, props: ScheduledTaskBaseProps) { super(scope, id); this.cluster = props.cluster; this.desiredTaskCount = props.desiredTaskCount || 1; // An EventRule that describes the event trigger (in this case a scheduled run) - this.eventRule = new events.Rule(this, 'ScheduledEventRule', { + this.eventRule = new Rule(this, 'ScheduledEventRule', { schedule: props.schedule, }); } /** - * Create an ecs task using the task definition provided and add it to the scheduled event rule + * Create an ECS task using the task definition provided and add it to the scheduled event rule. * * @param taskDefinition the TaskDefinition to add to the event rule */ - protected addTaskDefinitionToEventTarget(taskDefinition: ecs.TaskDefinition): eventsTargets.EcsTask { + protected addTaskDefinitionToEventTarget(taskDefinition: TaskDefinition): EcsTask { // Use the EcsTask as the target of the EventRule - const eventRuleTarget = new eventsTargets.EcsTask( { + const eventRuleTarget = new EcsTask( { cluster: this.cluster, taskDefinition, taskCount: this.desiredTaskCount @@ -96,7 +114,7 @@ export abstract class ScheduledTaskBase extends cdk.Construct { * * @param prefix the Cloudwatch logging prefix */ - protected createAWSLogDriver(prefix: string): ecs.AwsLogDriver { - return new ecs.AwsLogDriver({ streamPrefix: prefix }); + protected createAWSLogDriver(prefix: string): AwsLogDriver { + return new AwsLogDriver({ streamPrefix: prefix }); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts index bc49d6ef7367c..efb36749d3703 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts @@ -1,11 +1,26 @@ -import ecs = require('@aws-cdk/aws-ecs'); -import cdk = require('@aws-cdk/core'); +import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base'; /** - * Properties for a LoadBalancedEc2Service + * The properties for the LoadBalancedEc2Service service. */ export interface LoadBalancedEc2ServiceProps extends LoadBalancedServiceBaseProps { + + /** + * The number of cpu units used by the task. + * Valid values, which determines your range of valid values for the memory parameter: + * 256 (.25 vCPU) - Available memory values: 0.5GB, 1GB, 2GB + * 512 (.5 vCPU) - Available memory values: 1GB, 2GB, 3GB, 4GB + * 1024 (1 vCPU) - Available memory values: 2GB, 3GB, 4GB, 5GB, 6GB, 7GB, 8GB + * 2048 (2 vCPU) - Available memory values: Between 4GB and 16GB in 1GB increments + * 4096 (4 vCPU) - Available memory values: Between 8GB and 30GB in 1GB increments + * + * This default is set in the underlying FargateTaskDefinition construct. + * + * @default none + */ + readonly cpu?: number; /** * The hard limit (in MiB) of memory to present to the container. * @@ -41,35 +56,40 @@ export class LoadBalancedEc2Service extends LoadBalancedServiceBase { /** * The ECS service in this construct */ - public readonly service: ecs.Ec2Service; + public readonly service: Ec2Service; - constructor(scope: cdk.Construct, id: string, props: LoadBalancedEc2ServiceProps) { + /** + * Constructs a new instance of the LoadBalancedEc2Service class. + */ + constructor(scope: Construct, id: string, props: LoadBalancedEc2ServiceProps) { super(scope, id, props); - const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef', {}); + const taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', { + executionRole: props.executionRole, + taskRole: props.taskRole + }); - const container = taskDefinition.addContainer('web', { + const containerName = props.containerName !== undefined ? props.containerName : 'web'; + const container = taskDefinition.addContainer(containerName, { image: props.image, + cpu: props.cpu, memoryLimitMiB: props.memoryLimitMiB, memoryReservationMiB: props.memoryReservationMiB, environment: props.environment, secrets: props.secrets, logging: this.logDriver, }); - container.addPortMappings({ containerPort: props.containerPort || 80 }); - const assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; - const service = new ecs.Ec2Service(this, "Service", { + this.service = new Ec2Service(this, "Service", { cluster: this.cluster, - desiredCount: props.desiredCount || 1, + desiredCount: this.desiredCount, taskDefinition, - assignPublicIp + assignPublicIp: this.assignPublicIp, + serviceName: props.serviceName }); - - this.service = service; - this.addServiceAsTarget(service); + this.addServiceAsTarget(this.service); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts index 451dff98ee2f3..4eef0d356e748 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/queue-processing-ecs-service.ts @@ -1,9 +1,9 @@ -import ecs = require('@aws-cdk/aws-ecs'); -import cdk = require('@aws-cdk/core'); +import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base'; /** - * Properties to define a queue processing Ec2 service + * The properties for the QueueProcessingEc2Service service. */ export interface QueueProcessingEc2ServiceProps extends QueueProcessingServiceBaseProps { /** @@ -48,13 +48,16 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase { /** * The ECS service in this construct */ - public readonly service: ecs.Ec2Service; + public readonly service: Ec2Service; - constructor(scope: cdk.Construct, id: string, props: QueueProcessingEc2ServiceProps) { + /** + * Constructs a new instance of the QueueProcessingEc2Service class. + */ + constructor(scope: Construct, id: string, props: QueueProcessingEc2ServiceProps) { super(scope, id, props); // Create a Task Definition for the container to start - const taskDefinition = new ecs.Ec2TaskDefinition(this, 'QueueProcessingTaskDef'); + const taskDefinition = new Ec2TaskDefinition(this, 'QueueProcessingTaskDef'); taskDefinition.addContainer('QueueProcessingContainer', { image: props.image, memoryLimitMiB: props.memoryLimitMiB, @@ -68,7 +71,7 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase { // 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. - this.service = new ecs.Ec2Service(this, 'QueueProcessingService', { + this.service = new Ec2Service(this, 'QueueProcessingService', { cluster: props.cluster, desiredCount: this.desiredCount, taskDefinition diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts index 2cb713f2591bb..e819c6fdd306f 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts @@ -1,7 +1,10 @@ -import ecs = require('@aws-cdk/aws-ecs'); -import cdk = require('@aws-cdk/core'); +import { Ec2TaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; import { ScheduledTaskBase, ScheduledTaskBaseProps } from '../base/scheduled-task-base'; +/** + * The properties for the ScheduledEc2Task task. + */ export interface ScheduledEc2TaskProps extends ScheduledTaskBaseProps { /** * The minimum number of CPU units to reserve for the container. @@ -41,12 +44,21 @@ export interface ScheduledEc2TaskProps extends ScheduledTaskBaseProps { * A scheduled Ec2 task that will be initiated off of cloudwatch events. */ export class ScheduledEc2Task extends ScheduledTaskBase { - constructor(scope: cdk.Construct, id: string, props: ScheduledEc2TaskProps) { + + /** + * The ECS service in this construct + */ + public readonly taskDefinition: Ec2TaskDefinition; + + /** + * Constructs a new instance of the ScheduledEc2Task class. + */ + constructor(scope: Construct, id: string, props: ScheduledEc2TaskProps) { super(scope, id, props); // Create a Task Definition for the container to start, also creates a log driver - const taskDefinition = new ecs.Ec2TaskDefinition(this, 'ScheduledTaskDef'); - taskDefinition.addContainer('ScheduledContainer', { + this.taskDefinition = new Ec2TaskDefinition(this, 'ScheduledTaskDef'); + this.taskDefinition.addContainer('ScheduledContainer', { image: props.image, memoryLimitMiB: props.memoryLimitMiB, memoryReservationMiB: props.memoryReservationMiB, @@ -57,6 +69,6 @@ export class ScheduledEc2Task extends ScheduledTaskBase { logging: this.createAWSLogDriver(this.node.id) }); - this.addTaskDefinitionToEventTarget(taskDefinition); + this.addTaskDefinitionToEventTarget(this.taskDefinition); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts index 20240e1aa98dc..40d7bfaf35a78 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts @@ -1,10 +1,9 @@ -import ecs = require('@aws-cdk/aws-ecs'); -import iam = require('@aws-cdk/aws-iam'); -import cdk = require('@aws-cdk/core'); +import { FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base'; /** - * Properties for a LoadBalancedFargateService + * The properties for the LoadBalancedFargateService service. */ export interface LoadBalancedFargateServiceProps extends LoadBalancedServiceBaseProps { /** @@ -43,34 +42,6 @@ export interface LoadBalancedFargateServiceProps extends LoadBalancedServiceBase * @default 512 */ readonly memoryLimitMiB?: number; - - /** - * Override for the Fargate Task Definition execution role - * - * @default - No value - */ - readonly executionRole?: iam.IRole; - - /** - * Override for the Fargate Task Definition task role - * - * @default - No value - */ - readonly taskRole?: iam.IRole; - - /** - * Override value for the container name - * - * @default - No value - */ - readonly containerName?: string; - - /** - * Override value for the service name - * - * @default CloudFormation-generated name - */ - readonly serviceName?: string; } /** @@ -81,40 +52,39 @@ export class LoadBalancedFargateService extends LoadBalancedServiceBase { /** * The Fargate service in this construct */ - public readonly service: ecs.FargateService; + public readonly service: FargateService; - constructor(scope: cdk.Construct, id: string, props: LoadBalancedFargateServiceProps) { + /** + * Constructs a new instance of the LoadBalancedFargateService class. + */ + constructor(scope: Construct, id: string, props: LoadBalancedFargateServiceProps) { super(scope, id, props); - const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { + const taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { memoryLimitMiB: props.memoryLimitMiB, cpu: props.cpu, - executionRole: props.executionRole !== undefined ? props.executionRole : undefined, - taskRole: props.taskRole !== undefined ? props.taskRole : undefined + executionRole: props.executionRole, + taskRole: props.taskRole }); const containerName = props.containerName !== undefined ? props.containerName : 'web'; - const container = taskDefinition.addContainer(containerName, { image: props.image, logging: this.logDriver, environment: props.environment, secrets: props.secrets, }); - container.addPortMappings({ containerPort: props.containerPort || 80, }); - const assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; - const service = new ecs.FargateService(this, "Service", { + + this.service = new FargateService(this, "Service", { cluster: this.cluster, - desiredCount: props.desiredCount || 1, + desiredCount: this.desiredCount, taskDefinition, - assignPublicIp, - serviceName: props.serviceName, + assignPublicIp: this.assignPublicIp, + serviceName: props.serviceName }); - this.service = service; - - this.addServiceAsTarget(service); + this.addServiceAsTarget(this.service); } } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts index a9a04b627c457..1f78eb295e62a 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/queue-processing-fargate-service.ts @@ -1,9 +1,9 @@ -import ecs = require('@aws-cdk/aws-ecs'); -import cdk = require('@aws-cdk/core'); +import { FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; import { QueueProcessingServiceBase, QueueProcessingServiceBaseProps } from '../base/queue-processing-service-base'; /** - * Properties to define a queue processing Fargate service + * The properties for the QueueProcessingFargateService service. */ export interface QueueProcessingFargateServiceProps extends QueueProcessingServiceBaseProps { /** @@ -51,13 +51,16 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase { /** * The Fargate service in this construct */ - public readonly service: ecs.FargateService; + public readonly service: FargateService; - constructor(scope: cdk.Construct, id: string, props: QueueProcessingFargateServiceProps) { + /** + * Constructs a new instance of the QueueProcessingFargateService class. + */ + constructor(scope: Construct, id: string, props: QueueProcessingFargateServiceProps) { super(scope, id, props); // Create a Task Definition for the container to start - const taskDefinition = new ecs.FargateTaskDefinition(this, 'QueueProcessingTaskDef', { + const taskDefinition = new FargateTaskDefinition(this, 'QueueProcessingTaskDef', { memoryLimitMiB: props.memoryLimitMiB || 512, cpu: props.cpu || 256, }); @@ -71,7 +74,7 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase { // 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. - this.service = new ecs.FargateService(this, 'QueueProcessingFargateService', { + this.service = new FargateService(this, 'QueueProcessingFargateService', { cluster: props.cluster, desiredCount: this.desiredCount, taskDefinition diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts index 08ccd5228dac9..b2e1b5f059e69 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/scheduled-fargate-task.ts @@ -1,7 +1,10 @@ -import ecs = require('@aws-cdk/aws-ecs'); -import cdk = require('@aws-cdk/core'); +import { FargateTaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; import { ScheduledTaskBase, ScheduledTaskBaseProps } from '../base/scheduled-task-base'; +/** + * The properties for the ScheduledFargateTask service. + */ export interface ScheduledFargateTaskProps extends ScheduledTaskBaseProps { /** * The number of cpu units used by the task. @@ -24,27 +27,32 @@ export interface ScheduledFargateTaskProps extends ScheduledTaskBaseProps { * If your container attempts to exceed the allocated memory, the container * is terminated. * - * At least one of memoryLimitMiB and memoryReservationMiB is required for non-Fargate services. - * * @default 512 */ readonly memoryLimitMiB?: number; - } /** * A scheduled Fargate task that will be initiated off of cloudwatch events. */ export class ScheduledFargateTask extends ScheduledTaskBase { - constructor(scope: cdk.Construct, id: string, props: ScheduledFargateTaskProps) { + /** + * The ECS service in this construct + */ + public readonly taskDefinition: FargateTaskDefinition; + + /** + * Constructs a new instance of the ScheduledFargateTask class. + */ + constructor(scope: Construct, id: string, props: ScheduledFargateTaskProps) { super(scope, id, props); // Create a Task Definition for the container to start, also creates a log driver - const taskDefinition = new ecs.FargateTaskDefinition(this, 'ScheduledTaskDef', { + this. taskDefinition = new FargateTaskDefinition(this, 'ScheduledTaskDef', { memoryLimitMiB: props.memoryLimitMiB || 512, cpu: props.cpu || 256, }); - taskDefinition.addContainer('ScheduledContainer', { + this.taskDefinition.addContainer('ScheduledContainer', { image: props.image, command: props.command, environment: props.environment, @@ -52,6 +60,6 @@ export class ScheduledFargateTask extends ScheduledTaskBase { logging: this.createAWSLogDriver(this.node.id) }); - this.addTaskDefinitionToEventTarget(taskDefinition); + this.addTaskDefinitionToEventTarget(this.taskDefinition); } }