Amazon EC2 Auto Scaling based on CPUCreditBalance for the t2-instances


The fairly new instance-type t2 in Amazon has some really nice features. The instance earn CPU credits while they run below their base performance, i.e. 10% for t2.micro, 20% for t2.small and 40% for t2.medium. With that said. If you have instances that runs rather heavy day time, and has almost nothing to do night time, then this instance type is for you. Though if your instance require heavy CPU usage over a long time, maybe the instance can’t buffer up on CPU credits during the night, well then you could just scale up another instance when your old one get’s too low on credits.
So, create an alarm in CloudWatch. Attach it to your Auto Scaling Group, and watch it fire up another instance when the current instances are low.

Here is a small snippet of a CloudFormation template that does just that.

"YourAutoScalingGroup": {
  "Type": "AWS::AutoScaling::AutoScalingGroup",
  "Properties": {
	"AvailabilityZones": { "Fn::GetAZs": "" },
	"LaunchConfigurationName": "YourLaunchConfigurationName,
	"MinSize": "1",
	"MaxSize": "3",
	"LoadBalancerNames": [ "YourLoadBalancerName" ],
	"TerminationPolicies" : [ "OldestInstance", "OldestLaunchConfiguration" ],
	"Tags": [ {
		"PropagateAtLaunch": "true",
		"Value": { "Ref": "AWS::StackName" },
		"Key": "Name"
	} ]
  }
},
"YourAsgScaleUpPolicy" : {
  "Type" : "AWS::AutoScaling::ScalingPolicy",
  "Properties" : {
	"AdjustmentType" : "ChangeInCapacity",
	"AutoScalingGroupName" : { "Ref" : "YourAutoScalingGroup" },
	"Cooldown" : "60",
	"ScalingAdjustment" : "1"
  }
},
"ScaleUpAlarmCPUCreditBalance": {
  "Type": "AWS::CloudWatch::Alarm",
  "Properties": {
	"AlarmDescription": "Scale-up if CPUCreditBalance goes below 10 for 5 minutes",
	"MetricName": "CPUCreditBalance",
	"Namespace": "AWS/EC2",
	"Statistic": "Average",
	"Period": "300",
	"EvaluationPeriods": "1",
	"Threshold": "10",
	"AlarmActions": [ { "Ref": "YourAsgScaleUpPolicy" } ],
	"Dimensions": [ { "Name": "AutoScalingGroup", "Value": "YourAutoScalingGroup" } ],
	"ComparisonOperator": "LessThanThreshold"
  }

Comments

comments