Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Add autoscaling to web service (resolves #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Widjaja authored and Andrew W committed Jan 31, 2018
1 parent 18fd28c commit 3d49f0d
Showing 1 changed file with 115 additions and 3 deletions.
118 changes: 115 additions & 3 deletions services/website-service/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Parameters:
Type: Number
Default: 2

MaxCount:
Description: Maximum number of instances of this task we can run across our cluster
Type: Number
Default: 3

ProductServiceUrl:
Description: The URL of the Product Service (used to fetch product information)
Type: String
Expand Down Expand Up @@ -103,7 +108,7 @@ Resources:
# http://docs.aws.amazon.com/AmazonECS/latest/developerguide/service_IAM_role.html
ServiceRole:
Type: AWS::IAM::Role
Properties:
Properties:
RoleName: !Sub ecs-service-${AWS::StackName}
Path: /
AssumeRolePolicyDocument: |
Expand All @@ -114,9 +119,9 @@ Resources:
"Action": [ "sts:AssumeRole" ]
}]
}
Policies:
Policies:
- PolicyName: !Sub ecs-service-${AWS::StackName}
PolicyDocument:
PolicyDocument:
{
"Version": "2012-10-17",
"Statement": [{
Expand All @@ -136,3 +141,110 @@ Resources:
}]
}

AutoScalingRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
Action:
- 'sts:AssumeRole'
Effect: Allow
Principal:
Service:
- application-autoscaling.amazonaws.com
Path: /
Policies:
- PolicyName: ecs-service-autoscaling
PolicyDocument:
Statement:
Effect: Allow
Action:
- application-autoscaling:*
- cloudwatch:DescribeAlarms
- cloudwatch:PutMetricAlarm
- ecs:DescribeServices
- ecs:UpdateService
Resource: "*"

ServiceScalableTarget:
Type: "AWS::ApplicationAutoScaling::ScalableTarget"
Properties:
MaxCapacity: !Ref MaxCount
MinCapacity: !Ref DesiredCount
ResourceId: !Join
- /
- - service
- !Ref Cluster
- !GetAtt Service.Name
RoleARN: !GetAtt AutoScalingRole.Arn
ScalableDimension: ecs:service:DesiredCount
ServiceNamespace: ecs

ServiceScaleOutPolicy:
Type : "AWS::ApplicationAutoScaling::ScalingPolicy"
Properties:
PolicyName: ServiceScaleOutPolicy
PolicyType: StepScaling
ScalingTargetId: !Ref ServiceScalableTarget
StepScalingPolicyConfiguration:
AdjustmentType: ChangeInCapacity
Cooldown: 1800
MetricAggregationType: Average
StepAdjustments:
- MetricIntervalLowerBound: 0
ScalingAdjustment: 1

ServiceScaleInPolicy:
Type : "AWS::ApplicationAutoScaling::ScalingPolicy"
Properties:
PolicyName: ServiceScaleInPolicy
PolicyType: StepScaling
ScalingTargetId: !Ref ServiceScalableTarget
StepScalingPolicyConfiguration:
AdjustmentType: ChangeInCapacity
Cooldown: 1800
MetricAggregationType: Average
StepAdjustments:
- MetricIntervalUpperBound: 0
ScalingAdjustment: -1

CPUScaleOutAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: CPU utilization greater than 90%
AlarmDescription: Alarm if cpu utilization greater than 90% of reserved cpu
Namespace: AWS/ECS
MetricName: CPUUtilization
Dimensions:
- Name: ClusterName
Value: !Ref Cluster
- Name: ServiceName
Value: !GetAtt Service.Name
Statistic: Maximum
Period: '60'
EvaluationPeriods: '3'
Threshold: '90'
ComparisonOperator: GreaterThanThreshold
AlarmActions:
- !Ref ServiceScaleOutPolicy

CPUScaleInAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmName: CPU utilization less than 70%
AlarmDescription: Alarm if cpu utilization greater than 70% of reserved cpu
Namespace: AWS/ECS
MetricName: CPUUtilization
Dimensions:
- Name: ClusterName
Value: !Ref Cluster
- Name: ServiceName
Value: !GetAtt Service.Name
Statistic: Maximum
Period: '60'
EvaluationPeriods: '10'
Threshold: '70'
ComparisonOperator: LessThanThreshold
AlarmActions:
- !Ref ServiceScaleInPolicy

0 comments on commit 3d49f0d

Please sign in to comment.