kpgenes
kpgenes

Reputation: 113

How to define disk in gcp in a regional scope?

I am new to GCP and trying use Java SDK to create a template.

I am using the following code snippet and after executing it, I'm getting this error: "Disk must be in a regional scope".

InstanceTemplate instanceTemplate = new InstanceTemplate();
AttachedDisk attachedDisk = new AttachedDisk();
attachedDisk.setSource("");
List<AttachedDisk> list = new ArrayList<>();
list.add(attachedDisk);
InstanceProperties instanceProperties = new InstanceProperties();
instanceProperties.setDisks(list);
instanceProperties.setMachineType("e2");

List<NetworkInterface> listNetworkInterface = new ArrayList<>();
NetworkInterface networkInterface = new NetworkInterface();
networkInterface.setName("myname");
networkInterface.setNetwork("");
networkInterface.setSubnetwork("");
listNetworkInterface.add(networkInterface);
instanceProperties.setNetworkInterfaces(listNetworkInterface);

instanceTemplate
    .setName("instance-template-1")
    .setDescription("desc")
    .setProperties(instanceProperties);
compute.instanceTemplates().insert("", instanceTemplate).execute();

I am not able to understand what i am missing here.

Upvotes: 0

Views: 388

Answers (1)

Srividya
Srividya

Reputation: 2323

You must first create the disk before you can attach it. It is not possible to create and attach a disk at the same time. Also check the disk and the instance should be in the same region.

To attach the disk, use the compute.instances.attachDisk method and include the URL to the persistent disk that you’ve created.

If you are creating a new template to update an existing instance group, your new instance template must use the same network or, if applicable, the same subnetwork as the original template.

After you create and attach a new disk to a VM, you must format and mount the disk, so that the operating system can use the available storage space.

Note: Zonal disks must be specified with bare names; zonal disks specified with a path (even a matching one) results in an "Disk must be in a regional scope" error.

Upvotes: 2

Related Questions