Rakib
Rakib

Reputation: 13095

How To read through environment variables of an Amazon EC2 instance on the fly in PHP

I tried the following in PHP

    $AWS_environment_variables = array(
        "AWS_PATH"              => NULL,
        "AWS_AUTO_SCALING_HOME" => NULL,
        "AWS_IAM_HOME"          => NULL,
        "AWS_ELB_HOME"          => NULL,
        "HOSTNAME"              => NULL,
        "EC2_AMITOOL_HOME"      => NULL,
    );

    foreach($AWS_environment_variables as $key_name => &$value){
        $value = getenv($key_name);
    }

    echo "<pre>"; 
    print_r($AWS_environment_variables); 
    echo "</pre>";

I'm getting the following when run inside ec2. All AWS specific stuff is empty

Array
(
    [AWS_PATH] => 
    [AWS_AUTO_SCALING_HOME] => 
    [AWS_IAM_HOME] => 
    [AWS_ELB_HOME] => 
    [HOSTNAME] => 
    [EC2_AMITOOL_HOME] => 
)

Hashtag #HowDoIDoIt?

Upvotes: 4

Views: 1297

Answers (1)

Tech Consultant
Tech Consultant

Reputation: 370

try this

print_r($_ENV);
print_r($_SERVER);
print_r($GLOBALS);
phpinfo();

you should find the vars there (reference - https://forums.aws.amazon.com/message.jspa?messageID=447927)

Upvotes: 2

Related Questions