Skip to content

Commit

Permalink
One EcsCluster to Rule Them All
Browse files Browse the repository at this point in the history
  • Loading branch information
SoManyHs committed Nov 6, 2018
1 parent ed0956a commit 33a35b9
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 191 deletions.
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/ec2/ec2-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import elb = require('@aws-cdk/aws-elasticloadbalancing');
import cdk = require('@aws-cdk/cdk');
import { BaseService, BaseServiceProps } from '../base/base-service';
import { NetworkMode } from '../base/task-definition';
import { IEcsCluster } from '../ecs-cluster';
import { cloudformation } from '../ecs.generated';
import { IEc2Cluster } from './ec2-cluster';
import { Ec2TaskDefinition } from './ec2-task-definition';

/**
Expand All @@ -15,7 +15,7 @@ export interface Ec2ServiceProps extends BaseServiceProps {
/**
* Cluster where service will be deployed
*/
cluster: IEc2Cluster;
cluster: IEcsCluster;

/**
* Task Definition used for running tasks in the service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,64 @@ import cloudwatch = require ('@aws-cdk/aws-cloudwatch');
import ec2 = require('@aws-cdk/aws-ec2');
import iam = require('@aws-cdk/aws-iam');
import cdk = require('@aws-cdk/cdk');
import { BaseCluster, BaseClusterProps } from '../base/base-cluster';
import { cloudformation } from './ecs.generated';

/**
* Properties to define an ECS cluster
*/
// tslint:disable-next-line:no-empty-interface
export interface Ec2ClusterProps extends BaseClusterProps {
export interface EcsClusterProps {
/**
* A name for the cluster.
*
* @default CloudFormation-generated name
*/
clusterName?: string;

/**
* The VPC where your ECS instances will be running or your ENIs will be deployed
*/
vpc: ec2.VpcNetworkRef;
}

/**
* A container cluster that runs on your EC2 instances
*/
export class Ec2Cluster extends BaseCluster implements IEc2Cluster {
export class EcsCluster extends cdk.Construct implements IEcsCluster {
/**
* Import an existing cluster
*/
public static import(parent: cdk.Construct, name: string, props: ImportedEc2ClusterProps): IEc2Cluster {
return new ImportedEc2Cluster(parent, name, props);
public static import(parent: cdk.Construct, name: string, props: ImportedEcsClusterProps): IEcsCluster {
return new ImportedEcsCluster(parent, name, props);
}

/**
* Connections manager for the EC2 cluster
*/
public readonly connections: ec2.Connections = new ec2.Connections();

constructor(parent: cdk.Construct, name: string, props: Ec2ClusterProps) {
super(parent, name, props);
/**
* The VPC this cluster was created in.
*/
public readonly vpc: ec2.VpcNetworkRef;

/**
* The ARN of this cluster
*/
public readonly clusterArn: string;

/**
* The name of this cluster
*/
public readonly clusterName: string;

constructor(parent: cdk.Construct, name: string, props: EcsClusterProps) {
super(parent, name);

const cluster = new cloudformation.ClusterResource(this, 'Resource', {clusterName: props.clusterName});

this.vpc = props.vpc;
this.clusterArn = cluster.clusterArn;
this.clusterName = cluster.clusterName;
}

/**
Expand Down Expand Up @@ -84,9 +115,9 @@ export class Ec2Cluster extends BaseCluster implements IEc2Cluster {
}

/**
* Export the Ec2Cluster
* Export the EcsCluster
*/
public export(): ImportedEc2ClusterProps {
public export(): ImportedEcsClusterProps {
return {
clusterName: new cdk.Output(this, 'ClusterName', { value: this.clusterName }).makeImportValue().toString(),
vpc: this.vpc.export(),
Expand Down Expand Up @@ -149,7 +180,7 @@ export class EcsOptimizedAmi implements ec2.IMachineImageSource {
/**
* An ECS cluster
*/
export interface IEc2Cluster {
export interface IEcsCluster {
/**
* Name of the cluster
*/
Expand All @@ -169,7 +200,7 @@ export interface IEc2Cluster {
/**
* Properties to import an ECS cluster
*/
export interface ImportedEc2ClusterProps {
export interface ImportedEcsClusterProps {
/**
* Name of the cluster
*/
Expand All @@ -187,9 +218,9 @@ export interface ImportedEc2ClusterProps {
}

/**
* An Ec2Cluster that has been imported
* An EcsCluster that has been imported
*/
class ImportedEc2Cluster extends cdk.Construct implements IEc2Cluster {
class ImportedEcsCluster extends cdk.Construct implements IEcsCluster {
/**
* Name of the cluster
*/
Expand All @@ -205,7 +236,7 @@ class ImportedEc2Cluster extends cdk.Construct implements IEc2Cluster {
*/
public readonly connections = new ec2.Connections();

constructor(parent: cdk.Construct, name: string, props: ImportedEc2ClusterProps) {
constructor(parent: cdk.Construct, name: string, props: ImportedEcsClusterProps) {
super(parent, name);
this.clusterName = props.clusterName;
this.vpc = ec2.VpcNetworkRef.import(this, "vpc", props.vpc);
Expand Down
87 changes: 0 additions & 87 deletions packages/@aws-cdk/aws-ecs/lib/fargate/fargate-cluster.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/fargate/fargate-service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import { BaseService, BaseServiceProps } from '../base/base-service';
import { IFargateCluster } from './fargate-cluster';
import { IEcsCluster } from '../ecs-cluster';
import { FargateTaskDefinition } from './fargate-task-definition';

/**
Expand All @@ -11,7 +11,7 @@ export interface FargateServiceProps extends BaseServiceProps {
/**
* Cluster where service will be deployed
*/
cluster: IFargateCluster; // should be required? do we assume 'default' exists?
cluster: IEcsCluster; // should be required? do we assume 'default' exists?

/**
* Task Definition used for running tasks in the service
Expand Down
3 changes: 1 addition & 2 deletions packages/@aws-cdk/aws-ecs/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ export * from './base/task-definition';

export * from './container-definition';
export * from './container-image';
export * from './ecs-cluster';

export * from './ec2/ec2-cluster';
export * from './ec2/ec2-service';
export * from './ec2/ec2-task-definition';

export * from './fargate/fargate-cluster';
export * from './fargate/fargate-service';
export * from './fargate/fargate-task-definition';

Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/load-balanced-ecs-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import cdk = require('@aws-cdk/cdk');
import { IContainerImage } from './container-image';
import { IEc2Cluster } from './ec2/ec2-cluster';
import { Ec2Service } from './ec2/ec2-service';
import { Ec2TaskDefinition } from './ec2/ec2-task-definition';
import { IEcsCluster } from './ecs-cluster';

/**
* Properties for a LoadBalancedEc2Service
Expand All @@ -12,7 +12,7 @@ export interface LoadBalancedEc2ServiceProps {
/**
* The cluster where your Fargate service will be deployed
*/
cluster: IEc2Cluster;
cluster: IEcsCluster;

/**
* The image to start.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ec2 = require('@aws-cdk/aws-ec2');
import cdk = require('@aws-cdk/cdk');
import { DockerHub } from './container-image';
import { FargateCluster } from './fargate/fargate-cluster';
import { EcsCluster } from './ecs-cluster';
import { LoadBalancedFargateService } from './load-balanced-fargate-service';

/**
Expand Down Expand Up @@ -80,7 +80,7 @@ export class LoadBalancedFargateServiceApplet extends cdk.Stack {
super(parent, id, props);

const vpc = new ec2.VpcNetwork(this, 'MyVpc', { maxAZs: 2 });
const cluster = new FargateCluster(this, 'Cluster', { vpc });
const cluster = new EcsCluster(this, 'Cluster', { vpc });

// Instantiate Fargate Service with just cluster and image
new LoadBalancedFargateService(this, "FargateService", {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import elbv2 = require('@aws-cdk/aws-elasticloadbalancingv2');
import cdk = require('@aws-cdk/cdk');
import { IContainerImage } from './container-image';
import { IFargateCluster } from './fargate/fargate-cluster';
import { IEcsCluster } from './ecs-cluster';
import { FargateService } from './fargate/fargate-service';
import { FargateTaskDefinition } from './fargate/fargate-task-definition';

Expand All @@ -12,7 +12,7 @@ export interface LoadBalancedFargateServiceProps {
/**
* The cluster where your Fargate service will be deployed
*/
cluster: IFargateCluster;
cluster: IEcsCluster;

/**
* The image to start
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-awsvpc-nw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const stack = new cdk.Stack(app, 'aws-ecs-integ');

const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 2 });

const cluster = new ecs.Ec2Cluster(stack, 'Ec2Cluster', { vpc });
const cluster = new ecs.EcsCluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
instanceType: new ec2.InstanceType('t2.micro')
});
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const stack = new cdk.Stack(app, 'aws-ecs-integ-ecs');

const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 2 });

const cluster = new ecs.Ec2Cluster(stack, 'Ec2Cluster', { vpc });
const cluster = new ecs.EcsCluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
instanceType: new ec2.InstanceType('t2.micro')
});
Expand Down
Loading

0 comments on commit 33a35b9

Please sign in to comment.