From cb647d9f950bfb3cb6daa7f4765991bcc85dc225 Mon Sep 17 00:00:00 2001 From: Daniel Van Der Ploeg Date: Thu, 16 Nov 2023 11:26:30 +1030 Subject: [PATCH 1/2] feat: add custom cpu scaling --- packages/graphql-mesh-server/lib/fargate.ts | 26 ++++++++++++++----- .../lib/graphql-mesh-server.ts | 11 ++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/graphql-mesh-server/lib/fargate.ts b/packages/graphql-mesh-server/lib/fargate.ts index 0fef4854..66bb1635 100644 --- a/packages/graphql-mesh-server/lib/fargate.ts +++ b/packages/graphql-mesh-server/lib/fargate.ts @@ -7,7 +7,6 @@ import * as ecr from "aws-cdk-lib/aws-ecr"; import * as ecsPatterns from "aws-cdk-lib/aws-ecs-patterns"; import * as iam from "aws-cdk-lib/aws-iam"; import * as ssm from "aws-cdk-lib/aws-ssm"; -import * as auto_scaling from "aws-cdk-lib/aws-autoscaling"; import { Port, SecurityGroup, IVpc, Vpc } from "aws-cdk-lib/aws-ec2"; import { RedisService } from "./redis-construct"; import { @@ -15,6 +14,7 @@ import { Scope, WebApplicationFirewall, } from "./web-application-firewall"; +import { ScalingInterval, AdjustmentType } from "aws-cdk-lib/aws-autoscaling"; export interface MeshServiceProps { /** @@ -56,6 +56,16 @@ export interface MeshServiceProps { * SSM values to pass through to the container as secrets */ secrets?: { [key: string]: ssm.IStringParameter | ssm.IStringListParameter }; + /** + * Pass custom cpu scaling steps + * Default value: + * [ + * { upper: 30, change: -1 }, + * { lower: 50, change: +1 }, + * { lower: 85, change: +3 }, + * ] + */ + cpuScalingSteps: ScalingInterval[] } export class MeshService extends Construct { @@ -211,15 +221,17 @@ export class MeshService extends Construct { maxCapacity: props.maxCapacity || 5, }); - const cpuUtilization = fargateService.service.metricCpuUtilization(); - scaling.scaleOnMetric("auto-scale-cpu", { - metric: cpuUtilization, - scalingSteps: [ + const cpuScalingSteps = props.cpuScalingSteps || [ { upper: 30, change: -1 }, { lower: 50, change: +1 }, { lower: 85, change: +3 }, - ], - adjustmentType: auto_scaling.AdjustmentType.CHANGE_IN_CAPACITY, + ] + + const cpuUtilization = fargateService.service.metricCpuUtilization(); + scaling.scaleOnMetric("auto-scale-cpu", { + metric: cpuUtilization, + scalingSteps: cpuScalingSteps, + adjustmentType: AdjustmentType.CHANGE_IN_CAPACITY, }); } } diff --git a/packages/graphql-mesh-server/lib/graphql-mesh-server.ts b/packages/graphql-mesh-server/lib/graphql-mesh-server.ts index 2d95a8ed..3a764e61 100644 --- a/packages/graphql-mesh-server/lib/graphql-mesh-server.ts +++ b/packages/graphql-mesh-server/lib/graphql-mesh-server.ts @@ -7,6 +7,7 @@ import { Repository } from "aws-cdk-lib/aws-ecr"; import { FargateService } from "aws-cdk-lib/aws-ecs"; import { CfnCacheCluster } from "aws-cdk-lib/aws-elasticache"; import * as ssm from "aws-cdk-lib/aws-ssm"; +import { ScalingInterval } from "aws-cdk-lib/aws-autoscaling"; export type MeshHostingProps = { /** @@ -56,6 +57,16 @@ export type MeshHostingProps = { * SSM values to pass through to the container as secrets */ secrets?: { [key: string]: ssm.IStringParameter | ssm.IStringListParameter }; + /** + * Pass custom cpu scaling steps + * Default value: + * [ + * { upper: 30, change: -1 }, + * { lower: 50, change: +1 }, + * { lower: 85, change: +3 }, + * ] + */ + cpuScalingSteps: ScalingInterval[] /** * ARN of the SNS Topic to send deployment notifications to */ From 7810dd648c1703c1e5523d58744394bf49d54cca Mon Sep 17 00:00:00 2001 From: Daniel Van Der Ploeg Date: Thu, 16 Nov 2023 11:54:27 +1030 Subject: [PATCH 2/2] chore: run prettier --- packages/graphql-mesh-server/lib/fargate.ts | 10 +++++----- .../graphql-mesh-server/lib/graphql-mesh-server.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/graphql-mesh-server/lib/fargate.ts b/packages/graphql-mesh-server/lib/fargate.ts index 66bb1635..e787db12 100644 --- a/packages/graphql-mesh-server/lib/fargate.ts +++ b/packages/graphql-mesh-server/lib/fargate.ts @@ -65,7 +65,7 @@ export interface MeshServiceProps { * { lower: 85, change: +3 }, * ] */ - cpuScalingSteps: ScalingInterval[] + cpuScalingSteps: ScalingInterval[]; } export class MeshService extends Construct { @@ -222,10 +222,10 @@ export class MeshService extends Construct { }); const cpuScalingSteps = props.cpuScalingSteps || [ - { upper: 30, change: -1 }, - { lower: 50, change: +1 }, - { lower: 85, change: +3 }, - ] + { upper: 30, change: -1 }, + { lower: 50, change: +1 }, + { lower: 85, change: +3 }, + ]; const cpuUtilization = fargateService.service.metricCpuUtilization(); scaling.scaleOnMetric("auto-scale-cpu", { diff --git a/packages/graphql-mesh-server/lib/graphql-mesh-server.ts b/packages/graphql-mesh-server/lib/graphql-mesh-server.ts index 3a764e61..5e01f196 100644 --- a/packages/graphql-mesh-server/lib/graphql-mesh-server.ts +++ b/packages/graphql-mesh-server/lib/graphql-mesh-server.ts @@ -66,7 +66,7 @@ export type MeshHostingProps = { * { lower: 85, change: +3 }, * ] */ - cpuScalingSteps: ScalingInterval[] + cpuScalingSteps: ScalingInterval[]; /** * ARN of the SNS Topic to send deployment notifications to */