如何使用AWS SDK for PHP 2获取与某些过滤器匹配的 Amazon EC2 实例列表?
问问题
15575 次
4 回答
30
为此使用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')),
)
));
您可以在 Amazon DescribeInstances API 方法页面上获取可用过滤器列表。
但是等等,这里可能有什么困难?
- 注意参数名称
Filters
。在 API 中,它被称为Filter
- 参数
Values
调用与 API 不同,它是一个数组
是的,这一切都在文档中进行了描述。但是,如果您查看一些旧 API 使用示例,您会发现语法已经改变,这可能很难注意到这些示例中必须更新哪些内容才能使事情正常工作。
为了完成这个例子,让我展示一些简单的结果输出。
$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;
}
}
于 2013-03-05T20:53:45.333 回答
3
维克多的回答很好,但它对我不起作用,因为我错过了一行:
$reservations=$result->toArray();
Amazon PHP SDK 2 为许多事情(包括这个)返回 Guzzle 模型对象,并且需要将它们转换为数组,然后 foreach 才能工作。更多信息在这里:
http://guzzlephp.org/api/class-Guzzle.Service.Resource.Model.html
于 2013-03-25T00:17:38.197 回答
1
这对维克多很有帮助,嘿 voidstin,在我的情况下不需要 [$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>';
}
}
于 2014-03-18T09:26:14.410 回答
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,
)
于 2017-04-19T09:38:26.623 回答