Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Using the Compute Engine API

The Compute Engine API allows you to create and run virtual machines (VMs) on Google Cloud.

This guide shows you how to initialize the Compute Engine client library for Rust, and how to perform some basic operations using the library.

Pre-requisites

The guide assumes you have an existing Google Cloud project with billing enabled.

Add the client library as a dependency

Use cargo to add the necessary dependency:

cargo add google-cloud-compute-v1

List all the virtual machines

The client to create and manipulate virtual machines is called Instances. You can list all the VMs in a project using the list() function of this type:

// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub async fn quickstart(project_id: &str) -> anyhow::Result<()> {
    use google_cloud_compute_v1::client::Instances;
    use google_cloud_gax::paginator::ItemPaginator;

    const ZONE: &str = "us-central1-a";

    let client = Instances::builder().build().await?;
    println!("Listing instances for project {project_id}");
    let mut instances = client
        .list()
        .set_project(project_id)
        .set_zone(ZONE)
        .by_item();
    while let Some(item) = instances.next().await.transpose()? {
        println!("  {:?}", item.name);
    }
    println!("DONE");
    Ok(())
}

Next Steps