16

I'm trying to run a PHP script that is triggered by a cron script (in cron.d). The script is triggered properly but it is missing the Elastic Beanstalk "Environment Variables" that are stored in the $_SERVER superglobal. The script is being run as the user "root" for now, but it's not in the same environment that has the environment variables. The variables are set correctly, if I run the script from a full shell it runs just fine.

Where are the "exports" for these variables? Where do they get set? I found the SetEnvs for Apache in /etc/apache/conf.d/aws_env.conf. I can't find anything in the user's .bashrc, .bash_profile, etc. Is there a workaround? A better way to do this?

Thanks.

four43
  • 1,675
  • 2
  • 20
  • 33

10 Answers10

17

While searching for solutions to the same problem I ran into this blog post: http://sebgoo.blogspot.nl/2013/10/elastic-beanstalk-cron-command-and-rds.html. To summarize, you can load the Elastic Beanstalk environment variables using the opt/elasticbeanstalk/support/envvars file:

0 3 * * * . /opt/elasticbeanstalk/support/envvars; some_command

Hope this helps!

Elte Hupkes
  • 2,773
  • 2
  • 23
  • 18
9

I just found this, using

grep -r "export MY_VAR" /

EDIT: Amazon seems to move the file location from time to time. Current location is:

/opt/elasticbeanstalk/support/envvars

So I think I'll just include (source [file path]) that in my script before calling my php script. Still seems like a funky way to do things. I'm still in for better solutions.

I was running PHP via bash script triggered by cron. So to setup the environment, I would do something like this:

#!/bin/bash 
source /opt/elasticbeanstalk/support/envvars
php -f my-script.php

See @userid53's answer below for PHP solution.

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
four43
  • 1,675
  • 2
  • 20
  • 33
  • Could you explain how you're including it? Is it in the .ebextensions file? I am having the same issue. – AXM May 24 '14 at 20:19
  • 2
    @AlexMarkov use this command: `$ source /opt/elasticbeanstalk/support/envvars.d/sysenv` and all variables set to that file will be available as Environment Variables in current shell. – ggirtsou May 29 '14 at 13:23
  • @userid53 Thanks man! I actually got around with using `. $HOME/.bash_profile; ` before the command. I guess it is the same thing? – AXM May 30 '14 at 15:16
  • In bash? That might reload the environment. Is there a reference to that sysenv file we've been talking about? – four43 May 30 '14 at 18:41
  • Thank you so much! This worked great. My crontask looks like this `. /opt/elasticbeanstalk/support/envvars.d/sysenv && /path/to/php /path/to/my/php/script.php` – Cameron Jun 01 '14 at 22:19
  • 3
    The file was moved to /opt/elasticbeanstalk/support/envvars – Jose Nobile May 13 '15 at 17:14
4

I spent several hours trying to figure out how to pass Environment Variables to PHP CLI. I tried:

  • setting in ebextensions config: $ source /opt/elasticbeanstalk/support/envvars.d/sysenv
  • setting all my Environment variables to config file.

No matter what I tried, env variables won't pass to PHP CLI.

When I log to my EC2 instance as ec2-user and do this: $ echo $ENVIRONMENT I get prod. If I do it as $ sudo su and then $ echo $ENVIRONMENT I get prod.

If I manually run the PHP CLI file (used in cronjob) my script works. When it runs automatically (via cronjob) Environment Variables are not passed to my script.

Here's what I did. Put this in your cronjob entry script:

$variables = '/opt/elasticbeanstalk/support/envvars.d/sysenv';

if (file_exists($variables) && is_file($variables)) {
    $contents = file_get_contents($variables);
    foreach(explode("\n", $contents) as $line) {
        if (empty($line)) continue;

        $new_line = str_replace('export ', '', $line);
        $first_part = strpos($new_line, '=');

        $last_part = substr($new_line, $first_part, strlen($new_line));
        $variable_value = str_replace(array('=', '"'), array('',''), $last_part);

        $variable_name = substr($new_line, 0, $first_part);
        putenv($variable_name."=".$variable_value);
    }
}

It extracts each line from /opt/elasticbeanstalk/support/envvars.d/sysenv file, removes the export part, gets the variable name & value, and sets it via putenv() function.

ggirtsou
  • 2,050
  • 2
  • 20
  • 33
  • That's a good way to do it in PHP. I was running a bash script via cron, so I would do something like: #!/bin/bash source /opt/elasticbeanstalk/support/envvars.d/sysenv php -f my-script.php Sorry for the lack of newlines. – four43 May 29 '14 at 19:19
  • 1
    Saved the day after hours of scratching my head! – Adam B Jul 18 '15 at 05:20
  • For anyone wanting to run Laravel queue worker with SQS through Artisan you need something like this to load those environment variables before PHP will have them in the CLI. – danielwashbrook Nov 23 '18 at 22:26
4

It's works for me with a Laravel Project

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/artisan schedule:run 1>> /dev/null 2>&1

For a non Laravel Project you can test that:

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /path/to/your/script.php 1>> /dev/null 2>&1

Hope this helps!

Zaghyr
  • 41
  • 2
4

in version 3.0.3 it has changed again, use this to export your envars in command line

file="/opt/elasticbeanstalk/deployment/env"
        while IFS=: read -r f1
        do
            export $f1
        done <"$file"
Peter Bean
  • 313
  • 2
  • 9
3

I know this is an old thread but I recently needed to do something similar in a Node.js environment deployed on Elastic Beanstalk; for what I could tell, the file /opt/elasticbeanstalk/support/envvars is not present on Node.js environments. I ended up writing a Python script that loaded the environment variables from /opt/elasticbeanstalk/bin/get-config, and then executing my Node.js script from there.

The code I ended up using was:

#!/usr/bin/env python

import os
import subprocess
from subprocess import Popen, PIPE, call
import simplejson as json

envData = json.loads(Popen(['/opt/elasticbeanstalk/bin/get-config', 'environment'], stdout = PIPE).communicate()[0])

for k, v in envData.iteritems():
    os.environ[k] = v

call(["babel-node", "/var/app/current/<script_name>.js"])

Hope this helps anyone needing to do the same.

For the complete configuration I deployed, you can refer to my original post How to set environment variables in Amazon Elastic Beanstalk when running a cron job (Node.js)

M3talM0nk3y
  • 1,382
  • 14
  • 22
2

I added the following line to my shell script:

source /opt/elasticbeanstalk/support/envvars .bash_profile

So my script, which is executed by the crontab, looks like this:

#!/bin/sh
source /opt/elasticbeanstalk/support/envvars .bash_profile


# do some php stuff
totas
  • 10,288
  • 6
  • 35
  • 32
1

In case you need something similar for CodeIgniter:

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/index.php controller method

Example:

* * * * * root . /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/index.php tasks pushNotification

A more descriptive alternative:

* * * * * root source /opt/elasticbeanstalk/support/envvars && /usr/bin/php /var/www/html/index.php tasks pushNotification
Lucas
  • 383
  • 1
  • 5
  • 17
1

AWS and EBS are changing the location of the env vars from time to time. At the current time, I was able to retrieve the env vars from the following path (I've python EBS)

/opt/python/current/env

Jamal Alkelani
  • 606
  • 7
  • 19
1

On Amazon Linux 2 instances, environment variables should be loaded from a different file :

0 3 * * * export $(cat /opt/elasticbeanstalk/deployment/env | xargs); some_command
Enalla
  • 308
  • 1
  • 3
  • 6