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(autoscaling): health check configuration #3390

Merged
merged 12 commits into from
Jul 25, 2019
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 - HealthCheckConfiguration with defaults.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If healthCheck is not specified, then there are no health checks, right? Fix the default description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the health check is mandatory. The CloudFormation documentation says EC2 is the default, and the API reference says the value is required.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, so we should say that EC2 with no grace period is the default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*/
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,
};

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 -
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am assuming no grace is the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, like I said in the original PR, the CloudFormation doc isn't explicit, but the API reference says the duration is 0 seconds.
I updated the JSDoc accordingly

*/
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 && options.grace.toSeconds());
}

/**
* 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.toSeconds());
}

private constructor(public readonly type: string, public readonly gracePeriod?: number) { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a Duration actually and converted at the usage site

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

export enum HealthCheckType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to export this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

EC2 = 'EC2',
ELB = 'ELB',
}

/**
* Render the rolling update configuration into the appropriate object
*/
Expand Down
22 changes: 22 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,28 @@ export = {
test.done();
},

'can configure health check'(test: Test) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test for ec2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// 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