检索新配置的EC2实例的ID

时间:2015-09-01 13:57:34

标签: php amazon-web-services amazon-ec2 aws-sdk

我正在尝试使用this AWS博客文章作为起点加快从特定AMI启动实例的时间,但是我不知道如何获取新获取的实例的ID我已经使用PHP API的V3(在博客文章中反对V2)进行了调整,因此我可以检索有关实例的更多信息(使用我将检索到的ID)

<?php

require 'C:\wamp\bin\php\php5.5.12\vendor\autoload.php';

use Aws\Ec2\Ec2Client;

$ec2Client = \Aws\Ec2\Ec2Client::factory(array(
    'region'  => 'eu-west-1',
    'version' => 'latest'
));

//Default vars
$aws_key = 'aws-ireland';
$ami_id = 'ami-000000';
$min_count = '1';
$max_count = '1';
$instance_type = 't2.micro';
$instance_region = 'eu-west-1b';
$server_name = 'API Test Server';

$result = $ec2Client->runInstances(array (
    //Creating the instance
    'KeyName' => $aws_key,
    'ImageId' => $ami_id,
    'MinCount' => $min_count,
    'MaxCount' => $max_count,
    'InstanceType' => $instance_type,
    'Placement' => array('AvailabilityZone' => $instance_region),
));

//Wait for server to be created

//Return the instance ID

在博客文章之后,由于方法waitUntilInstanceRunning在API的V3中不存在,因此导致错误导致错误。我相信我需要使用服务员,但我不确定如何将此用于我的问题?

1 个答案:

答案 0 :(得分:0)

这个怎么样:

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