I am using the below code to start an instance. The code works as expected and starts an instance.
However, if $service->instances->start
fails to start an instance due to an error at GCP end, I wont receive any notice. It will execute without any exceptions thrown. I've tested this by trying to run 2 instances with more then 8 cpus in a single zone (the free account is not allowed to own more than 8 cores in a zone). While the instances never started, I didn't receive any error in this script.
How can I recover from a general starting error? Should I use timers to check a minute later if everything is running as expected? Is there a more robust way of starting instances?
<?php
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName('Google-ComputeSample/0.1');
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$service = new Google_Service_Compute($client);
$project = 'project_id'; // TODO: Update placeholder value.
$zone = 'us-east1-b'; // TODO: Update placeholder value.
$instance = 'instance-1'; // TODO: Update placeholder value.
$response = $service->instances->start($project, $zone, $instance);
// TODO: Change code below to process the `response` object:
echo '<pre>', var_export($response, true), '</pre>', "
";
On the other hand, CLI equivalent command gcloud compute instances start instance-1 --zone=us-east1-b
does return an error. Is it possible to get the error message in PHP as well?
Error message in CLI:
Starting instance(s) instance-2...failed.
ERROR: (gcloud.compute.instances.start) Quota 'CPUS' exceeded. Limit: 8.0 in region us-east1.