2

I am using Elastic Beanstalk for a project and was trying to execute a script via the console. When I did this, I was getting a strange error and tried to use Artisan Tinker to diagnose the problem.

When using Tinker, I found that it was unable to access the environment variables on Elastic Beanstalk and I suspect this might be related to why I was not able to execute a script manually via the console.

I found that Tinker could not access the environment variables because when I tried to dump something as simple as the environment "APP_ENV", it would return "null" instead of "development".

Is there a way to get Tinker to have access to the Elastic Beanstalk environment variables?

ackerchez
  • 1,684
  • 7
  • 28
  • 49

1 Answers1

0

You can use this command to start artisan tinker with your Elastic Beanstalk variables:

set -a && source <(cat /opt/elasticbeanstalk/deployment/env | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g") && set +a && /usr/bin/php /var/www/html/artisan tinker

Explaination:

The environment variables are located in the /opt/elasticbeanstalk/deployment/env file.

To load .env files I looked at this post: https://stackoverflow.com/a/66118031/7116840. This is merged into one command as following:

set -a && source <(cat /opt/elasticbeanstalk/deployment/env | sed -e '/^#/d;/^\s*$/d' -e "s/'/'\\\''/g" -e "s/=\(.*\)/='\1'/g") && set +a

Then to start php artisan tinker the following command is appended:

/usr/bin/php /var/www/html/artisan tinker

This loads php from the /usr/bin directory and than starts artisan tinker from your project directory which is located in /var/www/html

onno204
  • 71
  • 8