Skip to content

Instantly share code, notes, and snippets.

@nodeGarden
Created May 5, 2013 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodeGarden/db707771faf12f7db47a to your computer and use it in GitHub Desktop.
Save nodeGarden/db707771faf12f7db47a to your computer and use it in GitHub Desktop.
<?
include "aws/sdk.class.php";
$config = array();
$config['key'] = 'key';
$config['secret'] = 'secret';
$config['region'] = 'us-east-1d';
$ec2Client = \Aws\Ec2\Ec2Client::factory($config);
$result = $ec2Client->DescribeInstances(array(
'Filters' => array(
array('Name' => 'instance-type', 'Values' => array('m1.small')),
)
));
echo "<pre>";
print_r($result);
echo "</pre>";
echo "<br/><br/>";
$reservations = $result['Reservations'];
foreach ($reservations as $reservation) {
$instances = $reservation['Instances'];
foreach ($instances as $instance) {
$instanceName = '';
foreach ($instance['Tags'] as $tag) {
if ($tag['Key'] == 'Name') {
$instanceName = $tag['Value'];
}
}
echo 'Instance Name: ' . $instanceName . PHP_EOL;
echo '---> State: ' . $instance['State']['Name'] . PHP_EOL;
echo '---> Instance ID: ' . $instance['InstanceId'] . PHP_EOL;
echo '---> Image ID: ' . $instance['ImageId'] . PHP_EOL;
echo '---> Private Dns Name: ' . $instance['PrivateDnsName'] . PHP_EOL;
echo '---> Instance Type: ' . $instance['InstanceType'] . PHP_EOL;
echo '---> Security Group: ' . $instance['SecurityGroups'][0]['GroupName'] . PHP_EOL;
}
}
?>
// OUTPUT:
// PHP Fatal error: Class 'Aws\\Ec2\\Ec2Client' not found in /var/www/functions/ec2_instances.php on line 9
@victorsmirnov
Copy link

Hello,

I think you are using SDK version 1.

Please install second version. You can do this with Composer - http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/installation.html

I personally prefer to install dependencies with Composer but there other options. For example you can just download the source and setup autoloader. Composer implements autoloading for you so that you don't have to include any AWS class files.

Warm regards,
Victor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment