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

feat: add autoscaling to fargate #12

Merged
merged 1 commit into from
May 13, 2024
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
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