Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cron job only on single instance in AWS AutoScaling?

I have scheduled 2 cronjobs for my application.

My Application server is in an autoscaling group and I kept a minimum of 2 instances because of High availability. Everything working is fine but cron job is running multiple times because of 2 instances in autoscaling.

I could not limit the instance size to 1 because already my application in the production environment I prefer to have HA.

How should I have to limit execute cron job on a single instance? or should i have to use other services like AWS Lamda or AWS ELasticBeanstalk

like image 611
Rakesh Sivagouni Avatar asked Oct 27 '25 06:10

Rakesh Sivagouni


2 Answers

Firstly you should consider whether running the crons on these instances is suitable. If you're trying to keep this highly available and it is directly interacted via customers what will the impact of the crons performance be?

Perhaps consider using a separate autoscaling group or instance with a total of 1 instances to run these crons? You could launch the instance or update the autoscaling group just before the cron needs to run and then automate the shutdown after it has completed.

Otherwise you would need to consider using a locking mechanism for your script. By using this your script write a lock to confirm that it is in process, at the beginning of the script run it would check whether there was any script lock in progress. To further prevent the chance of a collision between multiple servers consider adding jitter (random seconds of sleep) to the start of your script.

Suitable technologies for writing a lock are below:

  • DynamoDB using strongly consistent reads.
  • EFS for a Linux application, or FSX for a Windows application.
  • S3 using strong consistency.
like image 157
Chris Williams Avatar answered Oct 28 '25 20:10

Chris Williams


Solutions suggested by Chris Williams sound reasonable if using lambda function is not an option.
One way to simulate cron job is by using CloudWatch Events (now known as EventBridge) in conjunction with AWS Lambda.
First you need to write a Lambda function with the code that needs to be executed on a schedule. Lambda supports cron expressions.
You can then use Schedule Expressions with EventBridge/CloudWatch Event in the same way as a cron tab and mention the Lambda function as target.

like image 29
Shazic Avatar answered Oct 28 '25 22:10

Shazic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!