Skip to content

Commit

Permalink
Merge pull request #1173 from aligent/feature/DO-1560_prerender_farga…
Browse files Browse the repository at this point in the history
…te_scaling_options

 Make the scaling options configurable
  • Loading branch information
krishanthisera authored Nov 1, 2023
2 parents f83c983 + 5d4d24c commit f70088f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 6 deletions.
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";

/**
* Options for configuring the Prerender Fargate construct.
Expand Down Expand Up @@ -35,6 +36,10 @@ export interface PrerenderFargateOptions {
* 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 @@ export interface PrerenderFargateOptions {
* ```
*/
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

0 comments on commit f70088f

Please sign in to comment.