Skip to content

Instantly share code, notes, and snippets.

@bshaffer
Created February 1, 2021 21:18
Show Gist options
  • Save bshaffer/3fd1f0b9b1ac736c49a9364d7a9e6b79 to your computer and use it in GitHub Desktop.
Save bshaffer/3fd1f0b9b1ac736c49a9364d7a9e6b79 to your computer and use it in GitHub Desktop.
<?php
require 'vendor/autoload.php';
use Google\Cloud\Compute\V1\Disk;
use Google\Cloud\Compute\V1\Instance;
use Google\Cloud\Compute\V1\AttachedDisk;
use Google\Cloud\Compute\V1\NetworkInterface;
$instancesClient = new Google\Cloud\Compute\V1\InstancesClient();
$disksClient = new Google\Cloud\Compute\V1\DisksClient();
$project = 'YOUR_PROJECT_ID';
$zone = 'us-central1-f';
$diskName = 'new-disk';
$instanceName = 'new-instance';
// New Network Instance
$networkInterface = new NetworkInterface();
$networkInterface->setNetwork(sprintf('https://www.googleapis.com/compute/v1/projects/%s/global/networks/default', $project));
// New Disk
$disk = new Disk();
$disk->setName($diskName);
$insertDisk = $disksClient->insert($disk, $project, $zone);
$attachDisk = new AttachedDisk();
$attachDisk->setBoot(true);
$attachDisk->setSource(sprintf('https://www.googleapis.com/compute/v1/projects/%s/zones/%s/disks/%s', $project, $zone, $diskName));
$newInstance = new Instance();
$newInstance->setName($instanceName);
$newInstance->setDisks([$attachDisk]);
$newInstance->setMachineType(sprintf('https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/n1-standard-1', $project, $zone));
$newInstance->setNetworkInterfaces([$networkInterface]);
$insertInstance = $instancesClient->insert($newInstance, $project, $zone);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment