如何使用AWS SDK for PHP 2获取与某些过滤器匹配的Amazon EC2实例列表?
答案 0 :(得分:31)
使用DescribeInstances方法。让我们用更多细节来说明这一点。
您需要先获取Ec2Client实例。初始化客户端的最简单方法:
$config = array();
$config['key'] = 'key';
$config['secret'] = 'secret';
$config['region'] = 'us-east-1';
$config['version'] = 'latest'; // Or Specified
$ec2Client = \Aws\Ec2\Ec2Client::factory($config);
然后只需调用DescribeInstances
方法。
$result = $ec2Client->DescribeInstances(array(
'Filters' => array(
array('Name' => 'instance-type', 'Values' => array('m1.small')),
)
));
您可以在亚马逊DescribeInstances API方法页面上获取可用过滤器列表。
但等等,这里可能有什么困难?
Filters
。在API中,它被称为Filter
Values
与API不同,它是一个数组是的,这些都在文档中描述。但是如果你看一些Old API usage samples,你会发现语法已经改变了,这可能很难注意到那个例子中需要更新的东西才能使事情有效。
为了完成这个例子,让我展示一些简单的结果输出。
$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;
}
}
答案 1 :(得分:2)
维克多的答案很棒,但它并不适合我,因为我错过了一行:
$reservations=$result->toArray();
Amazon PHP SDK 2为很多事情(包括这个)返回Guzzle Model对象,并且在foreach工作之前需要将它们转换为数组。更多信息:
http://guzzlephp.org/api/class-Guzzle.Service.Resource.Model.html
答案 2 :(得分:1)
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>';
}
}
答案 3 :(得分:0)
出于获取PublicDnsName
列表的目的,您可以使用此代码:
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);
您将收到以下内容:
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,
)