快速入門導覽課程:使用 Terraform 建立 VM 執行個體

本快速入門導覽課程將說明如何使用 Terraform 建立 Compute Engine 虛擬機器 (VM) 執行個體,並連線至該 VM 執行個體。

Hashicorp Terraform 是一種基礎架構即程式碼 (IaC) 工具,可讓您佈建及管理雲端基礎架構。Cloud de Confiance 的 Terraform 供應商 (Cloud de Confiance provider) 可讓您佈建及管理 Cloud de Confiance 基礎架構。

事前準備

  1. 如要使用已設定 gcloud CLI 和 Terraform 的線上終端機,請啟用 Cloud Shell:

    頁面底部會開啟 Cloud Shell 工作階段,並顯示指令列提示。工作階段可能要幾秒鐘的時間才能初始化。

  2. 建立或選取 Cloud de Confiance 專案

    選取或建立專案所需的角色

    • 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
    • 建立專案:如要建立專案,您需要具備專案建立者角色 (roles/resourcemanager.projectCreator),其中包含 resourcemanager.projects.create 權限。瞭解如何授予角色
    • 建立 Cloud de Confiance 專案:

      gcloud projects create PROJECT_ID

      PROJECT_ID 替換為您要建立的 Cloud de Confiance 專案名稱。

    • 選取您建立的 Cloud de Confiance 專案:

      gcloud config set project PROJECT_ID

      PROJECT_ID 替換為 Cloud de Confiance 專案名稱。

  3. 確認專案已啟用計費功能 Cloud de Confiance

  4. 啟用 Compute Engine API:

    啟用 API 時所需的角色

    如要啟用 API,您需要具備服務使用情形管理員 IAM 角色 (roles/serviceusage.serviceUsageAdmin),其中包含 serviceusage.services.enable 權限。瞭解如何授予角色

    gcloud services enable compute.googleapis.com
  5. 將角色授予使用者帳戶。針對下列每個 IAM 角色,執行一次下列指令: roles/compute.instanceAdmin.v1

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    更改下列內容:

準備環境

  1. 複製包含 Terraform 範例的 GitHub 存放區:

    git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branch
    
  2. 前往包含快速入門範例的目錄:

    cd terraform-docs-samples/compute/quickstart/create_vm
    

查看 Terraform 檔案

檢查 main.tf 檔案。這個檔案定義了您要建立的資源。 Cloud de Confiance

cat main.tf

輸出內容類似如下

resource "google_compute_instance" "default" {
  name         = "my-vm"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "ubuntu-minimal-2210-kinetic-amd64-v20230126"
    }
  }

  network_interface {
    network = "default"
    access_config {}
  }
}

這個檔案說明 google_compute_instance 資源,也就是 Compute Engine VM 執行個體的 Terraform 資源。google_compute_instance 設定為具有下列屬性:

  • name 設為 my-vm
  • machine_type 設為 n1-standard-1
  • zone 設為 us-central1-a
  • boot_disk 會設定執行個體的開機磁碟。
  • network_interface 設為使用Cloud de Confiance 專案中的預設網路。

建立 Compute Engine VM 執行個體

  1. 在 Cloud Shell 中執行下列指令,確認 Terraform 可供使用:

    terraform
    

    畫面會顯示如下的輸出內容:

    
    Usage: terraform [global options] <subcommand> [args]
    
    The available commands for execution are listed below.
    The primary workflow commands are given first, followed by
    less common or more advanced commands.
    
    Main commands:
      init          Prepare your working directory for other commands
      validate      Check whether the configuration is valid
      plan          Show changes required by the current configuration
      apply         Create or update infrastructure
      destroy       Destroy previously-created infrastructure
    
    
  2. 執行下列指令來初始化 Terraform。這項指令會準備工作區,讓 Terraform 可以套用設定。

    terraform init
    

    畫面會顯示如下的輸出內容:

    
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of hashicorp/google...
    - Installing hashicorp/google v5.35.0...
    - Installed hashicorp/google v5.35.0 (signed by HashiCorp)
    
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    
    Terraform has been successfully initialized!
    
    
  3. 執行下列指令,驗證 Terraform 設定。 這項指令會執行下列動作:

    • 確認 main.tf 的語法正確無誤。
    • 顯示即將建立的資源預覽畫面。
    terraform plan
    

    畫面會顯示如下的輸出內容:

    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Note: You didn't use the -out option to save this plan, so Terraform can't
    guarantee to take exactly these actions if you run "terraform apply" now.
    
  4. 套用設定,佈建 main.tf 檔案中說明的資源:

    terraform apply
    

    系統顯示提示訊息時,請輸入 yes

    Terraform 會呼叫 Cloud de Confiance API,建立 main.tf 檔案中定義的 VM 執行個體。

    畫面會顯示如下的輸出內容:

    Apply complete! Resources: 1 added, 0 changed, 0 destroyed
    

連線至 VM 執行個體

執行下列指令,連線至您剛建立的 VM 執行個體:

gcloud compute ssh --zone=us-central1-a my-vm

清除所用資源

為了避免系統向您的 Cloud de Confiance 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Cloud de Confiance 專案。

在 Cloud Shell 中執行下列指令,刪除 Terraform 資源:

terraform destroy

系統顯示提示訊息時,請輸入 yes

畫面會顯示如下的輸出內容:

Destroy complete! Resources: 1 destroyed.

後續步驟