2013-10-22

Elastic Beanstalk - cron command and RDS DB access

Elastic Beanstalk - cron command and RDS DB access

Problem

I have a console command in php which needs an access to DB. The command need to be launched via cron.

The DB connection string looks like like this

'connectionString' => 'mysql:host='.$_SERVER['RDS_HOSTNAME'].';port='.$_SERVER['RDS_PORT'].';dbname='.$_SERVER['RDS_DB_NAME'],

where RDS_xxx parameters come from environment variables.

The problem is that cron launches the command in a clean environment (there are no RDS_xx variables). So the command fails to access the database.

Solution

Solution is to set the required environment variables before launching the command and this can be done with '/opt/elasticbeanstalk/support/envvars' script:

0 3 * * * . /opt/elasticbeanstalk/support/envvars; /var/www/html/console/yiic mycommand

1 comment:

  1. This is a great solution!
    I actually found the file in: "/opt/elasticbeanstalk/containerfiles/envvars.d/sysenv"
    But again, great solution, thanks for sharing!

    ReplyDelete