Laravel Command returning different output when executed by Cron on AWS EC2

Viewed 283

I'm having some trouble with the cron jobs in a EC2 instance on a Elastic Beanstalk environment.

When I connect to the EC2 instance and I type php artisan command:backup_logs from the terminal, the job runs smoothly and everything works as it should. While, when the job runs through the cron * * * * * /usr/bin/php /var/app/current/artisan command:backup_logs it returns the following error in the logs:

[2018-09-17 16:14:01] production.ERROR: Missing required client configuration options:

region: (string)

  A "region" configuration value is required for the "s3" service

  (e.g., "us-west-2"). A list of available public regions and endpoints can be
  found at http://docs.aws.amazon.com/general/latest/gr/rande.html. {"exception":"[object] (InvalidArgumentException(code: 0): Missing required client configuration options:

region: (string)

  A \"region\" configuration value is required for the \"s3\" service
  (e.g., \"us-west-2\"). A list of available public regions and endpoints can be
  found at http://docs.aws.amazon.com/general/latest/gr/rande.html. at /var/app/current/vendor/aws/aws-sdk-php/src/ClientResolver.php:371)
[stacktrace]
#0 /var/app/current/vendor/aws/aws-sdk-php/src/ClientResolver.php(267): Aws\\ClientResolver->throwRequired(Array)
#1 /var/app/current/vendor/aws/aws-sdk-php/src/AwsClient.php(158): Aws\\ClientResolver->resolve(Array, Object(Aws\\HandlerList))
#2 /var/app/current/vendor/aws/aws-sdk-php/src/S3/S3Client.php(261): Aws\\AwsClient->__construct(Array)
#3 /var/app/current/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php(208): Aws\\S3\\S3Client->__construct(Array)
#4 /var/app/current/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php(125): Illuminate\\Filesystem\\FilesystemManager->createS3Driver(Array)
........

It gives a PRODUCTION error on laravel.log, while this is DEVELOPMENT, and the value is looking for is already set. As I mentioned earlier the upload works fine when I run it from the terminal as a logged user.

I am having the same problem with all the commands and also when I want to run the jobs through the schedule, the same error happens.

Anyone who can shed some light please? Thank you very much!

Solution

So to fix this, just create a config file in the .ebextensions folder and paste the following YAML code:

files:
  "/etc/cron.d/artisan_scheduler":
     mode: "000644"
     owner: root
     group: root
     content: |
       * * * * * ec2-user . $HOME/.bash_profile; php /var/app/current/artisan schedule:run >> /dev/null 2>&1
1 Answers

I think the S3 SDK environment variables are set somehow for your current shell user you're running the artisan command from (when it runs normally). You need to either check the env vars it neeeds and prepend the cron command with them or you can try to load your user existing variables within the cron command.

The most popular solution appears to be from this question and offers you to prepend your cron command with:

. $HOME/.profile;

Related