skip to Main Content

I’ve using Google Cloud API and I’ve created instance but without IPv4.
I need instance with IPv4.

Can anyone help me to config AccessConfig?

<?php
require_once 'vendor/autoload.php';

use GoogleCloudComputeV1InstancesClient;
use GoogleCloudComputeV1AttachedDisk;
use GoogleCloudComputeV1AttachedDiskInitializeParams;
use GoogleCloudComputeV1Instance;
use GoogleCloudComputeV1NetworkInterface;
use GoogleCloudComputeV1Operation;
use GoogleCloudComputeV1ZoneOperationsClient;
use GoogleCloudComputeV1AccessConfig;

putenv('GOOGLE_APPLICATION_CREDENTIALS=my.json');

$projectId = 'myProject';
$zoneName = 'europe-west3-c';

function create_instance(
    string $projectId,
    string $zone,
    string $instanceName,
    string $machineType = 'n1-standard-1',
    string $sourceImage = 'projects/debian-cloud/global/images/family/debian-10',
    string $networkName = 'global/networks/default'
) {
    // Set the machine type using the specified zone
    $machineTypeFullName = sprintf('zones/%s/machineTypes/%s', $zone, $machineType);

    // Set the boot disk
    $diskInitializeParams = (new AttachedDiskInitializeParams())
                            ->setSourceImage($sourceImage);
    $disk = (new AttachedDisk())
        ->setBoot(true)
        ->setInitializeParams($diskInitializeParams);

    // Set the network
    $network = (new NetworkInterface());

    // Create the Instance message
    $instance = (new Instance())
        ->setName($instanceName)
        ->setDisks([$disk])
        ->setMachineType($machineTypeFullName)
        ->setNetworkInterfaces([$network]);

    // Insert the new Compute Engine instance using the InstancesClient
    $instancesClient = new InstancesClient();
    $operation = $instancesClient->insert($instance, $projectId, $zone);

    if ($operation->getStatus() === OperationStatus::RUNNING) {
        // Wait until operation completes
        $operationClient = new ZoneOperationsClient();
        $operationClient->wait($operation->getName(), $projectId, $zone);
    }

    printf('Created instance %s' . PHP_EOL, $instanceName);
}

print_r(create_instance($projectId,$zoneName,"test-instance"));

I think I missed something like this:

$accessConfig = (new AccessConfig())
    ->setNatIP('xx.xx.xx.xx')
    ->setType("ONE_TO_ONE_NAT")
    ->setName("External NAT");

$network = (new NetworkInterface())
    ->setNetworkIP('10.156.0.66')
    ->setAccessConfigs(array($accessConfig));

Can anyone help me?

Source: https://github.com/googleapis/google-cloud-php/tree/master/Compute/src/V1
And: https://github.com/GoogleCloudPlatform/php-docs-samples/blob/master/compute/cloud-client/instances/src/create_instance.php

Thanks

2

Answers


  1. Chosen as BEST ANSWER

    SOLUTION:

    $accessConfig = (new AccessConfig())
        ->setNatIP('xx.xx.xx.xx');
    
    $network = (new NetworkInterface())
        ->setAccessConfigs([$accessConfig]);
    

  2. Typically, with gcloud you would add the ip at create time of the instance:

    gcloud compute instances create INSTANCE_NAME 
        --zone [ZONE] 
        [--network-interface
            [network=NETWORK,subnet=SUBNET],
            [address=EXT_IP_NAME | no-address],
            [private-network-ip=INT_NETWORK_IP]
        ...]
    

    Not familiar with the php API but perhaps look into the instance creation API call for similar parameters.

    More details:
    https://cloud.google.com/vpc/docs/create-use-multiple-interfaces#gcloud

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search