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(templates): enable autoscaling for simple target track policies #1355

Merged
merged 2 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions internal/pkg/template/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
"addons",
"sidecars",
"logconfig",
"autoscaling",
}
)

Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/template/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestTemplate_ParseSvc(t *testing.T) {
mockBox.AddString("workloads/common/cf/addons.yml", "addons")
mockBox.AddString("workloads/common/cf/sidecars.yml", "sidecars")
mockBox.AddString("workloads/common/cf/logconfig.yml", "logconfig")
mockBox.AddString("workloads/common/cf/autoscaling.yml", "autoscaling")

t.box = mockBox
},
Expand All @@ -51,6 +52,7 @@ func TestTemplate_ParseSvc(t *testing.T) {
addons
sidecars
logconfig
autoscaling
`,
},
}
Expand Down
58 changes: 58 additions & 0 deletions templates/workloads/common/cf/autoscaling.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{- if .Autoscaling}}
AutoScalingRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Join ['', [!Ref WorkloadName, AutoScalingRole]]
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service: ecs-tasks.amazonaws.com
Action: 'sts:AssumeRole'
ManagedPolicyArns:
- 'arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceAutoscaleRole'

AutoScalingTarget:
Type: AWS::ApplicationAutoScaling::ScalableTarget
Properties:
MinCapacity: {{.Autoscaling.MinCapacity}}
MaxCapacity: {{.Autoscaling.MaxCapacity}}
ResourceId:
Fn::Join:
- '/'
- - 'service'
- Fn::ImportValue:
!Sub '${AppName}-${EnvName}-ClusterId'
- !GetAtt Service.Name
ScalableDimension: ecs:service:DesiredCount
ServiceNamespace: ecs
RoleARN: !GetAtt AutoScalingRole.Arn
{{if .Autoscaling.CPU}}
AutoScalingPolicyECSServiceAverageCPUUtilization:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: !Join ['-', [!Ref WorkloadName, ECSServiceAverageCPUUtilization]]
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref AutoScalingTarget
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageCPUUtilization
ScaleInCooldown: 10
ScaleOutCooldown: 10
TargetValue: {{.Autoscaling.CPU}}
{{- end}}
{{if .Autoscaling.Memory}}
AutoScalingPolicyECSServiceAverageMemoryUtilization:
Type: AWS::ApplicationAutoScaling::ScalingPolicy
Properties:
PolicyName: !Join ['-', [!Ref WorkloadName, ECSServiceAverageMemoryUtilization]]
PolicyType: TargetTrackingScaling
ScalingTargetId: !Ref AutoScalingTarget
TargetTrackingScalingPolicyConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ECSServiceAverageMemoryUtilization
ScaleInCooldown: 10
ScaleOutCooldown: 10
TargetValue: {{.Autoscaling.Memory}}
{{- end}}
{{- end}}
3 changes: 1 addition & 2 deletions templates/workloads/services/backend/cf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ Resources:
{{- end}}
{{include "sidecars" . | indent 8}}
{{include "executionrole" . | indent 2}}

{{include "taskrole" . | indent 2}}

{{include "servicediscovery" . | indent 2}}
{{include "autoscaling" . | indent 2}}

Service:
Type: AWS::ECS::Service
Expand Down
3 changes: 1 addition & 2 deletions templates/workloads/services/lb-web/cf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ Resources:
{{include "logconfig" . | indent 10}}
{{include "sidecars" . | indent 8}}
{{include "executionrole" . | indent 2}}

{{include "taskrole" . | indent 2}}

{{include "servicediscovery" . | indent 2}}
{{include "autoscaling" . | indent 2}}

Service:
Type: AWS::ECS::Service
Expand Down