PHPackages                             avram/aws-cron-job - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. avram/aws-cron-job

AbandonedArchivedLibrary[CLI &amp; Console](/categories/cli)

avram/aws-cron-job
==================

Laravel command which makes only single EC2 instance run schedule:run for AWS cloud hosting

v0.4.2(2y ago)18114.5k↑85.7%3MITPHP

Since Oct 18Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/avramovic/aws-cron-job)[ Packagist](https://packagist.org/packages/avram/aws-cron-job)[ Fund](https://paypal.me/avramator)[ RSS](/packages/avram-aws-cron-job/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (2)Versions (19)Used By (0)

AWeSome Cron Job
================

[](#awesome-cron-job)

> ⚠️ This package is not maintained anymore. Newer versions of Laravel have [this functionality built-in](https://laravel.com/docs/10.x/scheduling#running-tasks-on-one-server). ⚠️

This is a Laravel 5/6/7/8/9/10 package which enables running cron jobs on only one EC2 instance in your ElasticBeanstalk load-balanced AWS setup.

For different Laravel version please use corresponding package version:

- Laravel 5: `^0.1`
- Laravel 6: `^0.2`
- Laravel 7: `^0.3`
- Laravel 8, 9 &amp; 10: `^0.4`

Problem
-------

[](#problem)

In a typical AWS load-balanced environment, all EC2 instances are exactly the same, and all of them will run any configured cron jobs (if configured through `.ebextensions` folder). This can be fine in some cases, but can result in cron jobs being run more than once, which can be a problem, for example your system can send same email notifications more than once.

### AWS solutions

[](#aws-solutions)

AWS recommends using `leader_only` flag for cron jobs in your `.ebextensions` folder, which will set up cron jobs only on a firstly deployed instance. That is fine until the load-balancer kicks in, creates a new non-leader instance and later decides it does not need additional instances and kills one of your instances, probably the longest-running one, which may just be your leader instance; and since `leader_only` is applied during deployment only, you're left without your leader instance and no cron jobs will be run.

There are more solutions to this problem, but most of them require additional setup on the AWS side. Some of them even suggest you to set up another EC2 instance only for cron jobs. This also can be fine in some cases, but I just didn't like it.

### AWS Cron Job

[](#aws-cron-job)

This package provides an easy way to fire cron jobs from a single EC2 instance without changing your AWS setup. It will simply get all currently running instances for a single AWS EB environment from your AWS account, sort them alphabetically and then check if current instance ID is the same as the first one in the list. When instances are changed, the list changes dynamically so you don't have to worry about that!

To boost performance, this package will cache list of your instances for 5 minutes by default, but you can change that. Also, since instance ID is never changed (instances can be changed but their IDs can't, at least not automatically), current instance ID will be cached forever. Everything is cached locally on the same instance (`file` cache driver).

Install
-------

[](#install)

Simply require it via composer:

```
composer require avram/aws-cron-job

```

Laravel 5.5 (and newer) should automatically discover and enable the service provider when you install this package with composer. For older versions you must add service provider to you `config/app.php` manually:

```
'providers' => [

	// ...

	Avram\AwsCronJob\Providers\AwsCronJobServiceProvider::class,

],

```

Setup
-----

[](#setup)

### AWS credentials

[](#aws-credentials)

This package relies on the AWS PHP SDK, which will automatically pick up your API key and secret key from `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables, so it is the best to simply define these in your environment. You can, however, hard-code these values (see below).

### Configuration

[](#configuration)

This package provides a config file which you **MUST** publish with:

```
php artisan vendor:publish --provider="Avram\AwsCronJob\Providers\AwsCronJobServiceProvider"

```

After that you can setup the package in `config/awscronjob.php`:

```
