Skip to content

Commit

Permalink
Merge pull request #12 from pixelfusion/feat/scaling
Browse files Browse the repository at this point in the history
feat: add autoscaling to fargate
  • Loading branch information
tractorcow authored May 13, 2024
2 parents ac359f2 + 43262c2 commit 052361d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions dist/fargate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface TaskConfiguration {
healthCheckGracePeriod?: cdk.Duration;
cpu?: number;
desiredCount?: number;
autoScalingCpuTarget?: number;
maxCount?: number;
environment?: Record<string, string>;
secrets?: Record<string, string>;
}
Expand Down
14 changes: 13 additions & 1 deletion dist/fargate.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FargateService extends cdk.NestedStack {
const image = imageVersion === DEFAULT_VERSION
? ecs.ContainerImage.fromRegistry(DEFAULT_IMAGE)
: ecs.ContainerImage.fromEcrRepository(repository, imageVersion);
const desiredCount = taskConfiguration?.desiredCount || 1;
this.service = new ecs_patterns.ApplicationLoadBalancedFargateService(this, stack.getResourceID('AdminService'), {
assignPublicIp: true,
cluster: cluster,
Expand All @@ -69,7 +70,7 @@ class FargateService extends cdk.NestedStack {
memoryLimitMiB: taskConfiguration?.memoryLimitMiB || 512,
healthCheckGracePeriod: taskConfiguration?.healthCheckGracePeriod || cdk.Duration.seconds(60),
cpu: taskConfiguration?.cpu || 256,
desiredCount: taskConfiguration?.desiredCount || 1,
desiredCount,
taskImageOptions: {
image,
environment: taskConfiguration?.environment || {},
Expand All @@ -80,6 +81,17 @@ class FargateService extends cdk.NestedStack {
onePerAz: true,
},
});
// Setup AutoScaling policy
if (taskConfiguration.autoScalingCpuTarget) {
// Default max capacity to double desired unless specified
const maxCapacity = taskConfiguration?.maxCount || desiredCount * 2;
const scaling = this.service.service.autoScaleTaskCount({ maxCapacity });
scaling.scaleOnCpuUtilization(stack.getResourceID('CpuScaling'), {
targetUtilizationPercent: taskConfiguration.autoScalingCpuTarget,
scaleInCooldown: cdk.Duration.seconds(60),
scaleOutCooldown: cdk.Duration.seconds(60),
});
}
// Hack to fix subnets issue
// https://github.com/aws/aws-cdk/issues/5892#issuecomment-701993883
const cfnLoadBalancer = this.service.loadBalancer.node
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/fargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface TaskConfiguration {
healthCheckGracePeriod?: cdk.Duration
cpu?: number
desiredCount?: number
autoScalingCpuTarget?: number
maxCount?: number
environment?: Record<string, string>
secrets?: Record<string, string>
}
Expand Down Expand Up @@ -100,6 +102,7 @@ export class FargateService extends cdk.NestedStack {
? ecs.ContainerImage.fromRegistry(DEFAULT_IMAGE)
: ecs.ContainerImage.fromEcrRepository(repository, imageVersion)

const desiredCount = taskConfiguration?.desiredCount || 1
this.service = new ecs_patterns.ApplicationLoadBalancedFargateService(
this,
stack.getResourceID('AdminService'),
Expand All @@ -112,7 +115,7 @@ export class FargateService extends cdk.NestedStack {
healthCheckGracePeriod:
taskConfiguration?.healthCheckGracePeriod || cdk.Duration.seconds(60),
cpu: taskConfiguration?.cpu || 256,
desiredCount: taskConfiguration?.desiredCount || 1,
desiredCount,
taskImageOptions: {
image,
environment: taskConfiguration?.environment || {},
Expand All @@ -125,6 +128,18 @@ export class FargateService extends cdk.NestedStack {
},
)

// Setup AutoScaling policy
if (taskConfiguration.autoScalingCpuTarget) {
// Default max capacity to double desired unless specified
const maxCapacity = taskConfiguration?.maxCount || desiredCount * 2
const scaling = this.service.service.autoScaleTaskCount({ maxCapacity })
scaling.scaleOnCpuUtilization(stack.getResourceID('CpuScaling'), {
targetUtilizationPercent: taskConfiguration.autoScalingCpuTarget,
scaleInCooldown: cdk.Duration.seconds(60),
scaleOutCooldown: cdk.Duration.seconds(60),
})
}

// Hack to fix subnets issue
// https://github.com/aws/aws-cdk/issues/5892#issuecomment-701993883
const cfnLoadBalancer = this.service.loadBalancer.node
Expand Down

0 comments on commit 052361d

Please sign in to comment.