启动 Spot 实例时出现无效时间错误

InvalidTime error on launching Spot Insances

我正在尝试启动 spot 实例,请求仅在特定日期和时间到特定日期和时间有效。以下是我的配置。

$request_valid_from = new DateTime();
$request_valid_until = add_time_interval((clone $request_valid_from), 5);

$config = [
                'BlockDurationMinutes' => 60,
                'InstanceCount' => 1,
                'SpotPrice' => strval($this->get_average_spot_price('c3.large)),
                'Type' => 'one-time',

                'LaunchSpecification' => [
                        'EbsOptimized' => false,
                        'InstanceType' => 'c3.large',
                        'ImageId' => AMI_ID,
                    ],
                'ValidFrom' => $request_valid_from,
                'ValidUntil' => $request_valid_until,
            ];

echo($config)

function add_time_interval($current_time, $interval){
        $current_time->modify( '+' .strval($interval) .  ' seconds' );
        return $current_time;
    }

当我打印配置时,我得到以下信息;

"BlockDurationMinutes": 60,
    "InstanceCount": 1,
    "SpotPrice": "0.04592",
    "Type": "one-time",
    "LaunchSpecification": {
        "EbsOptimized": false,
        "InstanceType": "c3.large",
        "ImageId": "ami-b23c22dc"
    },
    "ValidFrom": {
        "date": "2016-05-02 13:11:18.000000",
        "timezone_type": 3,
        "timezone": "Asia\/Tokyo"
    },
    "ValidUntil": {
        "date": "2016-05-02 13:11:23.000000",
        "timezone_type": 3,
        "timezone": "Asia\/Tokyo"
    }

现在,我尝试启动 Spot 实例,但出现以下错误。

InvalidTime (client): "Mon May 02 04:28:11 UTC 2016" is an invalid time

错误消息上的时间不同。所以,我在 this 之后设置了正确的 imezone。

运行 命令 date 给出了正确的时间(如请求中所示)。我不确定为什么错误消息显示不同的时间。

详细的错误信息

06-May-2016 13:40:20 Asia/Tokyo] PHP Fatal error:  Uncaught exception 'Aws\Ec2\Exception\Ec2Exception' with message 'Error executing "RequestSpotInstances" on "https://ec2.ap-northeast-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://ec2.ap-northeast-1.amazonaws.com` resulted in a `400 Bad Request` response:
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidTime</Code><Message>"Fri May 06 04:40:19 UT (truncated...)
 InvalidTime (client): "Fri May 06 04:40:19 UTC 2016" is an invalid time - <?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidTime</Code><Message>"Fri May 06 04:40:19 UTC 2016" is an invalid time</Message></Error></Errors><RequestID>efd4458f-e05a-4cb7-8332-4be2dca8c486</RequestID></Response>'

exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://ec2.ap-northeast-1.amazonaws.com` resulted in a `400 Bad Request` response:
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidTime</Code><Message>"Fri May 06 04:40:19 UT (trunca in /projects/mv2/millvi-auto-scaling/ec2-manager/vendor/aws/aws-sdk-php/src/WrappedHttpHandler.php on line 192

代码执行需要一些时间,所以我稍微延迟了 request_valid_from(2 秒)并且成功了。

$request_valid_from = new DateTime();

//Delay the request slightly (2 seconds)
$request_valid_from = add_time_interval((clone $request_valid_from), 2);

$request_valid_until = add_time_interval((clone $request_valid_from), 10);