빠른 시작: 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(())
}

다음 단계