Skip to content

Commit

Permalink
feat(autoscaling): health check configuration (#3390)
Browse files Browse the repository at this point in the history
Implements missing Auto Scaling Group properties [HealthCheckType](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-healthchecktype) and [HealthCheckGracePeriod](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-healthcheckgraceperiod)
They are grouped in a `healthCheck` object property.
The constructor checks that the following requirement is fulfilled:

> If you are adding an ELB health check, you must specify this property.


Fixes #3381
  • Loading branch information
nmussy authored and RomainMuller committed Jul 25, 2019
1 parent 6b763b7 commit 9347a71
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
66 changes: 65 additions & 1 deletion packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ export interface CommonAutoScalingGroupProps {
* @default none
*/
readonly spotPrice?: string;

/**
* Configuration for health checks
*
* @default - HealthCheck.ec2 with no grace period
*/
readonly healthCheck?: HealthCheck;
}

/**
Expand Down Expand Up @@ -444,7 +451,9 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
],
}
],
vpcZoneIdentifier: subnetIds
vpcZoneIdentifier: subnetIds,
healthCheckType: props.healthCheck && props.healthCheck.type,
healthCheckGracePeriod: props.healthCheck && props.healthCheck.gracePeriod && props.healthCheck.gracePeriod.toSeconds(),
};

if (!hasPublic && props.associatePublicIpAddress) {
Expand Down Expand Up @@ -673,6 +682,61 @@ export enum ScalingProcess {
ADD_TO_LOAD_BALANCER = 'AddToLoadBalancer'
}

/**
* EC2 Heath check options
*/
export interface Ec2HealthCheckOptions {
/**
* Specified the time Auto Scaling waits before checking the health status of an EC2 instance that has come into service
*
* @default Duration.seconds(0)
*/
readonly grace?: Duration;
}

/**
* ELB Heath check options
*/
export interface ElbHealthCheckOptions {
/**
* Specified the time Auto Scaling waits before checking the health status of an EC2 instance that has come into service
*
* This option is required for ELB health checks.
*/
readonly grace: Duration;
}

/**
* Health check settings
*/
export class HealthCheck {
/**
* Use EC2 for health checks
*
* @param options EC2 health check options
*/
public static ec2(options: Ec2HealthCheckOptions = {}): HealthCheck {
return new HealthCheck(HealthCheckType.EC2, options.grace);
}

/**
* Use ELB for health checks.
* It considers the instance unhealthy if it fails either the EC2 status checks or the load balancer health checks.
*
* @param options ELB health check options
*/
public static elb(options: ElbHealthCheckOptions): HealthCheck {
return new HealthCheck(HealthCheckType.ELB, options.grace);
}

private constructor(public readonly type: string, public readonly gracePeriod?: Duration) { }
}

enum HealthCheckType {
EC2 = 'EC2',
ELB = 'ELB',
}

/**
* Render the rolling update configuration into the appropriate object
*/
Expand Down
43 changes: 43 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,49 @@ export = {
test.done();
},

'can configure EC2 health check'(test: Test) {
// GIVEN
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' } });
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
healthCheck: autoscaling.HealthCheck.ec2()
});

// THEN
expect(stack).to(haveResourceLike("AWS::AutoScaling::AutoScalingGroup", {
HealthCheckType: 'EC2',
}));

test.done();
},

'can configure EBS health check'(test: Test) {
// GIVEN
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' } });
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
machineImage: new ec2.AmazonLinuxImage(),
vpc,
healthCheck: autoscaling.HealthCheck.elb({grace: cdk.Duration.minutes(15)})
});

// THEN
expect(stack).to(haveResourceLike("AWS::AutoScaling::AutoScalingGroup", {
HealthCheckType: 'ELB',
HealthCheckGracePeriod: 900
}));

test.done();
},

'can add Security Group to Fleet'(test: Test) {
// GIVEN
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' } });
Expand Down

0 comments on commit 9347a71

Please sign in to comment.