如何获得EC2实例的列表与Amazon PHP SDK 2?(How to get list of

2019-07-21 01:08发布

如何获得匹配使用一些过滤器亚马逊EC2实例的列表AWS SDK为PHP 2 ?

Answer 1:

使用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不同的,并且它是一个数组

是的,这个文档中的所有描述。 但是,如果你看一些旧的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;
    }

}


Answer 2:

维克多的答案是伟大的,但它不是为我工作,因为我缺少一条线:

$reservations=$result->toArray();

亚马逊PHP SDK 2返回很多东西(包括本)狂饮模型对象,它们需要转换到阵列之前的foreach会工作。 这里更多的信息:

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



Answer 3:

这是美妙的帮助维克多,嘿嘿voidstin,这是不是在我的情况下,要求[$保留= $ result->指定者();]

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


Answer 4:

随着宗旨,以获取列表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,
)


文章来源: How to get list of EC2 instances with Amazon PHP SDK 2?