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

Make the scaling options configurable #1173

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/prerender-fargate/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { PrerenderFargate } from "./lib/prerender-fargate";
import { PrerenderFargateOptions } from "./lib/prerender-fargate-options";
import {
PrerenderFargateOptions,
PrerenderFargateScalingOptions,
} from "./lib/prerender-fargate-options";
import { PrerenderTokenUrlAssociationOptions } from "./lib/recaching/prerender-tokens";

export {
PrerenderFargate,
PrerenderFargateOptions,
PrerenderFargateScalingOptions,
PrerenderTokenUrlAssociationOptions,
};
58 changes: 58 additions & 0 deletions packages/prerender-fargate/lib/prerender-fargate-options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PrerenderTokenUrlAssociationOptions } from "./recaching/prerender-tokens";
import * as ec2 from "aws-cdk-lib/aws-ec2";

Check warning on line 2 in packages/prerender-fargate/lib/prerender-fargate-options.ts

View workflow job for this annotation

GitHub Actions / build

'ec2' is defined but never used. Allowed unused vars must match /^_/u

/**
* Options for configuring the Prerender Fargate construct.
Expand Down Expand Up @@ -35,6 +36,10 @@
* The ARN of the SSL certificate to use for HTTPS connections.
*/
certificateArn: string;
/**
* The minimum number of Fargate instances to run.
*/
minInstanceCount?: number;
/**
* The desired number of Fargate instances to run.
*/
Expand Down Expand Up @@ -78,4 +83,57 @@
* ```
*/
tokenUrlAssociation?: PrerenderTokenUrlAssociationOptions;
/**
* Prerender Fargate Scaling option
* This allows to alter the scaling behavior. The default configuration should be sufficient
* for most of the cases.
*/
prerenderFargateScalingOptions?: PrerenderFargateScalingOptions;
}

/**
* Prerender Fargate Scaling option
*/
export interface PrerenderFargateScalingOptions {
/**
* Fargate service health check grace period.
* The minimum number of tasks, specified as a percentage of
* the Amazon ECS service's DesiredCount value, that must
* continue to run and remain healthy during a deployment.
* @default - 20 seconds
*/
healthCheckGracePeriod?: number;
/**
* Fargate service minimum healthy percent.
* @default - 0
*/
minHealthyPercent?: number;
/**
* Fargate service maximum healthy percent.
* This limits the scheduler from starting a replacement task first,
* the scheduler will stop an unhealthy task one at a time at random to
* free up capacity, and then start a replacement task
* @default - 200
*/
maxHealthyPercent?: number;
/**
* Health check interval in seconds.
* @default - 50
*/
healthCheckInterval?: number;
/**
* Scale in cooldown in seconds.
* @default - 60
*/
scaleInCooldown?: number;
/**
* Scale out cooldown in seconds.
* @default - 60
*/
scaleOutCooldown?: number;
/**
* The number of consecutive health check failures required before considering a task unhealthy.
* @default - 5
*/
unhealthyThresholdCount?: number;
}
27 changes: 22 additions & 5 deletions packages/prerender-fargate/lib/prerender-fargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export class PrerenderFargate extends Construct {
bucketName,
domainName,
prerenderName,
minInstanceCount,
prerenderFargateScalingOptions,
} = props;

// Create bucket for prerender storage
Expand Down Expand Up @@ -160,7 +162,6 @@ export class PrerenderFargate extends Construct {
TOKEN_LIST: tokenList.toString(),
},
},
healthCheckGracePeriod: Duration.seconds(20),
publicLoadBalancer: true,
assignPublicIp: true,
listenerPort: 443,
Expand All @@ -174,6 +175,14 @@ export class PrerenderFargate extends Construct {
"cert",
certificateArn
),
// Scaling configuration
healthCheckGracePeriod: Duration.seconds(
prerenderFargateScalingOptions?.healthCheckGracePeriod || 20
),
minHealthyPercent:
prerenderFargateScalingOptions?.minHealthyPercent || 50,
maxHealthyPercent:
prerenderFargateScalingOptions?.maxHealthyPercent || 200,
}
);

Expand All @@ -184,19 +193,27 @@ export class PrerenderFargate extends Construct {
// It should be considered healthy when receiving a 401 response
fargateService.targetGroup.configureHealthCheck({
path: "/health",
interval: Duration.seconds(120),
unhealthyThresholdCount: 5,
interval: Duration.seconds(
prerenderFargateScalingOptions?.healthCheckInterval || 120
),
unhealthyThresholdCount:
prerenderFargateScalingOptions?.unhealthyThresholdCount || 5,
healthyHttpCodes: "401",
});

// Setup AutoScaling policy
const scaling = fargateService.service.autoScaleTaskCount({
maxCapacity: maxInstanceCount || 2,
minCapacity: minInstanceCount || 1,
});
scaling.scaleOnCpuUtilization(`${prerenderName}-scaling`, {
targetUtilizationPercent: 50,
scaleInCooldown: Duration.seconds(60),
scaleOutCooldown: Duration.seconds(60),
scaleInCooldown: Duration.seconds(
prerenderFargateScalingOptions?.scaleInCooldown || 60
),
scaleOutCooldown: Duration.seconds(
prerenderFargateScalingOptions?.scaleOutCooldown || 60
),
});

/**
Expand Down
Loading