'Change the Desired Capacity of a auto scaling group in java
I want to bring up an EC2 instance at a particular time, run a java batch job and shut the instance down once done, using Java. I figured out how to bring up the instance and run my job. Need to know how can i shut it down once the job is done. Found out that it is possible by changing the "setDesiredCapacity" of the auto scaling group value to 0. this method takes the auto scaling group name as input. But since the ASG name is dynamically created, not sure how can i get it to my Java job. Any suggestions?
Solution 1:[1]
It appears that your requirements are:
- On a regular schedule, start an Amazon EC2 instance that will run a batch job
- At the conclusion of the job, terminate the EC2 instance
Instead of using Auto Scaling (which is designed to dynamically scale capacity based upon demand), I would recommend:
- Use a schedule to launch a new EC2 instance. The schedule could be a
cronjob on a machine somewhere (on EC2 or anywhere on the Internet), or you could use Amazon CloudWatch Events to run a Lambda function, which launches the instance. - When the batch job is complete, terminate the instance, which can be done via a couple of methods:
- Send a command to the operating system to shutdown. If the EC2 instance is launched with a shutdown behavior of
terminate, then the instance will automatically be terminated. See: Changing the Instance Initiated Shutdown Behavior - Alternatively, have your application make a
TerminateInstancesAPI call to AWS to directly terminate the instance.
- Send a command to the operating system to shutdown. If the EC2 instance is launched with a shutdown behavior of
Or, you could be nice and modern and not use an Amazon EC2 instance!
Since your batch job is in Java, you could use a Lambda function together with a CloudWatch Events schedule. The schedule would trigger the Lambda function, which could run your Java code. When it is finished, Lambda will automatically terminate. You are only billed per 100ms of usage.
Please note that Lambda functions can execute for a maximum of 5 minutes, so if your operation takes longer than this, Lambda is not a suitable solution.
Solution 2:[2]
You can use the following-
- Cloudwatch Event Rule as Scheduler. Target would be Lambda in point 2.
- Lambda to change the desired capacity of auto scaling group by calling "setDesiredCapacity" of the auto scaling group.
- In order to shut down the instances after the batch job is complete, please use the AWS Java SDK in the EC2 instance to change the "setDesiredCapacity" to zero again.
Points to be noted
- Your minimum capacity should be zero for instances to be terminated.
- Cloudwatch Event Rule Cron Expression should take the time taken by EC2 to spin up into consideration.
- If you don't want your requests to over public internet, use vpc endpoints to configure traffic internally within your VPC.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | John Rotenstein |
| Solution 2 | sam agrawal |
