简体   繁体   English

如何使用 Amazon PHP SDK 2 获取 EC2 实例列表?

[英]How to get list of EC2 instances with Amazon PHP SDK 2?

如何使用AWS SDK for PHP 2获取与某些过滤器匹配的 Amazon EC2 实例列表?

Use DescribeInstances method for this.为此使用DescribeInstances方法。 Let's cover this with some more details.让我们用更多的细节来说明这一点。

You need to get Ec2Client instance first.您需要先获取 Ec2Client 实例。 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.然后只需调用DescribeInstances方法。

$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.您可以在 Amazon DescribeInstances API 方法页面上获取可用过滤器的列表。

But wait, what might be difficult here?但是等等,这里有什么困难?

  • Note the parameter name Filters .请注意参数名称Filters In the API it is called Filter在 API 中它被称为Filter
  • Parameter Values is called different from API and it is an array参数Values的调用方式与 API 不同,它是一个数组

Yes, 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.但是,如果您查看一些旧 API 使用示例,您会发现语法发生了变化,这可能真的很难注意到这些示例中必须更新哪些内容才能使其正常工作。

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;
    }

}

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. Amazon PHP SDK 2 为许多事物(包括这个)返回 Guzzle Model 对象,并且它们需要在 foreach 工作之前转换为数组。 More info here:更多信息在这里:

http://guzzlephp.org/api/class-Guzzle.Service.Resource.Model.html http://guzzlephp.org/api/class-Guzzle.Service.Resource.Model.html

that was of wonderful help Victor, hey voidstin, that was not required in my case [$reservations=$result->toArray();]这对 Victor 很有帮助,嘿,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>';
}
}

With purpose to get list of PublicDnsName you can use this code:为了获得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);

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,
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM