クイックスタート: Rust で Compute Engine を使用する

Compute Engine API を使用すると、 Google Cloudで仮想マシン(VM)を作成して実行できます。

このガイドでは、Rust 用の Compute Engine クライアント ライブラリを初期化し、ライブラリを使用して基本的なオペレーションを実行する方法について説明します。

前提条件

このガイドを開始する前に、課金が有効になっている既存の Google Cloud プロジェクトがあることを確認してください。

クライアント ライブラリを依存関係として追加する

cargo を使用して、google-cloud-compute-v1 クレートを Cargo.toml ファイルに追加します。

cargo add google-cloud-compute-v1

仮想マシンを一覧表示する

仮想マシンを作成、管理するためのクライアントは Instances と呼ばれます。このタイプの list() メソッドを使用して、プロジェクトとゾーン内の VM を一覧表示できます。

pub async fn sample(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(())
}

次のステップ