如何获得EC2实例的列表与Amazon PHP SDK 2? [英] How to get list of EC2 instances with Amazon PHP SDK 2?

查看:121
本文介绍了如何获得EC2实例的列表与Amazon PHP SDK 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得亚马逊EC2实例为PHP 2 配套使用 AWS SDK的列表?

How to get list of Amazon EC2 instances matching some filters using AWS SDK for PHP 2?

推荐答案

使用<一个href="http://docs.aws.amazon.com/aws-sdk-php-2/latest/class-Aws.Ec2.Ec2Client.html#_describeInstances">DescribeInstances方法这一点。让我们管这个有一些更多的细节。

Use DescribeInstances method for this. Let's cover this with some more details.

您需要先获得Ec2Client实例。初始化客户端的最简单的方法:

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';
$ec2Client = \Aws\Ec2\Ec2Client::factory($config);

然后就叫 DescribeInstances 方法。

$result = $ec2Client->DescribeInstances(array(
        'Filters' => array(
                array('Name' => 'instance-type', 'Values' => array('m1.small')),
        )
));

您可以在亚马逊<一个可用过滤器列表href="http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html">DescribeInstances API方法页。

You can get list of available filters on the Amazon DescribeInstances API method page.

别急,什么可能在这里很困难?

But wait, what might be difficult here?

  • 注意参数的名称过滤器。在API它被称为过滤器
  • 参数被称为从API不同,它是一个数组
  • Note the parameter name Filters. In the API it is called Filter
  • Parameter Values is called different from API and it is an array

是的,这是所有的文档中描述。但是,如果你看一些<一href="http://stackoverflow.com/questions/10198612/how-to-programmatically-get-public-dns-of-an-instance">Old 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.

和完成的例子让我显示结果的一些简单的输出。

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

}

这篇关于如何获得EC2实例的列表与Amazon PHP SDK 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆