【问题标题】:Retrieving ID of newly provisioned EC2 instance检索新配置的 EC2 实例的 ID
【发布时间】:2015-09-11 23:00:06
【问题描述】:

我正在尝试加快从特定 AMI 启动实例所需的时间,使用 this AWS 博客文章作为起点,但是我不确定如何获取我拥有的新获取的实例的 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 中不存在。我认为我需要使用服务员,但我不确定如何使用它来解决我的问题?

【问题讨论】:

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


    【解决方案1】:

    这个呢:

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

    【讨论】:

    • 看,你可以为你要创建的实例设置标签,然后使用 PHP 函数 sleep(200),在我的例子中,大约需要 200 秒才能完成设置,然后调用描述实例不传递任何参数,如 $aws->DescribeInstances()。然后,简单地比较实例的标签,得到目标实例的ID。
    • @IrfanAhmed :- 我已经尝试过您的代码,但对我不起作用。你能取悦我吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    • 2019-10-21
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2021-04-04
    相关资源
    最近更新 更多