Reputation: 3780
How to get list of Amazon EC2 instances matching some filters using AWS SDK for PHP 2?
Upvotes: 16
Views: 16205
Reputation: 17071
With purpose to get list of PublicDnsName
you can use this code:
use Aws\Ec2\Ec2Client;
$ec2 = Ec2Client::factory($config);
$args = [
'Filters' => [
['Name' => 'tag:Name', 'Values' => ['*{{your-tag}}*']],
]
];
$data = $ec2->DescribeInstances($args)->toArray();
$instances = [];
array_walk_recursive($data, function ($value, $key) use (&$instances) {
if ($key === 'PublicDnsName') {
$instances[$value] = true;
}
});
var_export($instances);
You will receive something like this:
array (
'ec2-*-*-*-*.eu-west-1.compute.amazonaws.com' => true,
'ec2-*-*-*-*.eu-west-1.compute.amazonaws.com' => true,
'ec2-*-*-*-*.eu-west-1.compute.amazonaws.com' => true,
'ec2-*-*-*-*.eu-west-1.compute.amazonaws.com' => true,
'ec2-*-*-*-*.eu-west-1.compute.amazonaws.com' => true,
'ec2-*-*-*-*.eu-west-1.compute.amazonaws.com' => true,
)
Upvotes: 0
Reputation: 3780
Use DescribeInstances method for this. Let's cover this with some more details.
You need to get Ec2Client instance first. The easiest way to initialize the client:
$config = array();
$config['key'] = 'key';
$config['secret'] = 'secret';
$config['region'] = 'us-east-1';
$config['version'] = 'latest'; // Or Specified
$ec2Client = \Aws\Ec2\Ec2Client::factory($config);
And then just call DescribeInstances
method.
$result = $ec2Client->DescribeInstances(array(
'Filters' => array(
array('Name' => 'instance-type', 'Values' => array('m1.small')),
)
));
You can get list of available filters on the Amazon DescribeInstances API method page.
But wait, what might be difficult here?
Filters
. In the API it is called Filter
Values
is called different from API and it is an arrayYes, this is all described in the documentation. But if you look at some Old API usage samples you can see that the syntax has changed and this might be really hard to notice what have to be updated in that examples to make things working.
And to complete the example let me show some simple output of the results.
$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;
}
}
Upvotes: 31
Reputation: 59
Victor's answer is great, but it wasn't working for me because I was missing one line:
$reservations=$result->toArray();
The Amazon PHP SDK 2 returns Guzzle Model objects for many things (including this), and they need to be converted to arrays before foreach will work. More info here:
http://guzzlephp.org/api/class-Guzzle.Service.Resource.Model.html
Upvotes: 3
Reputation: 17
that was of wonderful help Victor, hey voidstin, that was not required in my case [$reservations=$result->toArray();]
require "aws.phar";
use Aws\Ec2\Ec2Client;
use Aws\Common\Enum\Region;
$aws = Ec2Client::factory(array(
'key' => 'XXXXXX', //Your key and secret key are found at https://portal.aws.amazon.com/gp/aws/securityCredentials
'secret' => 'XXXXXX',
'region' => 'XXXXXX' //This is the server cluster we are connecting to. US_EAST_1 is Northern Virginia. US_WEST_1 is Northern California. US_WEST_2 is Oregon
));
$result = $aws->DescribeInstances();
$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 '<br>';
echo '---> State: ' . $instance['State']['Name'] . PHP_EOL;
echo '<br>';
echo '---> Instance ID: ' . $instance['InstanceId'] . PHP_EOL;
echo '<br>';
echo '---> Image ID: ' . $instance['ImageId'] . PHP_EOL;
echo '<br>';
echo '---> Private Dns Name: ' . $instance['PrivateDnsName'] . PHP_EOL;
echo '<br>';
echo '---> Instance Type: ' . $instance['InstanceType'] . PHP_EOL;
echo '<br>';
echo '---> Security Group: ' . $instance['SecurityGroups'][0]['GroupName'] . PHP_EOL;
echo '<br>';
echo '-----------------------------------------------------------------------------------------------------';
echo '<br>';
echo '<br>';
}
}
Upvotes: 1