From 78d76a91ec3bf7b4a7ac9929705514e6ab188b6e Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Mon, 19 Aug 2019 15:43:32 -0700 Subject: [PATCH 1/7] (aws-ecs-patterns): separate out logic for Application and Network Load Balanced Services --- ...application-load-balanced-service-base.ts} | 78 ++---- .../network-load-balanced-service-base.ts | 247 ++++++++++++++++++ .../application-load-balanced-ecs-service.ts | 101 +++++++ ...s => network-load-balanced-ecs-service.ts} | 14 +- ...plication-load-balanced-fargate-service.ts | 97 +++++++ ... network-load-balanced-fargate-service.ts} | 14 +- .../@aws-cdk/aws-ecs-patterns/lib/index.ts | 10 +- .../aws-ecs-patterns/test/ec2/test.l3s.ts | 21 +- .../test/fargate/integ.asset-image.ts | 2 +- .../test/fargate/integ.executionrole.ts | 2 +- .../test/fargate/integ.l3-autocreate.ts | 4 +- .../test/fargate/integ.l3-vpconly.ts | 6 +- .../aws-ecs-patterns/test/fargate/integ.l3.ts | 2 +- .../test.load-balanced-fargate-service.ts | 30 +-- 14 files changed, 513 insertions(+), 115 deletions(-) rename packages/@aws-cdk/aws-ecs-patterns/lib/base/{load-balanced-service-base.ts => application-load-balanced-service-base.ts} (72%) create mode 100644 packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts create mode 100644 packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts rename packages/@aws-cdk/aws-ecs-patterns/lib/ecs/{load-balanced-ecs-service.ts => network-load-balanced-ecs-service.ts} (82%) create mode 100644 packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts rename packages/@aws-cdk/aws-ecs-patterns/lib/fargate/{load-balanced-fargate-service.ts => network-load-balanced-fargate-service.ts} (81%) 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/application-load-balanced-service-base.ts similarity index 72% rename from packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts index f941593e8c4c6..7d23d04e3dde7 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts @@ -1,22 +1,16 @@ import { ICertificate } from '@aws-cdk/aws-certificatemanager'; 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 { ApplicationListener, ApplicationLoadBalancer, ApplicationTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2'; import { IRole } from '@aws-cdk/aws-iam'; import { AddressRecordTarget, ARecord, IHostedZone } from '@aws-cdk/aws-route53'; import { LoadBalancerTarget } from '@aws-cdk/aws-route53-targets'; import cdk = require('@aws-cdk/core'); -export enum LoadBalancerType { - APPLICATION, - NETWORK -} - /** - * The properties for the base LoadBalancedEc2Service or LoadBalancedFargateService service. + * The properties for the base ApplicationLoadBalancedEc2Service or ApplicationLoadBalancedFargateService service. */ -export interface LoadBalancedServiceBaseProps { +export interface ApplicationLoadBalancedServiceBaseProps { /** * The name of the cluster that hosts the service. * @@ -66,13 +60,6 @@ export interface LoadBalancedServiceBaseProps { */ readonly desiredCount?: number; - /** - * The type of the load balancer to be used. - * - * @default application - */ - readonly loadBalancerType?: LoadBalancerType - /** * Certificate Manager certificate to associate with the load balancer. * Setting this option will set the load balancer port to 443. @@ -169,22 +156,20 @@ export interface LoadBalancedServiceBaseProps { } /** - * The base class for LoadBalancedEc2Service and LoadBalancedFargateService services. + * The base class for ApplicationLoadBalancedEc2Service and ApplicationLoadBalancedFargateService services. */ -export abstract class LoadBalancedServiceBase extends cdk.Construct { +export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct { public readonly assignPublicIp: boolean; /** * The desired number of instantiations of the task definition to keep running on the service. */ public readonly desiredCount: number; - public readonly loadBalancerType: LoadBalancerType; + public readonly loadBalancer: ApplicationLoadBalancer; - public readonly loadBalancer: BaseLoadBalancer; + public readonly listener: ApplicationListener; - public readonly listener: ApplicationListener | NetworkListener; - - public readonly targetGroup: ApplicationTargetGroup | NetworkTargetGroup; + public readonly targetGroup: ApplicationTargetGroup; /** * The cluster that hosts the service. */ @@ -193,9 +178,9 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { public readonly logDriver?: LogDriver; /** - * Constructs a new instance of the LoadBalancedServiceBase class. + * Constructs a new instance of the ApplicationLoadBalancedServiceBase class. */ - constructor(scope: cdk.Construct, id: string, props: LoadBalancedServiceBaseProps) { + constructor(scope: cdk.Construct, id: string, props: ApplicationLoadBalancedServiceBaseProps) { super(scope, id); if (props.cluster && props.vpc) { @@ -210,13 +195,6 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { this.assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; this.desiredCount = props.desiredCount || 1; - // Load balancer - this.loadBalancerType = props.loadBalancerType !== undefined ? props.loadBalancerType : LoadBalancerType.APPLICATION; - - if (this.loadBalancerType !== LoadBalancerType.APPLICATION && this.loadBalancerType !== LoadBalancerType.NETWORK) { - throw new Error(`invalid loadBalancerType`); - } - const internetFacing = props.publicLoadBalancer !== undefined ? props.publicLoadBalancer : true; const lbProps = { @@ -224,34 +202,20 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { internetFacing }; - if (this.loadBalancerType === LoadBalancerType.APPLICATION) { - this.loadBalancer = new ApplicationLoadBalancer(this, 'LB', lbProps); - } else { - this.loadBalancer = new NetworkLoadBalancer(this, 'LB', lbProps); - } + this.loadBalancer = new ApplicationLoadBalancer(this, 'LB', lbProps); const targetProps = { port: 80 }; - const hasCertificate = props.certificate !== undefined; - if (hasCertificate && this.loadBalancerType !== LoadBalancerType.APPLICATION) { - throw new Error("Cannot add certificate to an NLB"); - } - - if (this.loadBalancerType === LoadBalancerType.APPLICATION) { - this.listener = (this.loadBalancer as ApplicationLoadBalancer).addListener('PublicListener', { - port: hasCertificate ? 443 : 80, - open: true - }); - this.targetGroup = this.listener.addTargets('ECS', targetProps); + this.listener = this.loadBalancer.addListener('PublicListener', { + port: props.certificate !== undefined ? 443 : 80, + open: true + }); + this.targetGroup = this.listener.addTargets('ECS', targetProps); - if (props.certificate !== undefined) { - this.listener.addCertificateArns('Arns', [props.certificate.certificateArn]); - } - } else { - this.listener = (this.loadBalancer as NetworkLoadBalancer).addListener('PublicListener', { port: 80 }); - this.targetGroup = this.listener.addTargets('ECS', targetProps); + if (props.certificate !== undefined) { + this.listener.addCertificateArns('Arns', [props.certificate.certificateArn]); } if (typeof props.domainName !== 'undefined') { @@ -277,11 +241,7 @@ export abstract class LoadBalancedServiceBase extends cdk.Construct { } protected addServiceAsTarget(service: BaseService) { - if (this.loadBalancerType === LoadBalancerType.APPLICATION) { - (this.targetGroup as ApplicationTargetGroup).addTarget(service); - } else { - (this.targetGroup as NetworkTargetGroup).addTarget(service); - } + this.targetGroup.addTarget(service); } private createAWSLogDriver(prefix: string): AwsLogDriver { diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts new file mode 100644 index 0000000000000..5db3e4b924f68 --- /dev/null +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts @@ -0,0 +1,247 @@ +import { ICertificate } from '@aws-cdk/aws-certificatemanager'; +import { IVpc } from '@aws-cdk/aws-ec2'; +import { AwsLogDriver, BaseService, Cluster, ContainerImage, ICluster, LogDriver, Secret } from '@aws-cdk/aws-ecs'; +import { 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 { LoadBalancerTarget } from '@aws-cdk/aws-route53-targets'; +import cdk = require('@aws-cdk/core'); + +/** + * The properties for the base NetworkLoadBalancedEc2Service or NetworkLoadBalancedFargateService service. + */ +export interface NetworkLoadBalancedServiceBaseProps { + /** + * The name of the cluster that hosts the service. + * + * You can only specify either vpc or cluster. Alternatively, you can leave both blank. + * @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?: ICluster; + + /** + * The VPC where the ECS instances will be running or the ENIs will be deployed. + * + * You can only specify either vpc or cluster. Alternatively, you can leave both blank. + * @default - uses the vpc defined in the cluster or creates a new one. + */ + readonly vpc?: IVpc; + + /** + * The image used to start a container. + */ + readonly image: ContainerImage; + + /** + * The port number on the container that is bound to the user-specified or automatically assigned host port. + * + * If you are using containers in a task with the awsvpc or host network mode, exposed ports should be specified using containerPort. + * If you are using containers in a task with the bridge network mode and you specify a container port and not a host port, + * your container automatically receives a host port in the ephemeral port range. + * + * For more information, see hostPort. + * Port mappings that are automatically assigned in this way do not count toward the 100 reserved ports limit of a container instance. + * + * @default 80 + */ + readonly containerPort?: number; + + /** + * Determines whether the Load Balancer will be internet-facing. + * + * @default true + */ + readonly publicLoadBalancer?: boolean; + + /** + * The desired number of instantiations of the task definition to keep running on the service. + * + * @default 1 + */ + readonly desiredCount?: number; + + /** + * Certificate Manager certificate to associate with the load balancer. + * Setting this option will set the load balancer port to 443. + * + * @default - No certificate associated with the load balancer. + */ + readonly certificate?: ICertificate; + + /** + * The environment variables to pass to the container. + * + * @default - No environment variables. + */ + readonly environment?: { [key: string]: string }; + + /** + * The secret environment variables to pass to the container + * + * @default - No secret environment variables. + */ + readonly secrets?: { [key: string]: Secret }; + + /** + * Flag to indicate whether to enable logging. + * + * @default true + */ + readonly enableLogging?: boolean; + + /** + * Determines whether the Service will be assigned a public IP address. + * + * @default false + */ + readonly publicTasks?: boolean; + + /** + * Domain name for the service, e.g. api.example.com + * + * @default - No domain name. + */ + readonly domainName?: string; + + /** + * Route53 hosted zone for the domain, e.g. "example.com." + * + * @default - No Route53 hosted domain zone. + */ + readonly domainZone?: IHostedZone; + + /** + * Override for the Fargate Task Definition execution role + * + * @default - No value + */ + readonly executionRole?: IRole; + + /** + * The name of the IAM role that grants containers in the task permission to call AWS APIs on your behalf. + * + * @default - A task role is automatically created for you. + */ + readonly taskRole?: IRole; + + /** + * The container name value to be specified in the task definition. + * + * @default - none + */ + readonly containerName?: string; + + /** + * The name of the service. + * + * @default - CloudFormation-generated name. + */ + readonly serviceName?: string; + + /** + * The LogDriver to use for logging. + * + * @default - AwsLogDriver if enableLogging is true + */ + readonly logDriver?: LogDriver; + + /** + * The period of time, in seconds, that the Amazon ECS service scheduler ignores unhealthy + * Elastic Load Balancing target health checks after a task has first started. + * + * @default - defaults to 60 seconds if at least one load balancer is in-use and it is not already set + */ + readonly healthCheckGracePeriod?: cdk.Duration; + +} + +/** + * The base class for NetworkLoadBalancedEc2Service and NetworkLoadBalancedFargateService services. + */ +export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct { + public readonly assignPublicIp: boolean; + /** + * The desired number of instantiations of the task definition to keep running on the service. + */ + public readonly desiredCount: number; + + public readonly loadBalancer: NetworkLoadBalancer; + + public readonly listener: NetworkListener; + + public readonly targetGroup: NetworkTargetGroup; + /** + * The cluster that hosts the service. + */ + public readonly cluster: ICluster; + + public readonly logDriver?: LogDriver; + + /** + * Constructs a new instance of the NetworkLoadBalancedServiceBase class. + */ + constructor(scope: cdk.Construct, id: string, props: NetworkLoadBalancedServiceBaseProps) { + super(scope, id); + + if (props.cluster && props.vpc) { + throw new Error(`You can only specify either vpc or cluster. Alternatively, you can leave both blank`); + } + this.cluster = props.cluster || this.getDefaultCluster(this, props.vpc); + + // Create log driver if logging is enabled + const enableLogging = props.enableLogging !== undefined ? props.enableLogging : true; + this.logDriver = props.logDriver !== undefined ? props.logDriver : enableLogging ? this.createAWSLogDriver(this.node.id) : undefined; + + this.assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; + this.desiredCount = props.desiredCount || 1; + + const internetFacing = props.publicLoadBalancer !== undefined ? props.publicLoadBalancer : true; + + const lbProps = { + vpc: this.cluster.vpc, + internetFacing + }; + + this.loadBalancer = new NetworkLoadBalancer(this, 'LB', lbProps); + + const targetProps = { + port: 80 + }; + + if (props.certificate !== undefined) { + throw new Error("Cannot add certificate to an NLB"); + } + + this.listener = this.loadBalancer.addListener('PublicListener', { port: 80 }); + this.targetGroup = this.listener.addTargets('ECS', targetProps); + + if (typeof props.domainName !== 'undefined') { + if (typeof props.domainZone === 'undefined') { + throw new Error('A Route53 hosted domain zone name is required to configure the specified domain name'); + } + + new ARecord(this, "DNS", { + zone: props.domainZone, + recordName: props.domainName, + target: AddressRecordTarget.fromAlias(new LoadBalancerTarget(this.loadBalancer)), + }); + } + + new cdk.CfnOutput(this, 'LoadBalancerDNS', { value: this.loadBalancer.loadBalancerDnsName }); + } + + 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 Cluster || new Cluster(stack, DEFAULT_CLUSTER_ID, { vpc }); + } + + protected addServiceAsTarget(service: BaseService) { + this.targetGroup.addTarget(service); + } + + private createAWSLogDriver(prefix: string): AwsLogDriver { + return new AwsLogDriver({ streamPrefix: prefix }); + } +} diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts new file mode 100644 index 0000000000000..28ea3fcc221be --- /dev/null +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts @@ -0,0 +1,101 @@ +import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; +import { ApplicationLoadBalancedServiceBase, ApplicationLoadBalancedServiceBaseProps } from '../base/application-load-balanced-service-base'; + +/** + * The properties for the ApplicationLoadBalancedEc2Service service. + */ +export interface ApplicationLoadBalancedEc2ServiceProps extends ApplicationLoadBalancedServiceBaseProps { + + /** + * 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. + * + * If your container attempts to exceed the allocated memory, the container + * is terminated. + * + * At least one of memoryLimitMiB and memoryReservationMiB is required. + * + * @default - No memory limit. + */ + readonly memoryLimitMiB?: number; + + /** + * The soft limit (in MiB) of memory to reserve for the container. + * + * When system memory is under contention, Docker attempts to keep the + * container memory within the limit. If the container requires more memory, + * it can consume up to the value specified by the Memory property or all of + * the available memory on the container instance—whichever comes first. + * + * At least one of memoryLimitMiB and memoryReservationMiB is required. + * + * @default - No memory reserved. + */ + readonly memoryReservationMiB?: number; +} + +/** + * An EC2 service running on an ECS cluster fronted by an application load balancer. + */ +export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedServiceBase { + + /** + * The ECS service in this construct + */ + public readonly service: Ec2Service; + + /** + * Constructs a new instance of the ApplicationLoadBalancedEc2Service class. + */ + constructor(scope: Construct, id: string, props: ApplicationLoadBalancedEc2ServiceProps) { + super(scope, id, props); + + const taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', { + executionRole: props.executionRole, + taskRole: props.taskRole + }); + + 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 + }); + + this.service = new Ec2Service(this, "Service", { + cluster: this.cluster, + desiredCount: this.desiredCount, + taskDefinition, + assignPublicIp: this.assignPublicIp, + serviceName: props.serviceName, + healthCheckGracePeriod: props.healthCheckGracePeriod, + }); + this.addServiceAsTarget(this.service); + } +} 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/network-load-balanced-ecs-service.ts similarity index 82% rename from packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts index 6647554504e95..2b6fa77fc946c 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts @@ -1,11 +1,11 @@ import { Ec2Service, Ec2TaskDefinition } from '@aws-cdk/aws-ecs'; import { Construct } from '@aws-cdk/core'; -import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base'; +import { NetworkLoadBalancedServiceBase, NetworkLoadBalancedServiceBaseProps } from '../base/network-load-balanced-service-base'; /** - * The properties for the LoadBalancedEc2Service service. + * The properties for the NetworkLoadBalancedEc2Service service. */ -export interface LoadBalancedEc2ServiceProps extends LoadBalancedServiceBaseProps { +export interface NetworkLoadBalancedEc2ServiceProps extends NetworkLoadBalancedServiceBaseProps { /** * The number of cpu units used by the task. @@ -54,9 +54,9 @@ export interface LoadBalancedEc2ServiceProps extends LoadBalancedServiceBaseProp } /** - * An EC2 service running on an ECS cluster fronted by a load balancer. + * An EC2 service running on an ECS cluster fronted by a network load balancer. */ -export class LoadBalancedEc2Service extends LoadBalancedServiceBase { +export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBase { /** * The ECS service in this construct @@ -64,9 +64,9 @@ export class LoadBalancedEc2Service extends LoadBalancedServiceBase { public readonly service: Ec2Service; /** - * Constructs a new instance of the LoadBalancedEc2Service class. + * Constructs a new instance of the NetworkLoadBalancedEc2Service class. */ - constructor(scope: Construct, id: string, props: LoadBalancedEc2ServiceProps) { + constructor(scope: Construct, id: string, props: NetworkLoadBalancedEc2ServiceProps) { super(scope, id, props); const taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', { diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts new file mode 100644 index 0000000000000..2cef12926461f --- /dev/null +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts @@ -0,0 +1,97 @@ +import { FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs'; +import { Construct } from '@aws-cdk/core'; +import { ApplicationLoadBalancedServiceBase, ApplicationLoadBalancedServiceBaseProps } from '../base/application-load-balanced-service-base'; + +/** + * The properties for the ApplicationLoadBalancedFargateService service. + */ +export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationLoadBalancedServiceBaseProps { + /** + * 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 256 + */ + readonly cpu?: number; + + /** + * The amount (in MiB) of memory used by the task. + * + * This field is required and you must use one of the following values, which determines your range of valid values + * for the cpu parameter: + * + * 512 (0.5 GB), 1024 (1 GB), 2048 (2 GB) - Available cpu values: 256 (.25 vCPU) + * + * 1024 (1 GB), 2048 (2 GB), 3072 (3 GB), 4096 (4 GB) - Available cpu values: 512 (.5 vCPU) + * + * 2048 (2 GB), 3072 (3 GB), 4096 (4 GB), 5120 (5 GB), 6144 (6 GB), 7168 (7 GB), 8192 (8 GB) - Available cpu values: 1024 (1 vCPU) + * + * Between 4096 (4 GB) and 16384 (16 GB) in increments of 1024 (1 GB) - Available cpu values: 2048 (2 vCPU) + * + * Between 8192 (8 GB) and 30720 (30 GB) in increments of 1024 (1 GB) - Available cpu values: 4096 (4 vCPU) + * + * This default is set in the underlying FargateTaskDefinition construct. + * + * @default 512 + */ + readonly memoryLimitMiB?: number; +} + +/** + * A Fargate service running on an ECS cluster fronted by an application load balancer. + */ +export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalancedServiceBase { + + /** + * The Fargate service in this construct. + */ + public readonly service: FargateService; + + /** + * Constructs a new instance of the ApplicationLoadBalancedFargateService class. + */ + constructor(scope: Construct, id: string, props: ApplicationLoadBalancedFargateServiceProps) { + super(scope, id, props); + + const taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { + memoryLimitMiB: props.memoryLimitMiB, + cpu: props.cpu, + 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, + }); + + this.service = new FargateService(this, "Service", { + cluster: this.cluster, + desiredCount: this.desiredCount, + taskDefinition, + assignPublicIp: this.assignPublicIp, + serviceName: props.serviceName, + healthCheckGracePeriod: props.healthCheckGracePeriod, + }); + this.addServiceAsTarget(this.service); + } +} 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/network-load-balanced-fargate-service.ts similarity index 81% rename from packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts rename to packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts index 57af79c5e39ae..df0ea5728fb0f 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts @@ -1,11 +1,11 @@ import { FargateService, FargateTaskDefinition } from '@aws-cdk/aws-ecs'; import { Construct } from '@aws-cdk/core'; -import { LoadBalancedServiceBase, LoadBalancedServiceBaseProps } from '../base/load-balanced-service-base'; +import { NetworkLoadBalancedServiceBase, NetworkLoadBalancedServiceBaseProps } from '../base/network-load-balanced-service-base'; /** - * The properties for the LoadBalancedFargateService service. + * The properties for the NetworkLoadBalancedFargateService service. */ -export interface LoadBalancedFargateServiceProps extends LoadBalancedServiceBaseProps { +export interface NetworkLoadBalancedFargateServiceProps extends NetworkLoadBalancedServiceBaseProps { /** * The number of cpu units used by the task. * @@ -51,9 +51,9 @@ export interface LoadBalancedFargateServiceProps extends LoadBalancedServiceBase } /** - * A Fargate service running on an ECS cluster fronted by a load balancer. + * A Fargate service running on an ECS cluster fronted by a network load balancer. */ -export class LoadBalancedFargateService extends LoadBalancedServiceBase { +export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServiceBase { /** * The Fargate service in this construct. @@ -61,9 +61,9 @@ export class LoadBalancedFargateService extends LoadBalancedServiceBase { public readonly service: FargateService; /** - * Constructs a new instance of the LoadBalancedFargateService class. + * Constructs a new instance of the NetworkLoadBalancedFargateService class. */ - constructor(scope: Construct, id: string, props: LoadBalancedFargateServiceProps) { + constructor(scope: Construct, id: string, props: NetworkLoadBalancedFargateServiceProps) { super(scope, id, props); const taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts index 4efc2fb93715d..890387c7b77c3 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/index.ts @@ -2,9 +2,13 @@ export * from './ecs/queue-processing-ecs-service'; export * from './fargate/queue-processing-fargate-service'; export * from './base/queue-processing-service-base'; -export * from './ecs/load-balanced-ecs-service'; -export * from './fargate/load-balanced-fargate-service'; -export * from './base/load-balanced-service-base'; +export * from './ecs/network-load-balanced-ecs-service'; +export * from './fargate/network-load-balanced-fargate-service'; +export * from './base/network-load-balanced-service-base'; + +export * from './ecs/application-load-balanced-ecs-service'; +export * from './fargate/application-load-balanced-fargate-service'; +export * from './base/application-load-balanced-service-base'; export * from './ecs/scheduled-ecs-task'; export * from './fargate/scheduled-fargate-task'; diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts index 39a1edf8c42cc..3db1e84fd3dce 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/ec2/test.l3s.ts @@ -17,7 +17,7 @@ export = { cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); // WHEN - new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedEc2Service(stack, 'Service', { cluster, memoryLimitMiB: 1024, image: ecs.ContainerImage.fromRegistry('test'), @@ -63,7 +63,7 @@ export = { const vpc = new ec2.Vpc(stack, 'VPC'); // WHEN - new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedEc2Service(stack, 'Service', { vpc, memoryLimitMiB: 1024, image: ecs.ContainerImage.fromRegistry('test'), @@ -89,10 +89,9 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - test.throws(() => new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { + test.throws(() => new ecsPatterns.NetworkLoadBalancedEc2Service(stack, 'Service', { cluster, vpc, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") })); @@ -107,7 +106,7 @@ export = { cluster.addCapacity('DefaultAutoScalingGroup', { instanceType: new ec2.InstanceType('t2.micro') }); // WHEN - new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedEc2Service(stack, 'Service', { cluster, memoryReservationMiB: 1024, image: ecs.ContainerImage.fromRegistry('test') @@ -134,7 +133,7 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), desiredCount: 2, @@ -190,7 +189,7 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), desiredCount: 2, @@ -237,7 +236,7 @@ export = { const zone = new PublicHostedZone(stack, 'HostedZone', { zoneName: 'example.com' }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), domainName: 'api.example.com', @@ -283,7 +282,7 @@ export = { // THEN test.throws(() => { - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), domainName: 'api.example.com' @@ -299,7 +298,7 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), desiredCount: 2, @@ -348,7 +347,7 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { cluster, image: ecs.ContainerImage.fromRegistry('test'), desiredCount: 2, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.ts index 7a391c019303f..f17494760dd38 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.asset-image.ts @@ -14,7 +14,7 @@ Array.isArray(cluster); Array.isArray(path); // Instantiate Fargate Service with just cluster and image -const fargateService = new ecsPatterns.LoadBalancedFargateService(stack, "FargateService", { +const fargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(stack, "FargateService", { cluster, containerPort: 8000, image: new ecs.AssetImage(path.join(__dirname, '..', 'demo-image')), diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.ts index ce19c117b8504..842b0a2eda3eb 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.executionrole.ts @@ -11,7 +11,7 @@ const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 2 }); const cluster = new ecs.Cluster(stack, 'FargateCluster', { vpc }); -new ecsPatterns.LoadBalancedFargateService(stack, 'L3', { +new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'L3', { cluster, memoryLimitMiB: 1024, cpu: 512, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-autocreate.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-autocreate.ts index 6a400f37d0568..1d99268e21a6e 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-autocreate.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-autocreate.ts @@ -5,13 +5,13 @@ import ecsPatterns = require('../../lib'); const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-ecs-integ'); -new ecsPatterns.LoadBalancedFargateService(stack, 'L3', { +new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'L3', { memoryLimitMiB: 1024, cpu: 512, image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), }); -new ecsPatterns.LoadBalancedFargateService(stack, 'L3b', { +new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'L3b', { memoryLimitMiB: 1024, cpu: 512, image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-vpconly.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-vpconly.ts index 173f428e29b6a..51dba7ed27832 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-vpconly.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3-vpconly.ts @@ -7,7 +7,7 @@ const app = new cdk.App(); const stack = new cdk.Stack(app, 'aws-ecs-integ'); const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 2 }); -new ecsPatterns.LoadBalancedFargateService(stack, 'L3', { +new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'L3', { vpc, memoryLimitMiB: 1024, cpu: 512, @@ -15,14 +15,14 @@ new ecsPatterns.LoadBalancedFargateService(stack, 'L3', { }); const vpc2 = new ec2.Vpc(stack, 'Vpc2', { maxAzs: 2 }); -new ecsPatterns.LoadBalancedFargateService(stack, 'L3b', { +new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'L3b', { vpc: vpc2, memoryLimitMiB: 1024, cpu: 512, image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), }); -new ecsPatterns.LoadBalancedFargateService(stack, 'L3c', { +new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'L3c', { vpc: vpc2, memoryLimitMiB: 1024, cpu: 512, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.ts index dc098d3d7033c..9ca2b7ce09593 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/integ.l3.ts @@ -10,7 +10,7 @@ const vpc = new ec2.Vpc(stack, 'Vpc', { maxAzs: 2 }); const cluster = new ecs.Cluster(stack, 'FargateCluster', { vpc }); -new ecsPatterns.LoadBalancedFargateService(stack, 'L3', { +new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'L3', { cluster, memoryLimitMiB: 1024, cpu: 512, diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts index fe0104a929df0..64119a5021b98 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts @@ -17,10 +17,9 @@ export = { // WHEN const cert = new Certificate(stack, 'Cert', { domainName: '*.example.com' }); const toThrow = () => { - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, certificate: cert, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") }); }; @@ -37,9 +36,8 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") }); @@ -58,10 +56,9 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - test.throws(() => new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + test.throws(() => new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, vpc, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") })); @@ -83,9 +80,8 @@ export = { }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"), executionRole }); @@ -110,9 +106,8 @@ export = { }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"), taskRole }); @@ -130,9 +125,8 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"), containerName: 'bob' }); @@ -150,9 +144,8 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"), }); @@ -169,9 +162,8 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"), serviceName: 'bob', }); @@ -188,9 +180,8 @@ export = { const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { + new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, - loadBalancerType: ecsPatterns.LoadBalancerType.NETWORK, image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"), }); @@ -205,8 +196,7 @@ export = { const stack = new cdk.Stack(); // WHEN - new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { - loadBalancerType: ecsPatterns.LoadBalancerType.APPLICATION, + new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app"), healthCheckGracePeriod: cdk.Duration.seconds(600), }); From efb205dc83fc45ce8ca3ca3667e43eee7896afaf Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Mon, 19 Aug 2019 18:40:13 -0700 Subject: [PATCH 2/7] Update README --- packages/@aws-cdk/aws-ecs-patterns/README.md | 51 +++++++++++++++++--- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/README.md b/packages/@aws-cdk/aws-ecs-patterns/README.md index 4712f6104aa6f..a687e352956e6 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/README.md +++ b/packages/@aws-cdk/aws-ecs-patterns/README.md @@ -17,18 +17,19 @@ This library provides higher-level Amazon ECS constructs which follow common architectural patterns. It contains: -* Load Balanced Services +* Application Load Balanced Services +* Network Load Balanced Services * Queue Processing Services * Scheduled Tasks (cron jobs) -## Load Balanced Services +## Application Load Balanced Services -To define an Amazon ECS service that is behind a load balancer, instantiate one of the following: +To define an Amazon ECS service that is behind an application load balancer, instantiate one of the following: -* `LoadBalancedEc2Service` +* `ApplicationLoadBalancedEc2Service` ```ts -const loadBalancedEcsService = new ecsPatterns.LoadBalancedEc2Service(stack, 'Service', { +const loadBalancedEcsService = new ecsPatterns.ApplicationLoadBalancedEc2Service(stack, 'Service', { cluster, memoryLimitMiB: 1024, image: ecs.ContainerImage.fromRegistry('test'), @@ -40,10 +41,45 @@ const loadBalancedEcsService = new ecsPatterns.LoadBalancedEc2Service(stack, 'Se }); ``` -* `LoadBalancedFargateService` +* `ApplicationLoadBalancedFargateService` ```ts -const loadBalancedFargateService = new ecsPatterns.LoadBalancedFargateService(stack, 'Service', { +const loadBalancedFargateService = new ecsPatterns.ApplicationLoadBalancedFargateService(stack, 'Service', { + cluster, + memoryLimitMiB: 1024, + cpu: 512, + image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample"), +}); +``` + +Instead of providing a cluster you can specify a VPC and CDK will create a new ECS cluster. +If you deploy multiple services CDK will only create on cluster per VPC. + +You can omit `cluster` and `vpc` to let CDK create a new VPC with two AZs and create a cluster inside this VPC. + +## Network Load Balanced Services + +To define an Amazon ECS service that is behind a network load balancer, instantiate one of the following: + +* `NetworkLoadBalancedEc2Service` + +```ts +const loadBalancedEcsService = new ecsPatterns.NetworkLoadBalancedEc2Service(stack, 'Service', { + cluster, + memoryLimitMiB: 1024, + image: ecs.ContainerImage.fromRegistry('test'), + desiredCount: 2, + environment: { + TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value", + TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value" + } +}); +``` + +* `NetworkLoadBalancedFargateService` + +```ts +const loadBalancedFargateService = new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { cluster, memoryLimitMiB: 1024, cpu: 512, @@ -101,7 +137,6 @@ const queueProcessingFargateService = new QueueProcessingFargateService(stack, ' To define a task that runs periodically, instantiate an `ScheduledEc2Task`: - ```ts // Instantiate an Amazon EC2 Task to run at a scheduled interval const ecsScheduledTask = new ScheduledEc2Task(this, 'ScheduledTask', { From d8f9132ad8be6d1157c734a028a592466fb51aa9 Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Tue, 20 Aug 2019 07:11:11 -0700 Subject: [PATCH 3/7] Remove certificate from Network Load Balanced Service --- .../network-load-balanced-service-base.ts | 13 ----------- .../test.load-balanced-fargate-service.ts | 22 ------------------- 2 files changed, 35 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts index 5db3e4b924f68..fef1763c244be 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts @@ -1,4 +1,3 @@ -import { ICertificate } from '@aws-cdk/aws-certificatemanager'; import { IVpc } from '@aws-cdk/aws-ec2'; import { AwsLogDriver, BaseService, Cluster, ContainerImage, ICluster, LogDriver, Secret } from '@aws-cdk/aws-ecs'; import { NetworkListener, NetworkLoadBalancer, NetworkTargetGroup } from '@aws-cdk/aws-elasticloadbalancingv2'; @@ -60,14 +59,6 @@ export interface NetworkLoadBalancedServiceBaseProps { */ readonly desiredCount?: number; - /** - * Certificate Manager certificate to associate with the load balancer. - * Setting this option will set the load balancer port to 443. - * - * @default - No certificate associated with the load balancer. - */ - readonly certificate?: ICertificate; - /** * The environment variables to pass to the container. * @@ -208,10 +199,6 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct { port: 80 }; - if (props.certificate !== undefined) { - throw new Error("Cannot add certificate to an NLB"); - } - this.listener = this.loadBalancer.addListener('PublicListener', { port: 80 }); this.targetGroup = this.listener.addTargets('ECS', targetProps); diff --git a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts index 64119a5021b98..0cf6044ba7ade 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/test/fargate/test.load-balanced-fargate-service.ts @@ -1,5 +1,4 @@ import { expect, haveResourceLike, SynthUtils } 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 iam = require('@aws-cdk/aws-iam'); @@ -8,27 +7,6 @@ import { Test } from 'nodeunit'; import ecsPatterns = require('../../lib'); export = { - 'certificate requires an application load balancer'(test: Test) { - // GIVEN - const stack = new cdk.Stack(); - const vpc = new ec2.Vpc(stack, 'VPC'); - const cluster = new ecs.Cluster(stack, 'Cluster', { vpc }); - - // WHEN - const cert = new Certificate(stack, 'Cert', { domainName: '*.example.com' }); - const toThrow = () => { - new ecsPatterns.NetworkLoadBalancedFargateService(stack, 'Service', { - cluster, - certificate: cert, - image: ecs.ContainerImage.fromRegistry("/aws/aws-example-app") - }); - }; - - // THEN - test.throws(() => toThrow(), /Cannot add certificate to an NLB/); - test.done(); - }, - 'setting loadBalancerType to Network creates an NLB'(test: Test) { // GIVEN const stack = new cdk.Stack(); From 69350f2f5c247b891ee78987a73d306d6a7a2d23 Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Tue, 20 Aug 2019 09:20:06 -0700 Subject: [PATCH 4/7] Add TaskDef as a property on the construct --- .../lib/ecs/application-load-balanced-ecs-service.ts | 12 ++++++++---- .../lib/ecs/network-load-balanced-ecs-service.ts | 12 ++++++++---- .../lib/ecs/queue-processing-ecs-service.ts | 12 ++++++++---- .../aws-ecs-patterns/lib/ecs/scheduled-ecs-task.ts | 2 +- .../application-load-balanced-fargate-service.ts | 10 +++++++--- .../fargate/network-load-balanced-fargate-service.ts | 10 +++++++--- .../lib/fargate/queue-processing-fargate-service.ts | 12 ++++++++---- .../lib/fargate/scheduled-fargate-task.ts | 5 ++--- 8 files changed, 49 insertions(+), 26 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts index 28ea3fcc221be..904dc8844d533 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts @@ -59,9 +59,13 @@ export interface ApplicationLoadBalancedEc2ServiceProps extends ApplicationLoadB export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedServiceBase { /** - * The ECS service in this construct + * The EC2 service in this construct. */ public readonly service: Ec2Service; + /** + * The EC2 Task Definition in this construct. + */ + public readonly taskDefinition: Ec2TaskDefinition; /** * Constructs a new instance of the ApplicationLoadBalancedEc2Service class. @@ -69,13 +73,13 @@ export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedSe constructor(scope: Construct, id: string, props: ApplicationLoadBalancedEc2ServiceProps) { super(scope, id, props); - const taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', { + this.taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', { executionRole: props.executionRole, taskRole: props.taskRole }); const containerName = props.containerName !== undefined ? props.containerName : 'web'; - const container = taskDefinition.addContainer(containerName, { + const container = this.taskDefinition.addContainer(containerName, { image: props.image, cpu: props.cpu, memoryLimitMiB: props.memoryLimitMiB, @@ -91,7 +95,7 @@ export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedSe this.service = new Ec2Service(this, "Service", { cluster: this.cluster, desiredCount: this.desiredCount, - taskDefinition, + taskDefinition: this.taskDefinition, assignPublicIp: this.assignPublicIp, serviceName: props.serviceName, healthCheckGracePeriod: props.healthCheckGracePeriod, diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts index 2b6fa77fc946c..81bd7eaf98b9b 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts @@ -59,9 +59,13 @@ export interface NetworkLoadBalancedEc2ServiceProps extends NetworkLoadBalancedS export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBase { /** - * The ECS service in this construct + * The ECS service in this construct. */ public readonly service: Ec2Service; + /** + * The EC2 Task Definition in this construct. + */ + public readonly taskDefinition: Ec2TaskDefinition; /** * Constructs a new instance of the NetworkLoadBalancedEc2Service class. @@ -69,13 +73,13 @@ export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBas constructor(scope: Construct, id: string, props: NetworkLoadBalancedEc2ServiceProps) { super(scope, id, props); - const taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', { + this.taskDefinition = new Ec2TaskDefinition(this, 'TaskDef', { executionRole: props.executionRole, taskRole: props.taskRole }); const containerName = props.containerName !== undefined ? props.containerName : 'web'; - const container = taskDefinition.addContainer(containerName, { + const container = this.taskDefinition.addContainer(containerName, { image: props.image, cpu: props.cpu, memoryLimitMiB: props.memoryLimitMiB, @@ -91,7 +95,7 @@ export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBas this.service = new Ec2Service(this, "Service", { cluster: this.cluster, desiredCount: this.desiredCount, - taskDefinition, + taskDefinition: this.taskDefinition, assignPublicIp: this.assignPublicIp, serviceName: props.serviceName, healthCheckGracePeriod: props.healthCheckGracePeriod, 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 2002b37fd5949..932216bd9d25e 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 @@ -59,9 +59,13 @@ export interface QueueProcessingEc2ServiceProps extends QueueProcessingServiceBa export class QueueProcessingEc2Service extends QueueProcessingServiceBase { /** - * The ECS service in this construct + * The EC2 service in this construct. */ public readonly service: Ec2Service; + /** + * The EC2 task definition in this construct + */ + public readonly taskDefinition: Ec2TaskDefinition; /** * Constructs a new instance of the QueueProcessingEc2Service class. @@ -70,8 +74,8 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase { super(scope, id, props); // Create a Task Definition for the container to start - const taskDefinition = new Ec2TaskDefinition(this, 'QueueProcessingTaskDef'); - taskDefinition.addContainer('QueueProcessingContainer', { + this.taskDefinition = new Ec2TaskDefinition(this, 'QueueProcessingTaskDef'); + this.taskDefinition.addContainer('QueueProcessingContainer', { image: props.image, memoryLimitMiB: props.memoryLimitMiB, memoryReservationMiB: props.memoryReservationMiB, @@ -87,7 +91,7 @@ export class QueueProcessingEc2Service extends QueueProcessingServiceBase { this.service = new Ec2Service(this, 'QueueProcessingService', { cluster: this.cluster, desiredCount: this.desiredCount, - taskDefinition + taskDefinition: this.taskDefinition }); this.configureAutoscalingForService(this.service); } 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 feeb5ee43740b..65e182bfbbe2b 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 @@ -46,7 +46,7 @@ export interface ScheduledEc2TaskProps extends ScheduledTaskBaseProps { export class ScheduledEc2Task extends ScheduledTaskBase { /** - * The ECS service in this construct + * The EC2 task definition in this construct. */ public readonly taskDefinition: Ec2TaskDefinition; diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts index 2cef12926461f..28c71e9c44677 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts @@ -59,6 +59,10 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc * The Fargate service in this construct. */ public readonly service: FargateService; + /** + * The Fargate task definition in this construct. + */ + public readonly taskDefinition: FargateTaskDefinition; /** * Constructs a new instance of the ApplicationLoadBalancedFargateService class. @@ -66,7 +70,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc constructor(scope: Construct, id: string, props: ApplicationLoadBalancedFargateServiceProps) { super(scope, id, props); - const taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { + this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { memoryLimitMiB: props.memoryLimitMiB, cpu: props.cpu, executionRole: props.executionRole, @@ -74,7 +78,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc }); const containerName = props.containerName !== undefined ? props.containerName : 'web'; - const container = taskDefinition.addContainer(containerName, { + const container = this.taskDefinition.addContainer(containerName, { image: props.image, logging: this.logDriver, environment: props.environment, @@ -87,7 +91,7 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc this.service = new FargateService(this, "Service", { cluster: this.cluster, desiredCount: this.desiredCount, - taskDefinition, + taskDefinition: this.taskDefinition, assignPublicIp: this.assignPublicIp, serviceName: props.serviceName, healthCheckGracePeriod: props.healthCheckGracePeriod, diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts index df0ea5728fb0f..5950543023c11 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts @@ -59,6 +59,10 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic * The Fargate service in this construct. */ public readonly service: FargateService; + /** + * The Fargate task definition in this construct. + */ + public readonly taskDefinition: FargateTaskDefinition; /** * Constructs a new instance of the NetworkLoadBalancedFargateService class. @@ -66,7 +70,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic constructor(scope: Construct, id: string, props: NetworkLoadBalancedFargateServiceProps) { super(scope, id, props); - const taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { + this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { memoryLimitMiB: props.memoryLimitMiB, cpu: props.cpu, executionRole: props.executionRole, @@ -74,7 +78,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic }); const containerName = props.containerName !== undefined ? props.containerName : 'web'; - const container = taskDefinition.addContainer(containerName, { + const container = this.taskDefinition.addContainer(containerName, { image: props.image, logging: this.logDriver, environment: props.environment, @@ -87,7 +91,7 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic this.service = new FargateService(this, "Service", { cluster: this.cluster, desiredCount: this.desiredCount, - taskDefinition, + taskDefinition: this.taskDefinition, assignPublicIp: this.assignPublicIp, serviceName: props.serviceName, healthCheckGracePeriod: props.healthCheckGracePeriod, 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 27647cd4815fb..4c0564893adbc 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 @@ -49,9 +49,13 @@ export interface QueueProcessingFargateServiceProps extends QueueProcessingServi */ export class QueueProcessingFargateService extends QueueProcessingServiceBase { /** - * The Fargate service in this construct + * The Fargate service in this construct. */ public readonly service: FargateService; + /** + * The Fargate task definition in this construct. + */ + public readonly taskDefinition: FargateTaskDefinition; /** * Constructs a new instance of the QueueProcessingFargateService class. @@ -60,11 +64,11 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase { super(scope, id, props); // Create a Task Definition for the container to start - const taskDefinition = new FargateTaskDefinition(this, 'QueueProcessingTaskDef', { + this.taskDefinition = new FargateTaskDefinition(this, 'QueueProcessingTaskDef', { memoryLimitMiB: props.memoryLimitMiB || 512, cpu: props.cpu || 256, }); - taskDefinition.addContainer('QueueProcessingContainer', { + this.taskDefinition.addContainer('QueueProcessingContainer', { image: props.image, command: props.command, environment: this.environment, @@ -77,7 +81,7 @@ export class QueueProcessingFargateService extends QueueProcessingServiceBase { this.service = new FargateService(this, 'QueueProcessingFargateService', { cluster: this.cluster, desiredCount: this.desiredCount, - taskDefinition + taskDefinition: this.taskDefinition }); this.configureAutoscalingForService(this.service); } 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 0ff7f087e96a1..b295a725fbabe 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 @@ -37,7 +37,7 @@ export interface ScheduledFargateTaskProps extends ScheduledTaskBaseProps { */ export class ScheduledFargateTask extends ScheduledTaskBase { /** - * The ECS service in this construct + * The Fargate task definition in this construct. */ public readonly taskDefinition: FargateTaskDefinition; @@ -47,8 +47,7 @@ export class ScheduledFargateTask extends ScheduledTaskBase { 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 - this. taskDefinition = new FargateTaskDefinition(this, 'ScheduledTaskDef', { + this.taskDefinition = new FargateTaskDefinition(this, 'ScheduledTaskDef', { memoryLimitMiB: props.memoryLimitMiB || 512, cpu: props.cpu || 256, }); From c60535cd4aeb7ee558672951407ee8137a24550a Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Tue, 20 Aug 2019 11:57:21 -0700 Subject: [PATCH 5/7] Only allow assign public ip on fargate services --- .../base/application-load-balanced-service-base.ts | 11 +---------- .../lib/base/network-load-balanced-service-base.ts | 13 ++----------- .../ecs/application-load-balanced-ecs-service.ts | 2 +- .../lib/ecs/network-load-balanced-ecs-service.ts | 2 +- .../application-load-balanced-fargate-service.ts | 10 ++++++++++ .../network-load-balanced-fargate-service.ts | 10 ++++++++++ 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts index 7d23d04e3dde7..49de4686ac198 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts @@ -90,14 +90,7 @@ export interface ApplicationLoadBalancedServiceBaseProps { readonly enableLogging?: boolean; /** - * Determines whether the service will be assigned a public IP address. - * - * @default false - */ - readonly publicTasks?: boolean; - - /** - * The domain name for the service, e.g. "api.example.com." + * The domain name for the service, e.g. "api.example.com" * * @default - No domain name. */ @@ -159,7 +152,6 @@ export interface ApplicationLoadBalancedServiceBaseProps { * The base class for ApplicationLoadBalancedEc2Service and ApplicationLoadBalancedFargateService services. */ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct { - public readonly assignPublicIp: boolean; /** * The desired number of instantiations of the task definition to keep running on the service. */ @@ -192,7 +184,6 @@ export abstract class ApplicationLoadBalancedServiceBase extends cdk.Construct { const enableLogging = props.enableLogging !== undefined ? props.enableLogging : true; this.logDriver = props.logDriver !== undefined ? props.logDriver : enableLogging ? this.createAWSLogDriver(this.node.id) : undefined; - this.assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; this.desiredCount = props.desiredCount || 1; const internetFacing = props.publicLoadBalancer !== undefined ? props.publicLoadBalancer : true; diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts index fef1763c244be..1f212489006e7 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts @@ -81,21 +81,14 @@ export interface NetworkLoadBalancedServiceBaseProps { readonly enableLogging?: boolean; /** - * Determines whether the Service will be assigned a public IP address. - * - * @default false - */ - readonly publicTasks?: boolean; - - /** - * Domain name for the service, e.g. api.example.com + * The domain name for the service, e.g. "api.example.com" * * @default - No domain name. */ readonly domainName?: string; /** - * Route53 hosted zone for the domain, e.g. "example.com." + * The Route53 hosted zone for the domain, e.g. "example.com." * * @default - No Route53 hosted domain zone. */ @@ -150,7 +143,6 @@ export interface NetworkLoadBalancedServiceBaseProps { * The base class for NetworkLoadBalancedEc2Service and NetworkLoadBalancedFargateService services. */ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct { - public readonly assignPublicIp: boolean; /** * The desired number of instantiations of the task definition to keep running on the service. */ @@ -183,7 +175,6 @@ export abstract class NetworkLoadBalancedServiceBase extends cdk.Construct { const enableLogging = props.enableLogging !== undefined ? props.enableLogging : true; this.logDriver = props.logDriver !== undefined ? props.logDriver : enableLogging ? this.createAWSLogDriver(this.node.id) : undefined; - this.assignPublicIp = props.publicTasks !== undefined ? props.publicTasks : false; this.desiredCount = props.desiredCount || 1; const internetFacing = props.publicLoadBalancer !== undefined ? props.publicLoadBalancer : true; diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts index 904dc8844d533..9b2ba5e497de7 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/application-load-balanced-ecs-service.ts @@ -96,7 +96,7 @@ export class ApplicationLoadBalancedEc2Service extends ApplicationLoadBalancedSe cluster: this.cluster, desiredCount: this.desiredCount, taskDefinition: this.taskDefinition, - assignPublicIp: this.assignPublicIp, + assignPublicIp: false, serviceName: props.serviceName, healthCheckGracePeriod: props.healthCheckGracePeriod, }); diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts index 81bd7eaf98b9b..f7794ef5ced35 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/ecs/network-load-balanced-ecs-service.ts @@ -96,7 +96,7 @@ export class NetworkLoadBalancedEc2Service extends NetworkLoadBalancedServiceBas cluster: this.cluster, desiredCount: this.desiredCount, taskDefinition: this.taskDefinition, - assignPublicIp: this.assignPublicIp, + assignPublicIp: false, serviceName: props.serviceName, healthCheckGracePeriod: props.healthCheckGracePeriod, }); diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts index 28c71e9c44677..f86e1716a9094 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts @@ -48,6 +48,13 @@ export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationL * @default 512 */ readonly memoryLimitMiB?: number; + + /** + * Determines whether the Service will be assigned a public IP address. + * + * @default false + */ + readonly assignPublicIp?: boolean; } /** @@ -55,6 +62,7 @@ export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationL */ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalancedServiceBase { + public readonly assignPublicIp: boolean; /** * The Fargate service in this construct. */ @@ -70,6 +78,8 @@ export class ApplicationLoadBalancedFargateService extends ApplicationLoadBalanc constructor(scope: Construct, id: string, props: ApplicationLoadBalancedFargateServiceProps) { super(scope, id, props); + this.assignPublicIp = props.assignPublicIp !== undefined ? props.assignPublicIp : false; + this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { memoryLimitMiB: props.memoryLimitMiB, cpu: props.cpu, diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts index 5950543023c11..38bca6f8d2a49 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts @@ -48,6 +48,13 @@ export interface NetworkLoadBalancedFargateServiceProps extends NetworkLoadBalan * @default 512 */ readonly memoryLimitMiB?: number; + + /** + * Determines whether the Service will be assigned a public IP address. + * + * @default false + */ + readonly assignPublicIp?: boolean; } /** @@ -55,6 +62,7 @@ export interface NetworkLoadBalancedFargateServiceProps extends NetworkLoadBalan */ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServiceBase { + public readonly assignPublicIp: boolean; /** * The Fargate service in this construct. */ @@ -70,6 +78,8 @@ export class NetworkLoadBalancedFargateService extends NetworkLoadBalancedServic constructor(scope: Construct, id: string, props: NetworkLoadBalancedFargateServiceProps) { super(scope, id, props); + this.assignPublicIp = props.assignPublicIp !== undefined ? props.assignPublicIp : false; + this.taskDefinition = new FargateTaskDefinition(this, 'TaskDef', { memoryLimitMiB: props.memoryLimitMiB, cpu: props.cpu, From 4049cc9ebcde6af9151f8cfe0e9f166de1052fc4 Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Wed, 21 Aug 2019 10:25:47 -0700 Subject: [PATCH 6/7] Update documentation --- .../application-load-balanced-service-base.ts | 2 +- .../base/network-load-balanced-service-base.ts | 18 +++++++++--------- .../lib/base/queue-processing-service-base.ts | 16 ++++++++-------- .../lib/base/scheduled-task-base.ts | 16 ++++++++-------- ...pplication-load-balanced-fargate-service.ts | 2 +- .../network-load-balanced-fargate-service.ts | 2 +- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts index 49de4686ac198..023bcfb1af74f 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/application-load-balanced-service-base.ts @@ -90,7 +90,7 @@ export interface ApplicationLoadBalancedServiceBaseProps { readonly enableLogging?: boolean; /** - * The domain name for the service, e.g. "api.example.com" + * The domain name for the service, e.g. "api.example.com." * * @default - No domain name. */ diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts index 1f212489006e7..801040a4a1a03 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts @@ -13,16 +13,16 @@ export interface NetworkLoadBalancedServiceBaseProps { /** * The name of the cluster that hosts the service. * - * You can only specify either vpc or cluster. Alternatively, you can leave both blank. - * @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. + * If a cluster is specified, the vpc construct should be omitted. Alternatively, you can omit both cluster and vpc. + * @default - create a new cluster; if both cluster and vpc are omitted, a new VPC will be created for you. */ readonly cluster?: ICluster; /** - * The VPC where the ECS instances will be running or the ENIs will be deployed. + * The VPC where the container instances will be launched or the elastic network interfaces (ENIs) will be deployed. * - * You can only specify either vpc or cluster. Alternatively, you can leave both blank. - * @default - uses the vpc defined in the cluster or creates a new one. + * If a vpc is specified, the cluster construct should be omitted. Alternatively, you can omit both vpc and cluster. + * @default - uses the VPC defined in the cluster or creates a new VPC. */ readonly vpc?: IVpc; @@ -67,7 +67,7 @@ export interface NetworkLoadBalancedServiceBaseProps { readonly environment?: { [key: string]: string }; /** - * The secret environment variables to pass to the container + * The secret to expose to the container as an environment variable. * * @default - No secret environment variables. */ @@ -81,7 +81,7 @@ export interface NetworkLoadBalancedServiceBaseProps { readonly enableLogging?: boolean; /** - * The domain name for the service, e.g. "api.example.com" + * The domain name for the service, e.g. "api.example.com." * * @default - No domain name. */ @@ -95,7 +95,7 @@ export interface NetworkLoadBalancedServiceBaseProps { readonly domainZone?: IHostedZone; /** - * Override for the Fargate Task Definition execution role + * The name of the task execution IAM role that grants the Amazon ECS container agent permission to call AWS APIs on your behalf. * * @default - No value */ @@ -123,7 +123,7 @@ export interface NetworkLoadBalancedServiceBaseProps { readonly serviceName?: string; /** - * The LogDriver to use for logging. + * The log driver to use. * * @default - AwsLogDriver if enableLogging is true */ 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 2006243311d99..23a980e1579fd 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 @@ -11,16 +11,16 @@ export interface QueueProcessingServiceBaseProps { /** * The name of the cluster that hosts the service. * - * You can only specify either vpc or cluster. Alternatively, you can leave both blank. - * @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. + * If a cluster is specified, the vpc construct should be omitted. Alternatively, you can omit both cluster and vpc. + * @default - create a new cluster; if both cluster and vpc are omitted, a new VPC will be created for you. */ readonly cluster?: ICluster; /** - * The VPC where the ECS instances will be running or the ENIs will be deployed. + * The VPC where the container instances will be launched or the elastic network interfaces (ENIs) will be deployed. * - * You can only specify either vpc or cluster. Alternatively, you can leave both blank. - * @default - uses the vpc defined in the cluster or creates a new one. + * If a vpc is specified, the cluster construct should be omitted. Alternatively, you can omit both vpc and cluster. + * @default - uses the VPC defined in the cluster or creates a new VPC. */ readonly vpc?: IVpc; @@ -63,7 +63,7 @@ export interface QueueProcessingServiceBaseProps { readonly environment?: { [key: string]: string }; /** - * The secret environment variables to pass to the container. + * The secret to expose to the container as an environment variable. * * @default - No secret environment variables. */ @@ -97,9 +97,9 @@ export interface QueueProcessingServiceBaseProps { readonly scalingSteps?: ScalingInterval[]; /** - * The LogDriver to use for logging. + * The log driver to use. * - * @default AwsLogDriver if enableLogging is true + * @default - AwsLogDriver if enableLogging is true */ readonly logDriver?: LogDriver; } 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 ea9f8632ad962..c325492c41718 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 @@ -12,16 +12,16 @@ export interface ScheduledTaskBaseProps { /** * The name of the cluster that hosts the service. * - * You can only specify either vpc or cluster. Alternatively, you can leave both blank. - * @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. + * If a cluster is specified, the vpc construct should be omitted. Alternatively, you can omit both cluster and vpc. + * @default - create a new cluster; if both cluster and vpc are omitted, a new VPC will be created for you. */ readonly cluster?: ICluster; /** - * The VPC where the ECS instances will be running or the ENIs will be deployed. + * The VPC where the container instances will be launched or the elastic network interfaces (ENIs) will be deployed. * - * You can only specify either vpc or cluster. Alternatively, you can leave both blank. - * @default - uses the vpc defined in the cluster or creates a new one. + * If a vpc is specified, the cluster construct should be omitted. Alternatively, you can omit both vpc and cluster. + * @default - uses the VPC defined in the cluster or creates a new VPC. */ readonly vpc?: IVpc; @@ -63,16 +63,16 @@ export interface ScheduledTaskBaseProps { readonly environment?: { [key: string]: string }; /** - * The secret environment variables to pass to the container + * The secret to expose to the container as an environment variable. * * @default - No secret environment variables. */ readonly secrets?: { [key: string]: Secret }; /** - * The LogDriver to use for logging. + * The log driver to use. * - * @default AwsLogDriver if enableLogging is true + * @default - AwsLogDriver if enableLogging is true */ readonly logDriver?: LogDriver; } diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts index f86e1716a9094..94e9604347957 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/application-load-balanced-fargate-service.ts @@ -50,7 +50,7 @@ export interface ApplicationLoadBalancedFargateServiceProps extends ApplicationL readonly memoryLimitMiB?: number; /** - * Determines whether the Service will be assigned a public IP address. + * Determines whether the service will be assigned a public IP address. * * @default false */ diff --git a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts index 38bca6f8d2a49..fa959c27ab815 100644 --- a/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts +++ b/packages/@aws-cdk/aws-ecs-patterns/lib/fargate/network-load-balanced-fargate-service.ts @@ -50,7 +50,7 @@ export interface NetworkLoadBalancedFargateServiceProps extends NetworkLoadBalan readonly memoryLimitMiB?: number; /** - * Determines whether the Service will be assigned a public IP address. + * Determines whether the service will be assigned a public IP address. * * @default false */ From 3d89e4cfbcd0e787efecc7379df2fb4564d8e53d Mon Sep 17 00:00:00 2001 From: Piradeep Kandasamy Date: Thu, 22 Aug 2019 13:12:08 -0700 Subject: [PATCH 7/7] Add to allowed-breaking-changes doc --- allowed-breaking-changes.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/allowed-breaking-changes.txt b/allowed-breaking-changes.txt index 2d9ccc62c0ae0..2fe195fc56fcc 100644 --- a/allowed-breaking-changes.txt +++ b/allowed-breaking-changes.txt @@ -12,3 +12,4 @@ removed:@aws-cdk/aws-apigateway.HttpIntegration.props removed:@aws-cdk/aws-apigateway.Integration.props removed:@aws-cdk/aws-apigateway.LambdaIntegration.props removed:@aws-cdk/aws-apigateway.MockIntegration.props +removed:@aws-cdk/aws-ecs-patterns.LoadBalancerType