Ensure only one Laravel instance is running CRON jobs in an EB environment
foxxmd/laravel-elasticbeanstalk-cron is a Laravel package for ensure only one laravel instance is running cron jobs in an eb environment.
It currently has 61 GitHub stars and 319.232 downloads on Packagist (latest version 2.1.0).
Install it with composer require foxxmd/laravel-elasticbeanstalk-cron.
Discover more Laravel packages by foxxmd
or browse all Laravel packages to compare alternatives.
Last updated
Ensure one instance in an Elastic Beanstalk environment is running Laravel's Scheduler
A common problem many people have encountered with Amazon's Elastic Beanstalk is maintaining a single instance in an environment that runs Laravel's Task Scheduler. Difficulties arise because auto-scaling does not guarantee any instance is run indefinitely and there are no "master-slave" relationships within an environment to differentiate one instance from the rest.
Although Amazon has provided a solution it involves setting up a worker tier and then, potentially, creating new routes/methods for implementing the tasks that need to be run. Yuck!
This package provides a simple, zero-setup solution for maintaining one instance within an Elastic Beanstalk environment that runs the Task Scheduler.
Amazon Linux 1 (AL1) is retired and Amazon Linux 2 (AL2) PHP platforms were removed from Elastic Beanstalk on April 16, 2026. You must use Amazon Linux 2023 (AL2023). https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/using-features.migration-al.html https://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platform-history-php.html
This release only supports AL2023. Please use previous releases for AL2.
Glad you asked! The below process is completely automated and only requires that you publish the .platform folder to the root of your application.
EB applications since AL2 can contain platform hooks that provides advanced configuration for an EB environment, called .platform.
This package provides a configuration file that runs two commands on deployment (every instance initialization) that setup the conditions needed to run the Task Scheduler on one instance:
system:start:leaderselectionThis is the first command that is run on deployment. It configures the instance's Cron to run Leader Selection at a configured interval (default = 5 minutes)
aws:configure:leaderThis is the Leader Selection command. It does the following:
EnvironmentName of this Instance. (When running in an EB environment all EC2 instances have the same EnvironmentName)EnvironmentNameIf this instance is the earliest launched then it is deemed the Leader and runs system:start:cron
system:start:cronThis command is run only if the current instance running Leader Selection is the Leader. It inserts another entry in the instance's Cron to run Laravel's Scheduler.
Now only one instance, the earliest launched, will have the scheduler inserted into its Cron. If that instance is terminated by auto-scaling a new Leader will be chosen within 5 minutes (or the configured interval) from the remaining running instances.
Require this package
composer require "foxxmd/laravel-elasticbeanstalk-cron"
Then, publish the .platform folder and configuration file
php artisan vendor:publish --tag=ebcron
Don't forget to add +x permission to the EB Platform Hooks scripts (no longer required for Amazon Linux platform that released on or after April 29, 2022)
find .platform -type f -iname "*.sh" -exec chmod +x {} +
In order for Leader Selection to run a few environmental variables must be present:
These can be included in your .env or, for EB, in the environment's configuration section.
Package uses the EC2 instance profile for credentials. Region is resolved from the AWS_REGION environment variable if set, otherwise it is auto-detected from EC2 instance metadata (IMDS).
The EC2 instance profile must have ec2:DescribeInstances permission for leader selection to work. Add the following inline policy to your EB instance role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ec2:DescribeInstances",
"Resource": "*"
}
]
}
Make a PR for some extra functionality and I will happily accept it :)
This package is licensed under the MIT license.