Skip to content

Instantly share code, notes, and snippets.

@dsmrt
Last active August 4, 2016 04:58
Show Gist options
  • Save dsmrt/c8c3194380f12aa6a694f58bf393a731 to your computer and use it in GitHub Desktop.
Save dsmrt/c8c3194380f12aa6a694f58bf393a731 to your computer and use it in GitHub Desktop.
I was surprised to see how it is to get the ElasticBeanstalk (EB) environment name from the EB instance. I wrote this quickly to fetch the name so I can run a switch on that, and have different configurations on different environments running crons.
<?php
$instanceId = file_get_contents('http://169.254.169.254/latest/meta-data/instance-id');
//I'd prefer to use the SDK here
exec('aws ec2 describe-instances --filters "Name=instance-id,Values='.$instanceId.'"',$results);
$instances = json_decode(implode(PHP_EOL,$results),true);
//fingers crossed
$tags = $instances['Reservations'][0]['Instances'][0]['Tags'];
$keyMatch = 'elasticbeanstalk:environment-name';
$environment = null;
foreach($tags as $tag) {
if($tag['Key'] == $keyMatch){
$environment = $tag['Value'];
}
}
echo $environment;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment