Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-eks): add construct library for EKS #1655

Merged
merged 12 commits into from
Feb 8, 2019
62 changes: 35 additions & 27 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,25 @@ import { BaseTargetTrackingProps, PredefinedMetric, TargetTrackingScalingPolicy
const NAME_TAG: string = 'Name';

/**
* Properties of a Fleet
* Basic properties of an AutoScalingGroup, except the exact machines to run and where they should run
*
* Constructs that want to create AutoScalingGroups can inherit
* this interface and specialize the essential parts in various ways.
*/
export interface AutoScalingGroupProps {
/**
* Type of instance to launch
*/
instanceType: ec2.InstanceType;

export interface CommonAutoScalingGroupProps {
/**
* Minimum number of instances in the fleet
*
* @default 1
*/
minSize?: number;
minCapacity?: number;

/**
* Maximum number of instances in the fleet
*
* @default desiredCapacity
*/
maxSize?: number;
maxCapacity?: number;

/**
* Initial amount of instances in the fleet
Expand All @@ -52,16 +50,6 @@ export interface AutoScalingGroupProps {
*/
keyName?: string;

/**
* AMI to launch
*/
machineImage: ec2.IMachineImageSource;

/**
* VPC to launch these instances in.
*/
vpc: ec2.IVpcNetwork;

/**
* Where to place instances within the VPC
*/
Expand Down Expand Up @@ -153,6 +141,26 @@ export interface AutoScalingGroupProps {
associatePublicIpAddress?: boolean;
}

/**
* Properties of a Fleet
*/
export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
/**
* VPC to launch these instances in.
*/
vpc: ec2.IVpcNetwork;

/**
* Type of instance to launch
*/
instanceType: ec2.InstanceType;

/**
* AMI to launch
*/
machineImage: ec2.IMachineImageSource;
}

/**
* A Fleet represents a managed set of EC2 instances
*
Expand Down Expand Up @@ -236,19 +244,19 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup

const desiredCapacity =
(props.desiredCapacity !== undefined ? props.desiredCapacity :
(props.minSize !== undefined ? props.minSize :
(props.maxSize !== undefined ? props.maxSize : 1)));
const minSize = props.minSize !== undefined ? props.minSize : 1;
const maxSize = props.maxSize !== undefined ? props.maxSize : desiredCapacity;
(props.minCapacity !== undefined ? props.minCapacity :
(props.maxCapacity !== undefined ? props.maxCapacity : 1)));
const minCapacity = props.minCapacity !== undefined ? props.minCapacity : 1;
const maxCapacity = props.maxCapacity !== undefined ? props.maxCapacity : desiredCapacity;

if (desiredCapacity < minSize || desiredCapacity > maxSize) {
throw new Error(`Should have minSize (${minSize}) <= desiredCapacity (${desiredCapacity}) <= maxSize (${maxSize})`);
if (desiredCapacity < minCapacity || desiredCapacity > maxCapacity) {
throw new Error(`Should have minCapacity (${minCapacity}) <= desiredCapacity (${desiredCapacity}) <= maxCapacity (${maxCapacity})`);
}

const asgProps: CfnAutoScalingGroupProps = {
cooldown: props.cooldownSeconds !== undefined ? `${props.cooldownSeconds}` : undefined,
minSize: minSize.toString(),
maxSize: maxSize.toString(),
minSize: minCapacity.toString(),
maxSize: maxCapacity.toString(),
desiredCapacity: desiredCapacity.toString(),
launchConfigurationName: launchConfig.ref,
loadBalancerNames: new cdk.Token(() => this.loadBalancerNames.length > 0 ? this.loadBalancerNames : undefined),
Expand Down
22 changes: 11 additions & 11 deletions packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ export = {
test.done();
},

'can set minSize, maxSize, desiredCapacity to 0'(test: Test) {
'can set minCapacity, maxCapacity, desiredCapacity to 0'(test: Test) {
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
const vpc = mockVpc(stack);

new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 0,
maxSize: 0,
minCapacity: 0,
maxCapacity: 0,
desiredCapacity: 0
});

Expand All @@ -159,7 +159,7 @@ export = {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 10
minCapacity: 10
});

// THEN
Expand All @@ -183,7 +183,7 @@ export = {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
maxSize: 10
maxCapacity: 10
});

// THEN
Expand Down Expand Up @@ -415,8 +415,8 @@ export = {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 0,
maxSize: 0,
minCapacity: 0,
maxCapacity: 0,
desiredCapacity: 0,
associatePublicIpAddress: true,
});
Expand All @@ -438,8 +438,8 @@ export = {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 0,
maxSize: 0,
minCapacity: 0,
maxCapacity: 0,
desiredCapacity: 0,
associatePublicIpAddress: false,
});
Expand All @@ -461,8 +461,8 @@ export = {
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
minSize: 0,
maxSize: 0,
minCapacity: 0,
maxCapacity: 0,
desiredCapacity: 0,
});

Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2/lib/machine-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class GenericLinuxImage implements IMachineImageSource {
public getImage(scope: Construct): MachineImage {
const stack = Stack.find(scope);
const region = stack.requireRegion('AMI cannot be determined');
const ami = this.amiMap[region];
const ami = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
if (!ami) {
throw new Error(`Unable to find AMI in AMI map: no AMI specified for region '${region}'`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const cluster = new ecs.Cluster(this, 'Cluster', {
});

// Add capacity to it
cluster.addDefaultAutoScalingGroupCapacity({
cluster.addDefaultAutoScalingGroupCapacity('Capacity', {
instanceType: new ec2.InstanceType("t2.xlarge"),
instanceCount: 3,
});
Expand Down
36 changes: 6 additions & 30 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,13 @@ export class Cluster extends cdk.Construct implements ICluster {
*
* Returns the AutoScalingGroup so you can add autoscaling settings to it.
*/
public addDefaultAutoScalingGroupCapacity(options: AddDefaultAutoScalingGroupOptions): autoscaling.AutoScalingGroup {
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'DefaultAutoScalingGroup', {
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
public addDefaultAutoScalingGroupCapacity(id: string, options: AddDefaultAutoScalingGroupOptions): autoscaling.AutoScalingGroup {
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, id, {
...options,
vpc: this.vpc,
instanceType: options.instanceType,
machineImage: new EcsOptimizedAmi(),
updateType: autoscaling.UpdateType.ReplacingUpdate,
minSize: options.minCapacity,
maxSize: options.maxCapacity,
desiredCapacity: options.instanceCount,
updateType: options.updateType || autoscaling.UpdateType.ReplacingUpdate,
rix0rrr marked this conversation as resolved.
Show resolved Hide resolved
instanceType: options.instanceType,
});

this.addAutoScalingGroupCapacity(autoScalingGroup, options);
Expand Down Expand Up @@ -375,31 +373,9 @@ export interface AddAutoScalingGroupCapacityOptions {
/**
* Properties for adding autoScalingGroup
*/
export interface AddDefaultAutoScalingGroupOptions extends AddAutoScalingGroupCapacityOptions {

export interface AddDefaultAutoScalingGroupOptions extends AddAutoScalingGroupCapacityOptions, autoscaling.CommonAutoScalingGroupProps {
/**
* The type of EC2 instance to launch into your Autoscaling Group
*/
instanceType: ec2.InstanceType;

/**
* Number of container instances registered in your ECS Cluster
*
* @default 1
*/
instanceCount?: number;

/**
* Maximum number of instances
*
* @default Same as instanceCount
*/
maxCapacity?: number;

/**
* Minimum number of instances
*
* @default Same as instanceCount
*/
minCapacity?: number;
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ecs/test/ec2/integ.event-task.lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EventStack extends cdk.Stack {
const vpc = new ec2.VpcNetwork(this, 'Vpc', { maxAZs: 1 });

const cluster = new ecs.Cluster(this, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro')
});

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 @@ -10,7 +10,7 @@ const stack = new cdk.Stack(app, 'aws-ecs-integ');
const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 2 });

const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro')
});

Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const stack = new cdk.Stack(app, 'aws-ecs-integ-ecs');
const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 2 });

const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro')
});

Expand Down Expand Up @@ -44,4 +44,4 @@ listener.addTargets('ECS', {

new cdk.Output(stack, 'LoadBalancerDNS', { value: lb.dnsName, });

app.run();
app.run();
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export = {
const stack = new cdk.Stack();
const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 1 });
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
cluster.addDefaultAutoScalingGroupCapacity({
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro')
});

Expand Down Expand Up @@ -58,4 +58,4 @@ export = {

test.done();
}
};
};
Loading