將 Google Cloud 資源匯出為 Terraform 格式

您已在 Google Cloud中部署資源,現在需要使用 Terraform 管理基礎架構即程式碼 (IaC)。Google 提供的工具可用於為專案、資料夾或機構中的資源產生 Terraform 程式碼。

事前準備

  • 準備 Cloud Shell。

    啟動 Cloud Shell,並設定預設 Google Cloud 專案,以便為已部署的資源產生 Terraform 程式碼。

    您只需為每個專案執行這個指令一次,而且可以在任何目錄中執行。

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    如果您在 Terraform 設定檔中設定明確的值,系統會覆寫環境變數。

  • 在 Cloud Shell 中安裝 Config Connector 的指令列介面 (CLI)。

    gcloud components install config-connector
    

    設定連接器可讓您使用 Google Cloud的 Terraform 大量匯出工具。

    如果看到 ERROR: (gcloud.components.install) You cannot perform this action because the Google Cloud CLI component manager is disabled for this installation,請改為執行下列指令:

    sudo apt-get install google-cloud-sdk-config-connector
    
  • 啟用 Cloud Asset API。

    gcloud services enable cloudasset.googleapis.com
    
  • 建立要用於匯出作業的服務帳戶:

    gcloud beta services identity create --service=cloudasset.googleapis.com
    
  • 確認 Cloud Asset Service Agent (gcp-sa-cloudasset.iam.gserviceaccount.com) 具備 roles/servicenetworking.serviceAgent 角色:

    gcloud projects add-iam-policy-binding PROJECT_ID \
      --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-cloudasset.iam.gserviceaccount.com \
      --role=roles/servicenetworking.serviceAgent
    
  • 確認 Cloud Asset Service Agent (gcp-sa-cloudasset.iam.gserviceaccount.com) 具備 roles/storage.objectAdmin 角色:

    gcloud projects add-iam-policy-binding PROJECT_ID \
      --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-cloudasset.iam.gserviceaccount.com \
      --role=roles/storage.objectAdmin
    

限制

雖然 Terraform Google 供應商支援部分資源類型,但這些類型的資源無法匯出為 Terraform 格式。如需匯出至 Terraform 格式時支援的資源類型清單,請執行 gcloud beta resource-config list-resource-types 指令。

將整個專案設定匯出為 Terraform HCL 程式碼

gcloud beta resource-config bulk-export --resource-format=terraform 指令會匯出專案、資料夾或機構中設定的資源,並以 HCL 程式碼格式在畫面上列印這些資源。

gcloud beta resource-config bulk-export \
  --project=PROJECT_ID \
  --resource-format=terraform

將輸出內容寫入目錄結構

  1. 如果您尚未建立目錄,請建立要輸出專案設定的目錄:

    mkdir OUTPUT_DIRECTORY
    
  2. 將專案的整個設定匯出至目錄:

    gcloud beta resource-config bulk-export \
     --path=OUTPUT_DIRECTORY \
     --project=PROJECT_ID \
     --resource-format=terraform
    

    --path 標記會指定輸出 HCL 程式碼的位置。

執行指令後,每個資源的 HCL 程式碼會輸出至以下目錄結構中的個別 .tf 檔案:

OUTPUT_DIRECTORY/projects/PROJECT_ID/RESOURCE_TYPE

將輸出內容寫入單一檔案

如果您不想將輸出內容列印到螢幕上,或建立個別的 .tf 檔案,可以將所有輸出內容寫入單一檔案,如以下範例所示:

gcloud beta resource-config bulk-export \
  --resource-format=terraform \
  --project=PROJECT_ID \
  >> gcp_resources.tf

篩選輸出內容

指定資源類型,篩選大量匯出指令的輸出內容。

列出要篩選的支援資源類型

如需支援匯出至 Terraform 格式的資源類型清單,請執行 gcloud beta resource-config list-resource-types 指令:

gcloud beta resource-config list-resource-types

您可以選擇將輸出內容寫入檔案:

gcloud beta resource-config list-resource-types >> strings.txt

在輸出內容中,Compute Engine VM 的資源類型會列為:

KRM KIND: ComputeInstance

您可以忽略 KRM KIND: 前置字元。

匯出單一資源類型

使用字串 (例如 ComputeInstance),以 HCL 程式碼格式匯出專案的特定資源類型:

gcloud beta resource-config bulk-export \
  --resource-types=RESOURCE_TYPE \
  --project=PROJECT_ID \
  --resource-format=terraform

--resource-types 旗標會指定要輸出的資源類型。

匯出多種資源類型

以 HCL 程式碼格式匯出 VM 執行個體和防火牆規則:

gcloud beta resource-config bulk-export \
  --resource-types=ComputeFirewall,ComputeInstance \
  --project=PROJECT_ID \
  --resource-format=terraform

使用檔案指定要匯出的資源類型

  1. 建立名為 tf-output 的目錄。

    cd && mkdir tf-output && cd tf-output
    
  2. 建立名為 types.txt 的檔案,並新增資源類型清單。例如:

    ComputeBackendBucket
    ComputeBackendService
    ComputeForwardingRule
    
  3. 執行加上 --resource-types-file 旗標的 gcloud beta resource-config bulk-export 指令:

    gcloud beta resource-config bulk-export \
     --resource-types-file=types.txt \
     --path=tf-output \
     --project=PROJECT_ID \
     --resource-format=terraform
    

如果專案不含任何特定資源類型,指令會成功執行,但不會針對該資源類型輸出任何內容。

疑難排解

如果您看到下列錯誤訊息:

「權限遭拒,無法匯出。請確認已啟用 Cloud Asset Inventory API。」

請確認您已按照「事前準備」一節中的指示操作。

後續步驟