studentofcs
studentofcs

Reputation: 65

How do I set min and max tasks for auto scaling policy for ecs services using the SDK?

I want to utilize the AWS SDK to set and define min/max tasks for my auto scaling policy for my ECS service.

So I'm able to successfully modify my auto scaling group policy for my ECS containers instances using code.

UpdateAutoScalingGroupRequest request = new UpdateAutoScalingGroupRequest().withAutoScalingGroupName("helloWorld-ASG").withMinSize(1);

UpdateAutoScalingGroupResult response = client.updateAutoScalingGroup(request);

UpdateScalingPlanResult scalingResponse = scalingClient.updateScalingPlan(scalingRequest);

but how do I do this for the auto scaling policy for my ECS service? What classes do I need to do this? Is it possible?

Upvotes: 1

Views: 932

Answers (3)

nigel.swinson
nigel.swinson

Reputation: 1

Took me a while to find this too...

You want ApplicationAutoScaling:DescribeScalableTargets()/RegisterScalableTarget(). Ref: https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-application-autoscaling-2016-02-06.html#describescalabletargets and https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-application-autoscaling-2016-02-06.html#registerscalabletarget. This has a MinCapacity/MaxCapacity. Call with ServiceNamespace:ecs.

Code below (in PHP syntax) shows you the current values. Change with RegisterScalableTarget.

$aasClient = new \Aws\ApplicationAutoScaling\ApplicationAutoScalingClient([
        'profile' => 'default',
        'region' => 'eu-west-2',
        'version' => '2016-02-06'
]);
$aResult = $aasClient->DescribeScalingPolicies(array(
        'ServiceNamespace' => 'ecs'
));

The DescribeScalingPolicies() will then show you policies which interact with your scalable target. And this is all done in the context of a capacity provider, which also has min/max, but this is min/max instances in your cluster.

This returns MinCapacity/MaxCapacity. There's a corresponding RegisterScalableTarget for registering or updating a "scalable target".

Upvotes: 0

atom88
atom88

Reputation: 1554

This answer above was for version 1.x of the SDK for version 2.x of the SDK you would need something like this one:

https://sdk.amazonaws.com/java/api/latest/index.html?software/amazon/awssdk/services/applicationautoscaling/ApplicationAutoScalingClient.html

Upvotes: 0

Patrick
Patrick

Reputation: 773

For ECS service auto scaling look at the AWSApplicationAutoScalingClient, PutScalingPolicyRequest and PutScalingPolicyResult classes, then depending on your preferred scaling policy you will need either the StepScalingPolicyConfiguration or TargetTrackingScalingPolicyConfiguration class.

See the following example taken from the AWS Java SDK docs:

AWSApplicationAutoScaling client = AWSApplicationAutoScalingClientBuilder.standard().build();
PutScalingPolicyRequest request = new PutScalingPolicyRequest()
        .withPolicyName("web-app-cpu-gt-75")
        .withServiceNamespace("ecs")
        .withResourceId("service/default/web-app")
        .withScalableDimension("ecs:service:DesiredCount")
        .withPolicyType("StepScaling")
        .withStepScalingPolicyConfiguration(
                new StepScalingPolicyConfiguration().withAdjustmentType("PercentChangeInCapacity")
                        .withStepAdjustments(new StepAdjustment().withMetricIntervalLowerBound(0d).withScalingAdjustment(200)).withCooldown(60));
PutScalingPolicyResult response = client.putScalingPolicy(request);AWSApplicationAutoScaling client = AWSApplicationAutoScalingClientBuilder.standard().build();

Upvotes: 1

Related Questions