private static void setupPolicy( AmazonAutoScalingClient autoScale, AmazonCloudWatchClient cloudWatch) { PutScalingPolicyRequest policyReq = new PutScalingPolicyRequest(); policyReq.setPolicyName("On Demand Scale Up Policy"); policyReq.setAutoScalingGroupName("OnDemand ASGroup"); policyReq.setAdjustmentType("ChangeInCapacity"); policyReq.setCooldown(60); policyReq.setScalingAdjustment(1); PutScalingPolicyResult arn_up = autoScale.putScalingPolicy(policyReq); PutMetricAlarmRequest putMetricAlarmRequest = new PutMetricAlarmRequest(); putMetricAlarmRequest.setMetricName("HighCPUAlarm"); putMetricAlarmRequest.setComparisonOperator("GreaterThanOrEqualToThreshold"); putMetricAlarmRequest.setEvaluationPeriods(1); putMetricAlarmRequest.setMetricName("CPUUtilization"); putMetricAlarmRequest.setNamespace("AWS/EC2"); putMetricAlarmRequest.setPeriod(120); putMetricAlarmRequest.setStatistic("Average"); putMetricAlarmRequest.setThreshold(50.0); List<String> arnList = new ArrayList<String>(); arnList.add(arn_up.getPolicyARN()); putMetricAlarmRequest.setAlarmActions(arnList); putMetricAlarmRequest.setAlarmName("On Demand Alarm Up"); cloudWatch.putMetricAlarm(putMetricAlarmRequest); policyReq = new PutScalingPolicyRequest(); policyReq.setPolicyName("On Demand Scale Down Policy"); policyReq.setAutoScalingGroupName("OnDemand ASGroup"); policyReq.setAdjustmentType("ChangeInCapacity"); policyReq.setCooldown(60); policyReq.setScalingAdjustment(-1); PutScalingPolicyResult arn_down = autoScale.putScalingPolicy(policyReq); putMetricAlarmRequest = new PutMetricAlarmRequest(); putMetricAlarmRequest.setMetricName("LowCPUAlarm"); putMetricAlarmRequest.setComparisonOperator("LessThanOrEqualToThreshold"); putMetricAlarmRequest.setEvaluationPeriods(1); putMetricAlarmRequest.setMetricName("CPUUtilization"); putMetricAlarmRequest.setNamespace("AWS/EC2"); putMetricAlarmRequest.setPeriod(120); putMetricAlarmRequest.setStatistic("Average"); putMetricAlarmRequest.setThreshold(4.0); List<String> arnList2 = new ArrayList<String>(); arnList2.add(arn_down.getPolicyARN()); putMetricAlarmRequest.setAlarmActions(arnList2); putMetricAlarmRequest.setAlarmName("On Demand Alarm Down"); cloudWatch.putMetricAlarm(putMetricAlarmRequest); System.out.println("Setup autoscaler to monitor average CPU Usage"); }
public String putScalingPolicy( String autoScalingGroupName, String policyName, int scalingAdjustment, String adjustmentType, int cooldown) { PutScalingPolicyRequest request = new PutScalingPolicyRequest(); request.setAutoScalingGroupName(autoScalingGroupName); request.setAdjustmentType(adjustmentType); request.setCooldown(cooldown); request.setPolicyName(policyName); request.setScalingAdjustment(scalingAdjustment); PutScalingPolicyResult result = scalingClient.putScalingPolicy(request); return result.getPolicyARN(); }