
Scaling Efficiency: Managing Multiple Instances of Cron Jobs in Elastic Beanstalk
Cron jobs on multi-instance Elastic Beanstalk can be challenging. Our goal is often to have the jobs handled by NodeJS and also guaranteed to run on only one given instance at a time. Sometimes a separate web worker instance makes sense for this, but a lot of times we don’t want a separate server and codebase.
You can specify a cron job to run on Elastic Beanstalk using .ebextensions and a “leader_only” flag, but this is not reliable in a situation where your original leader instance gets terminated.
There are many task queue alternatives that can manage jobs, but these usually require a central data store like redis.
One simple and self-contained strategy is to have the instance itself check if it is the “leader” and if so to run the NodeJS “cron” code. This can be accomplished by using the aws-sdk and adding some permissions to the role that the elastic beanstalk environment is using.
The steps in the code below are:
- Get the current instance ID
- Get the environment ID
- Get the other instances in the environment
- Determine if the current instance is “master” based on list order.
Node code:
123456789101112131415161718192021222324252627282930313233IAM Policy for the EB role. { "Version": "2012-10-17", "Statement": [{ "Sid": "Stmt1415212833000", "Effect": "Allow", "Action": [ "s3:*", "s3:Get*", "s3:List*", "s3:ListAllMyBuckets", "s3:PutObject", "sts:AssumeRole", "ec2:DescribeTags", "elasticbeanstalk:DescribeEnvironmentResources", "autoscaling:DescribeAutoScalingGroups", "cloudformation:ListStackResources", "codedeploy:*" ], "Resource": [ "*", "arn:aws:s3:::*", "arn:aws:s3:::deploymentbucket/*", "arn:aws:s3:::aws-codedeploy-us-east-1/*" ] }] } // This is just a sample script. Paste your real code (javascript or HTML) here. if ('this_is' == /an_example/) { of_beautifier(); } else { var a = b ? (c % d) : e[f]; }
This code is based on some work found online including https://gist.github.com/ippeiukai/37e812e49f04ea0f26d84d380d050304
Related Articles
With access to device and user-behavior analytics, this organization reduced device loss by 20-30% per year. The IT department was able to make informed decisions around the utilization and distribution needs of in-store devices which helped validate future hardware requests.
Empowering Clients and Partners
Focusing on the Frontline Worker Experience

Validate the Fit
See BlueFletch Solve Your Device Challenges First-Hand, In Your Own Environment






