@Override public void execute(Context context) throws Exception { String asGroupName = resource.asGroup.remoteASGroup.getAutoScalingGroupName(); String policyARN = AWS.as.createPolicy(new PutScalingPolicyRequest() .withPolicyName(resource.id) .withAutoScalingGroupName(asGroupName) .withScalingAdjustment(resource.adjustmentPercentage) .withAdjustmentType("PercentChangeInCapacity") .withMinAdjustmentStep(1) .withCooldown(300)); AWS.cloudWatch.createAlarm(new PutMetricAlarmRequest() .withAlarmName(context.env.name + ":" + resource.id + "-alarm") .withMetricName("CPUUtilization") .withComparisonOperator(resource.comparisonOperator) .withThreshold(resource.cpuUtilizationPercentage) .withPeriod(60) .withEvaluationPeriods(resource.lastMinutes) .withStatistic(Statistic.Average) .withNamespace("AWS/EC2") .withDimensions(new Dimension().withName("AutoScalingGroupName").withValue(asGroupName)) .withAlarmActions(policyARN)); }
public void configureClusterInfrastructure() { LOG.info("Configuration of cluster infrastructure started"); // update AutoScalingGroup with min and max node number autoScaling.updateAutoScalingGroup(new UpdateAutoScalingGroupRequest() .withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) .withMaxSize(configurationMediator.getMaxNodeNumberInCluster()) .withMinSize(configurationMediator.getMinNodeNumberInCluster()) .withDesiredCapacity(configurationMediator.getMinNodeNumberInCluster())); LOG.info("AutoScalingGroup {} updated: {}", autoScalingGroup.getAutoScalingGroupName(), autoScalingGroup.toString()); // we create this infrustructure from JAVA since currently we can not get arn when we create policy from CFT //create AutoScaling Policies String scaleUpPolicyARN = autoScaling.putScalingPolicy(new PutScalingPolicyRequest().withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) .withPolicyName(SCALE_UP_POLICY) .withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) //Increase or decrease the current capacity of the group by the specified number of instances. .withAdjustmentType("ChangeInCapacity") .withPolicyType("SimpleScaling") .withScalingAdjustment(1)).getPolicyARN(); LOG.info("Scale up policy created: {}", SCALE_UP_POLICY); String scaleDownPolicyARN = autoScaling.putScalingPolicy(new PutScalingPolicyRequest().withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) .withPolicyName(SCALE_DOWN_POLICY) .withAutoScalingGroupName(getAutoScalingGroup().getAutoScalingGroupName()) //Increase or decrease the current capacity of the group by the specified number of instances. .withAdjustmentType("ChangeInCapacity") .withPolicyType("SimpleScaling") .withScalingAdjustment(-1)).getPolicyARN(); LOG.info("Scale down policy created: {}", SCALE_DOWN_POLICY); // create custom metric MetricDatum metricDatum = new MetricDatum(); metricDatum.setValue(0.0); metricDatum.setUnit(StandardUnit.Count); metricDatum.setTimestamp(new Date()); metricDatum.setMetricName(METRIC_DATA_NAME); cloudWatch.putMetricData(new PutMetricDataRequest() .withNamespace("ESS/Tasks").withMetricData(metricDatum)); LOG.info("Custom metric added: {}", metricDatum.toString()); // create custom alarm cloudWatch.putMetricAlarm(new PutMetricAlarmRequest() .withAlarmName(ESS_OVERLOAD_ALARM) .withMetricName(METRIC_DATA_NAME) .withComparisonOperator(ComparisonOperator.GreaterThanOrEqualToThreshold) .withThreshold(80.0) .withPeriod(300) .withEvaluationPeriods(2) .withStatistic(Statistic.Average) .withNamespace("ESS/Tasks") .withAlarmActions(scaleUpPolicyARN)); LOG.info("Load alarm added: ", cloudWatch.describeAlarms().getMetricAlarms() .stream().filter(alarm -> alarm.getAlarmName().equals(ESS_OVERLOAD_ALARM)).findFirst().get().toString()); // create custom alarm cloudWatch.putMetricAlarm(new PutMetricAlarmRequest() .withAlarmName(ESS_IDLE_ALARM) .withMetricName(METRIC_DATA_NAME) .withComparisonOperator(ComparisonOperator.LessThanThreshold) .withThreshold(40.0) .withPeriod(300) .withEvaluationPeriods(2) .withStatistic(Statistic.Average) .withNamespace("ESS/Tasks") .withAlarmActions(scaleDownPolicyARN)); LOG.info("Alarm for idle resources added: ", cloudWatch.describeAlarms().getMetricAlarms() .stream().filter(alarm -> alarm.getAlarmName().equals(ESS_IDLE_ALARM)).findFirst().get().toString()); // subscribe to topic amazonSQS.createQueue(new CreateQueueRequest().withQueueName(ESS_QUEUE_NAME)); Topics.subscribeQueue(amazonSNS, amazonSQS, getEssTopicArn(), amazonSQS.getQueueUrl(ESS_QUEUE_NAME).getQueueUrl()); LOG.info("Cluster infrastructure successfully configured."); }
public String createPolicy(PutScalingPolicyRequest request) { logger.info("create scaling policy, request={}", request); return autoScaling.putScalingPolicy(request).getPolicyARN(); }