將 Terraform 狀態儲存在 Cloud Storage 值區中


在本教學課程中,您將瞭解如何將 Terraform 狀態儲存在 Cloud Storage 值區中。

根據預設,Terraform 會將狀態儲存在名為 terraform.tfstate 的本機檔案中。如果多位使用者同時執行 Terraform,且每部機器對目前的基礎架構都有自己的理解,這個預設設定會使得團隊難以使用 Terraform。

為協助您避免這類問題,本頁面說明如何設定指向 Cloud Storage 值區的遠端狀態。遠端狀態是 Terraform 後端的一項功能。

目標

本教學課程將說明如何執行下列操作:

  • 使用 Terraform 佈建 Cloud Storage bucket,以儲存 Terraform 狀態。
  • 在 Terraform 設定檔中新增範本,將狀態從本機後端遷移至 Cloud Storage bucket。

費用

在本文件中,您會使用 Google Cloud的下列計費元件:

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。

Cloud Storage 會收取儲存、讀取和寫入作業、網路輸出和複製的費用。

本教學課程中的 Cloud Storage 值區已啟用物件版本管理功能,可保留部署記錄。不過,啟用這項功能會增加儲存空間成本;您可以設定物件生命週期管理將舊的狀態版本刪除,藉此降低相關費用的支出。

事前準備

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    Cloud Shell 已預先安裝 Terraform。

  2. 如果您使用本機殼層,請執行下列步驟:

  3. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  4. Make sure that billing is enabled for your Google Cloud project.

  5. Enable the Cloud Storage API:

    gcloud services enable storage.googleapis.com
  6. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/storage.admin

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

    或者,您也可以建立包含下列權限的自訂 IAM 角色

    • storage.buckets.create
    • storage.buckets.list
    • storage.objects.get
    • storage.objects.create
    • storage.objects.delete
    • storage.objects.update

    建議您最好控管值區和儲存在其中的狀態檔案存取權。只有少數使用者 (例如主要雲端管理員,以及擔任替代或備份管理員的人員) 應具備值區的管理員權限。其他開發人員應僅具備在值區中寫入及讀取物件的權限。

準備環境

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

    git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branch
    
  2. 變更為工作目錄:

    cd terraform-docs-samples/storage/remote_terraform_backend_template
    

查看 Terraform 檔案

  1. 查看 main.tf 檔案:

    cat main.tf
    

    輸出內容類似如下

    resource "random_id" "default" {
      byte_length = 8
    }
    
    resource "google_storage_bucket" "default" {
      name     = "${random_id.default.hex}-terraform-remote-backend"
      location = "US"
    
      force_destroy               = false
      public_access_prevention    = "enforced"
      uniform_bucket_level_access = true
    
      versioning {
        enabled = true
      }
    }
    
    resource "local_file" "default" {
      file_permission = "0644"
      filename        = "${path.module}/backend.tf"
    
      # You can store the template in a file and use the templatefile function for
      # more modularity, if you prefer, instead of storing the template inline as
      # we do here.
      content = <<-EOT
      terraform {
        backend "gcs" {
          bucket = "${google_storage_bucket.default.name}"
        }
      }
      EOT
    }

    這個檔案說明下列資源:

    • random_id:附加至 Cloud Storage bucket 名稱,確保 Cloud Storage bucket 名稱不重複。
    • google_storage_bucket:用於儲存狀態檔案的 Cloud Storage 值區。這個 bucket 的設定如下:
      • force_destroy 設為 false,確保值區中如有物件,就不會遭到刪除。這樣才能確保值區中的狀態資訊不會意外遭到刪除。
      • public_access_prevention 設為 enforced,確保儲存空間內容不會意外公開。
      • uniform_bucket_level_access 設為 true,即可使用 IAM 權限 (而非存取控制清單) 控管值區和內容的存取權
      • versioning,確保值區中保留較舊的狀態版本。
    • local_file:本機檔案。這個檔案的內容會指示 Terraform 在建立 bucket 後,使用 Cloud Storage bucket 做為遠端後端。

佈建 Cloud Storage bucket

  1. 初始化 Terraform:

    terraform init
    

    首次執行 terraform init 時,您在 main.tf 檔案中指定的 Cloud Storage bucket 尚不存在,因此 Terraform 會初始化本機後端,將狀態儲存在本機檔案系統中。

  2. 套用設定,佈建 main.tf 檔案中說明的資源:

    terraform apply
    

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

    首次執行 terraform apply 時,Terraform 會佈建 Cloud Storage 值區,用於儲存狀態。此外,這個指令也會建立本機檔案;這個檔案的內容會指示 Terraform 使用 Cloud Storage 值區做為遠端後端,以儲存狀態。

將狀態遷移至 Cloud Storage 值區

  1. 將 Terraform 狀態遷移至遠端 Cloud Storage 後端:

    terraform init -migrate-state
    

    Terraform 會偵測到您已在本機擁有狀態檔案,並提示您將狀態遷移至新的 Cloud Storage 值區。系統顯示提示訊息時,請輸入 yes

執行這項指令後,Terraform 狀態會儲存在 Cloud Storage bucket 中。Terraform 會先從這個 bucket 提取最新狀態,再執行指令,並在執行指令後將最新狀態推送至 bucket。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

刪除專案

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

  1. 開啟 main.tf 檔案。

  2. google_storage_bucket.default 資源中,將 force_destroy 的值更新為 true

  3. 套用更新後的設定:

    terraform apply
    

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

  4. 刪除狀態檔案:

    rm backend.tf
    
  5. 將後端重新設定為本機:

    terraform init -migrate-state
    

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

  6. 執行下列指令,刪除 Terraform 資源:

    terraform destroy
    

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

後續步驟