From 61a7c25c31bd3ad16e37536c853a1baf924866a4 Mon Sep 17 00:00:00 2001 From: Daniel Van Der Ploeg Date: Mon, 30 Sep 2024 13:51:44 +0930 Subject: [PATCH] feat AM-1945: modify mesh scaling rules --- packages/graphql-mesh-server/lib/fargate.ts | 50 ++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/packages/graphql-mesh-server/lib/fargate.ts b/packages/graphql-mesh-server/lib/fargate.ts index 9e0fcb36..1c286a69 100644 --- a/packages/graphql-mesh-server/lib/fargate.ts +++ b/packages/graphql-mesh-server/lib/fargate.ts @@ -1,5 +1,5 @@ import { Construct } from "constructs"; -import { Duration } from "aws-cdk-lib"; +import { Duration, IResolvable } from "aws-cdk-lib"; import { RemovalPolicy } from "aws-cdk-lib"; import * as acm from "aws-cdk-lib/aws-certificatemanager"; import * as ecs from "aws-cdk-lib/aws-ecs"; @@ -17,14 +17,19 @@ import { WebApplicationFirewall, } from "./web-application-firewall"; import { CfnIPSet, CfnWebACL } from "aws-cdk-lib/aws-wafv2"; -import { ScalingInterval, AdjustmentType } from "aws-cdk-lib/aws-autoscaling"; +import { + ScalingInterval, + AdjustmentType, + BasicStepScalingPolicyProps, +} from "aws-cdk-lib/aws-autoscaling"; import { ApplicationLoadBalancer } from "aws-cdk-lib/aws-elasticloadbalancingv2"; import { LogGroup } from "aws-cdk-lib/aws-logs"; import path = require("path"); +import { MetricOptions } from "aws-cdk-lib/aws-cloudwatch"; export interface MeshServiceProps { /** - * VPC to attach Redis instance to + * VPC to attach Fargate instance to */ vpc?: IVpc; /** @@ -131,7 +136,7 @@ export interface MeshServiceProps { allowedIps?: string[]; /** * Pass custom cpu scaling steps - * Default value: + * @default * [ * { upper: 30, change: -1 }, * { lower: 50, change: +1 }, @@ -202,6 +207,31 @@ export interface MeshServiceProps { * Optional manual overrides for nginx sidecar container */ nginxConfigOverride?: Partial; + + /** + * Override cpu scaling options + * + * @default + * { + * period: Duration.minutes(1), + * statistic: "max", + * } + */ + cpuScalingOptions?: Partial; + + /** + * Override cpu step scaling options + * + * @default + * { + * metric: cpuUtilization, // use cpuScalingOptions to modify + * scalingSteps: cpuScalingSteps, // use cpuScalingSteps to modify + * adjustmentType: AdjustmentType.CHANGE_IN_CAPACITY, + * evaluationPeriods: 3, + * datapointsToAlarm: 2, + * } + */ + cpuStepScalingOptions?: Partial; } export class MeshService extends Construct { @@ -613,11 +643,21 @@ export class MeshService extends Construct { { lower: 85, change: +3 }, ]; - const cpuUtilization = this.service.metricCpuUtilization(); + // These default options are based on testing + /// however they can be overwritten if required + const cpuUtilization = this.service.metricCpuUtilization({ + period: Duration.minutes(1), + statistic: "max", + ...props.cpuScalingOptions, + }); + scaling.scaleOnMetric("auto-scale-cpu", { metric: cpuUtilization, scalingSteps: cpuScalingSteps, adjustmentType: AdjustmentType.CHANGE_IN_CAPACITY, + evaluationPeriods: 3, + datapointsToAlarm: 2, + ...props.cpuStepScalingOptions, }); } }