Google-cloud-java: How to create instance from snapshot

Created on 26 May 2016  路  7Comments  路  Source: googleapis/google-cloud-java

Hi guys, I did not find discussion forum or group so I'm asking here.

I have an VM instance in google cloud and I need to create many clones of the instance. So I created snapshot of instance's disk in Google Cloud Console. The console allows to create new instance based on snapshot, but I need to do it via GCloud Java API, how can I do that?

Thanks

compute

All 7 comments

Hi @ukman,

On StackOverflow you can use the gcloud-java tag.
In general to see usage examples for gcloud-java you can have a look at our gcloud-java-examples module.
A detailed example of gcloud-java-compute features can be found in ComputeExample.java.
In CreateAddressDiskAndInstance.java you have an example of how to create an Instance after creating an Address and a Disk. You can follow that example and change the way the disk is created to create it from your Snapshot:

DiskId diskId = DiskId.of("us-central1-a", "test-disk");
SnapshotId snapshotId = SnapshotId.of("your-snapshot-id");
SnapshotDiskConfiguration diskConfiguration = SnapshotDiskConfiguration.of(snapshotId);
DiskInfo disk = DiskInfo.of(diskId, diskConfiguration);
operation = compute.create(disk);
// Wait for operation to complete
while (!operation.isDone()) {
  Thread.sleep(1000L);
}
// Check operation errors
operation = operation.reload();
if (operation.errors() == null) {
  System.out.println("Disk " + diskId + " was successfully created");
} else {
  // inspect operation.errors()
  throw new RuntimeException("Disk creation failed");
}

If you are not interested in creating a static address for your instance you can skip that part and create the instance as follows (notice that I remove the code to set the access configurations for the NetworkInterface):

InstanceId instanceId = InstanceId.of("us-central1-a", "test-instance");
NetworkId networkId = NetworkId.of("default");
PersistentDiskConfiguration attachConfiguration =
  PersistentDiskConfiguration.builder(diskId).boot(true).build();
AttachedDisk attachedDisk = AttachedDisk.of("dev0", attachConfiguration);
NetworkInterface networkInterface = NetworkInterface.builder(networkId).build();
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
InstanceInfo instance =
  InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface);
operation = compute.create(instance);
// Wait for operation to complete
while (!operation.isDone()) {
  Thread.sleep(1000L);
}
// Check operation errors
operation = operation.reload();
if (operation.errors() == null) {
  System.out.println("Instance " + instanceId + " was successfully created");
} else {
  // inspect operation.errors()
  throw new RuntimeException("Instance creation failed");
}

Hope this helps.

Marco, thanks a lot, it works!

Glad to here this. Please do not hesitate to contact us if you need help. Bug reports are more than welcome :)

Marco, do you have example for access configuration? I need external ephemeral IP for my VM, but cannot do this.
I found the following example, but it does not work.

  AccessConfig accessConfig = AccessConfig.builder().name("external-nat").build();
  compute.addAccessConfig(instance.instanceId(), "nic-0", accessConfig);

Hi @ukman,

I believe that the default network interface for an instance is called nic0 (notice the missing "-"). You can verify this by checking compute.getInstance(instanceId). The following code should work:

AccessConfig accessConfig = AccessConfig.builder().name("external-nat").build();
compute.addAccessConfig(instance.instanceId(), "nic0", accessConfig);

Notice that you can also assign an ephemeral address at creation time (to spare one extra RPC call) with something like:

Compute compute = ComputeOptions.defaultInstance().service();
ImageId imageId = ImageId.of("debian-cloud", "debian-8-jessie-v20160329");
NetworkId networkId = NetworkId.of("default");
AttachedDisk attachedDisk =
    AttachedDisk.of(CreateDiskConfiguration.builder(imageId).autoDelete(true).build());
NetworkInterface networkInterface = NetworkInterface.builder(networkId)
    .accessConfigurations(NetworkInterface.AccessConfig.builder().name("external-nat").build())
    .build();
InstanceId instanceId = InstanceId.of("us-central1-a", "instance-name");
MachineTypeId machineTypeId = MachineTypeId.of("us-central1-a", "n1-standard-1");
Operation operation =
    compute.create(InstanceInfo.of(instanceId, machineTypeId, attachedDisk, networkInterface));
while (!operation.isDone()) {
    Thread.sleep(1000L);
}

Please let me know if this fixed your issue.

It works! Thanks!

Hi mziccard,

Could you please share code to create google cloud instance using java core code by authenticating google cloud client id and secrect?

If possible pls send complete expample to create vm from image using java code.

Was this page helpful?
0 / 5 - 0 ratings