Reputation: 1172
So basically I have five tasks that need to be run once a day and the process is usually quick.
The problem I am having is that I don't want to have an EC2 up all day just to use it for thirty minutes. Is there a way to have the ECS create an instance, run the tasks and finish the instance until the next day?
Upvotes: 0
Views: 286
Reputation: 3622
These are a few options:
1. Scheduled Scaling with an Autoscaling Group
You run your instance in an autoscaling group and use time based scaling to set the desired count to 0 when you don't need it and 1(or any number you want) when you have tasks to run.
https://docs.aws.amazon.com/autoscaling/ec2/userguide/schedule_time.html
2. Use AWS Instance Scheduler
AWS Instance Scheduler allows you to create EC2 start-stop scheduling rules. In this approach, you configure the schedule in a dynamodb table, add an appropriate tag for your instance and then a lambda function will ensure that the schedule is followed.
https://aws.amazon.com/answers/infrastructure-management/instance-scheduler/
3. EC2 Start Stop based on pending task count
Schedule a lambda function to run periodically to detect the pending task count for your cluster. If it is non zero, start the instance. The lambda should also stop the instance when there are no pending or running tasks. You'll need to use DescribeTasks
API to filter tasks with lastStatus=PENDING
Some points:
If your tasks don't take a long time to complete, Fargate(if available) could be a better and cheap solution to run your tasks without worrying about servers.
You could also possibly use Cloudwatch alarm actions or AWS Batch but one of the above options is probably simpler and enough for your use case.
Upvotes: 2