GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — How to create instance from snapshot · Issue #1026 · googleapis/google-cloud-java

How to create instance from snapshot #1026

New issue
Copy link
New issue
Copy link
Closed
Closed
How to create instance from snapshot#1026
Copy link
Assignees
ukman
Labels
api: computeIssues related to the Compute Engine API.

Description

@ukman
ukman
opened on May 26, 2016
Issue body actions

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

Activity

Next
  1. mziccard commented on May 26, 2016

    @mziccard
    mziccard
    on May 26, 2016
    Contributor
    More actions

    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.

    Reacted by Sergey Grigorchuk
  2. mziccard
    added
    api: computeIssues related to the Compute Engine API.
    on May 26, 2016
  3. ukman commented on May 26, 2016

    @ukman
    ukman
    on May 26, 2016
    Author
    More actions

    Marco, thanks a lot, it works!

  4. ukman
    closed this as completedon May 26, 2016
  5. mziccard commented on May 26, 2016

    @mziccard
    mziccard
    on May 26, 2016
    Contributor
    More actions

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

    Reacted by Sergey Grigorchuk
  6. ukman commented on Jun 2, 2016

    @ukman
    ukman
    on Jun 2, 2016
    Author
    More actions

    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);
    
  7. ukman
    reopened this on Jun 2, 2016
  8. mziccard commented on Jun 2, 2016

    @mziccard
    mziccard
    on Jun 2, 2016
    Contributor
    More actions

    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.

    Reacted by Sergey Grigorchuk
  9. ukman commented on Jun 2, 2016

    @ukman
    ukman
    on Jun 2, 2016
    Author
    More actions

    It works! Thanks!

  10. ukman
    closed this as completedon Jun 2, 2016
  11. ashokreddy2 commented on Dec 5, 2018

    @ashokreddy2
    ashokreddy2
    on Dec 5, 2018
    More actions

    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.

  12. JustinBeckwith
    assigned
    ukman
    on Feb 1, 2021
  13. github-actions
    added a commit that references this issue on Aug 18, 2022

    deps: update dependency com.google.cloud:google-cloud-bigquery to v2.…

    6ed66cc
  14. github-actions
    added a commit that references this issue on Sep 19, 2022

    chore(main): release 3.3.0 (#1025)

    9bd4e09
  15. github-actions
    added a commit that references this issue on Sep 30, 2022

    feat: Add Agent Assist Summarization API (https://cloud.google.com/ag…

    adbb984
  16. 3 remaining items

  17. github-actions
    added 2 commits that reference this issue on Oct 6, 2022

    deps: update dependency com.google.cloud:google-cloud-pubsub to v1.12…

    cbfc55a

    chore(main): release 3.7.6 (googleapis#1029)

    83ffc4f
  18. github-actions
    added a commit that references this issue on Nov 9, 2022

    deps: update dependency com.google.cloud:google-cloud-core to v2.8.27 (…

    ea10fcc
  19. suztomo
    added a commit that references this issue on Feb 1, 2023

    chore(main): release 2.9.0 (#1026)

    c449b3d
  20. chingor13
    added a commit that references this issue on Jan 6, 2026

    chore(main): release 0.129.7-SNAPSHOT (#1026)

    61ea6f2
  21. chingor13
    added a commit that references this issue on Jan 22, 2026

    chore(main): release 2.14.2-SNAPSHOT (#1026)

    ef2c74c
  22. chingor13
    added a commit that references this issue on Feb 24, 2026

    chore(deps): update dependency protobuf to v4 (#1026)

    335224d
  23. chingor13
    added a commit that references this issue on Mar 12, 2026

    chore(deps): update dependency protobuf to v4 (#1026)

    b42dd2c
  24. suztomo
    added 2 commits that reference this issue on Mar 23, 2026

    fix(java): initialize classes at build-time to address native image 2…

    d5e4cb6

    chore(main): release 2.7.12 (googleapis#1027)

    a985d09
  25. suztomo
    added a commit that references this issue on Mar 30, 2026

    chore(deps): update dependency com.google.cloud:libraries-bom to v26.…

    e77e676
  26. lqiu96
    added a commit that references this issue on Apr 1, 2026

    chore(deps): update dependency com.google.cloud:libraries-bom to v26.…

    5142820
  27. meltsufin
    added 2 commits that reference this issue on Apr 29, 2026

    docs(sample): Add sample for Native Image support in Pub/Sub (#1026)

    a4fa69b

    docs(sample): Add sample for Native Image support in Pub/Sub (#1026)

    ca4cab6
  28. benjaminp
    added a commit that references this issue on Jul 13, 2026

    chore: change branch master to main (googleapis#1026)

    1ad7c0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @ukman
    ukman

Labels

api: computeIssues related to the Compute Engine API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.