来自PHP SDK的AWS EC2 Spot请求履行服务员无限期等待

时间:2017-08-18 16:28:14

标签: amazon-ec2 aws-php-sdk

我试图了解一个现场实例的最终一致性,以便我可以在我的应用程序中分配它。这是我所见过的,来自一个相当简单的现场实例调用。

请求的实例立即启动(快速15-30秒实例运行状态),但服务员无限期地继续。

我不确定问题是我的代码,SDK还是AWS没有快速完成请求。

$spotEc2Client = $this->sdk->createEc2([]);

// Create instance via spot request
$result        = $spotEc2Client->requestSpotInstances($spotConfig);
$spotResult    = $result->getPath('SpotInstanceRequests');
$spotRequestId = $spotResult[0]['SpotInstanceRequestId'];

$now = date("F d, Y H:i:s A");
echo "Spot Request ID is: $spotRequestId\n";
echo "Starting at $now MST\n";
$spotEc2Client->waitUntil(
    'SpotInstanceRequestFulfilled',
    [
        'SpotInstanceRequestId' => $spotRequestId,
        '@waiter' => [
            'before' => function (CommandInterface $command, $attempts) {
                $now = date("H:i:s");
                printf(
                    "[$now] About to send %s. Attempt %d\n",
                    $command->getName(),
                    $attempts
                );
            }
        ],
    ]
);

输出:

Spot Request ID is: sir-<my request id>
Starting at August 18, 2017 10:10:20 AM MST
[10:10:20] About to send DescribeSpotInstanceRequests. Attempt 1
[10:10:21] About to send DescribeSpotInstanceRequests. Attempt 2
[10:10:36] About to send DescribeSpotInstanceRequests. Attempt 3
[10:10:52] About to send DescribeSpotInstanceRequests. Attempt 4
[10:11:07] About to send DescribeSpotInstanceRequests. Attempt 5
[10:11:23] About to send DescribeSpotInstanceRequests. Attempt 6
[10:11:38] About to send DescribeSpotInstanceRequests. Attempt 7

将检查时间与发布时间进行比较:

spot instance launch time

我认为我需要对describeInstances进行自己的轮询,专门查看正在运行或初始化的点实例,并自行处理它 - 因为调用{{1}在我能够分辨的情况下,无限期地继续下去。

1 个答案:

答案 0 :(得分:0)

我错误地格式化了SpotRequestId的论点。 AWS PHP SDK的github上的某个人很快就向我指出了这一点!

我的spot实例请求的格式化需要复数和数组作为参数。

$spotEc2Client->waitUntil(
    'SpotInstanceRequestFulfilled',
    [
        'SpotInstanceRequestId' => $spotRequestId,
    ]
    ...

要成为:

$spotEc2Client->waitUntil(
    'SpotInstanceRequestFulfilled',
    [
        'SpotInstanceRequestIds' => [
            $spotRequestId
    ]
    ...

我认为作为一个API用户,我更喜欢异常,或者对错误的参数发出警告,但这是对另一个地方的讨论。