建立 Terraform 設定

為貴機構收錄 Service Catalog 解決方案時,您可以建立 Terraform 設定或設定,供使用者使用 Terraform 部署。建立設定後,您可以將設定指派給目錄,與使用者共用。

在本指南中,您將使用 Cloud Build 在最新版的 Terraform Docker 映像檔中執行 Terraform 指令,並使用 Cloud Storage 儲存及管理 Terraform 資源,例如模組和狀態檔案

如需使用 Terraform 的資源和指南,請參閱「使用 Terraform 與 Google Cloud」。

事前準備

  • 您必須具備下列 Identity and Access Management (IAM) 角色

    • 目錄管理員 目錄管理員 (針對與已啟用 Service Catalog 的 Google Cloud 專案相關聯的Google Cloud 組織)。如果您不具備這個角色,請與機構管理員聯絡,要求提供存取權。
    • 您要建立解決方案的 Google Cloud 專案的「儲存空間管理員」角色。如果 Terraform 設定位於其他專案,您必須具備包含 Terraform 設定的專案的「儲存空間管理員」角色。
    • 針對您要建立解決方案的 Google Cloud 專案,使用 Cloud Build 編輯器

    如果您打算使用自己的服務帳戶,則必須為包含您打算使用的服務帳戶的 Google Cloud 專案,設定服務帳戶使用者角色。

    如果您不具備這些角色,請向機構管理員要求存取權。

  • 如果您打算使用自己的服務帳戶,該服務帳戶必須具備下列 IAM 角色

    • 您要建立解決方案的 Google Cloud 專案的「儲存空間管理員」角色。如果 Terraform 設定位於其他專案中,服務帳戶也必須具備含有 Terraform 設定的專案的 Storage 物件檢視者角色。
    • 針對您要建立解決方案的Google Cloud 專案,使用「Logs Writer」

    如果您不具備這些角色,請向機構管理員要求存取權。

  • 在建立設定檔的專案中設定 Cloud Build:

    1. Enable the Cloud Build API.

      Enable the API

      啟用 API 時,系統會自動建立 Cloud Build 服務帳戶,供您在後續步驟中使用。
    2. 前往「Cloud Build」頁面。
    3. 在左側面板中選取「設定」
    4. 使用「服務帳戶電子郵件」專區下方提供的服務帳戶。
    5. 如果您未使用自己的服務帳戶,請為服務帳戶授予下列 IAM 角色。如要瞭解如何授予角色,請參閱「設定 Cloud Build 服務帳戶的存取權」。

將設定檔上傳至 Cloud Storage

您使用 Cloud Storage 管理 Service Catalog 的 Terraform 設定檔。

建立 Cloud Storage 值區

如要設定 Cloud Storage,請在為貴機構啟用服務目錄的專案中建立值區:

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.

    Go to Buckets

  2. Click Create.
  3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
    1. In the Get started section, do the following:
      • Enter a globally unique name that meets the bucket naming requirements.
      • To add a bucket label, expand the Labels section (), click Add label, and specify a key and a value for your label.
    2. In the Choose where to store your data section, do the following:
      1. Select a Location type.
      2. Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
      3. To set up cross-bucket replication, select Add cross-bucket replication via Storage Transfer Service and follow these steps:

        Set up cross-bucket replication

        1. In the Bucket menu, select a bucket.
        2. In the Replication settings section, click Configure to configure settings for the replication job.

          The Configure cross-bucket replication pane appears.

          • To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
          • To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
          • Click Done.
    3. In the Choose how to store your data section, do the following:
      1. Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
      2. To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
    4. In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
    5. In the Choose how to protect object data section, do the following:
      • Select any of the options under Data protection that you want to set for your bucket.
        • To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
        • To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
        • To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
          • To enable Object Retention Lock, click the Enable object retention checkbox.
          • To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
      • To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
  4. Click Create.

啟用物件版本管理功能

您必須為值區使用物件版本管理功能,才能避免 Terraform 設定遭到刪除或覆寫。如要啟用物件版本管理功能,請參閱 Cloud Storage 的「使用物件版本管理」說明文件。

授予值區存取權

貴機構的使用者部署設定時,Cloud Build 服務帳戶必須具備值區的讀取權限。如果您的值區儲存在使用者部署設定的 Google Cloud 專案中,服務帳戶就已具備這項存取權。

如果使用者在其他Google Cloud 專案中部署設定,請使用下列任一方法授予值區存取權:

  • Storage 物件檢視者 (roles/storage.objectViewer) 角色授予使用者的 Cloud Build 服務帳戶,或是使用者部署 Terraform 設定的 Google Cloud 資源 (例如專案、資料夾或機構)。

  • 使用存取控制清單 (ACL) 管理儲存貯體的存取權。

如要進一步瞭解如何管理值區存取權,請參閱 Cloud Storage 的「存取權控管總覽」一文。

建立及上傳 Terraform 模組

設定 Cloud Storage 值區後,您必須建立並上傳 Terraform 模組,這是所有設定檔的容器。Service Catalog 會使用這個模組自動產生 JSON 結構定義檔,用來定義設定變數。

當您呼叫目前目錄以外的模組時,請使用遠端路徑,而非相對路徑。例如,使用 source = "GoogleCloudPlatform/cloud-run/google",而不使用 source = "../../"

如要瞭解常見 Google Cloud 用途的模組,請參閱 Terraform 設計圖和模組 Google Cloud

以下程式碼範例說明 Terraform 設定檔 main.tf


variable "machine_type" {
  type    = string
  default = "n1-standard-1"
}

variable "zone" {
  type    = string
  default = "us-central1-a"
}

variable "deployment_identifier" {
  description = "The unique name for your instance"
  type        = string
}

resource "google_compute_instance" "default" {
  name         = "vm-${var.deployment_identifier}"
  machine_type = var.machine_type
  zone         = var.zone

  boot_disk {
    device_name = "boot"
    auto_delete = true
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  network_interface {
    network = "default"
    access_config {
      // Ephemeral IP
    }
  }
}

建立模組後,請將檔案壓縮為 ZIP 檔案。請確認模組儲存在 ZIP 檔案的根目錄中。

為確保在壓縮 Terraform 檔案時正確儲存模組,請執行下列指令: none zip solution.zip file1.tf file2.tf file3.tf

然後將 ZIP 檔案上傳至值區。如要瞭解如何上傳 ZIP 檔案,請參閱 Cloud Storage 的上傳物件說明文件。

在 Service Catalog 中建立設定

使用 Terraform 模組設定 Cloud Storage 值區後,您可以建立包含值區的 Service Catalog 解決方案。

如要將 Terraform 設定建立為服務目錄解決方案,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Service Catalog Admin」Solutions 頁面
    前往「解決方案」頁面

  2. 如要選擇 Google Cloud 專案,請按一下「選取」

  3. 按一下「建立解決方案」。在下拉式清單中,選取「Create Terraform config」(建立 Terraform 設定)

  4. 輸入 Terraform 設定的名稱、說明和標語。標語是使用者瀏覽 Service Catalog 時看到的解決方案簡短說明。

  5. 在「Link to Terraform config」欄位中,提供 Cloud Storage 值區的連結,該值區包含 Terraform 模組的 ZIP 檔案,例如 gs://my-terraform-bucket/my-zip-file.zip

  6. 您可以選擇上傳解決方案的圖示。建議的圖示尺寸為 80 x 80 像素。

  7. 視需要輸入創作者的支援連結和聯絡資訊。

  8. 視需要新增解決方案的說明文件連結。

  9. 選取要用於部署解決方案的 Terraform 版本。

  10. 您可以選擇提供自己的服務帳戶,方法是按一下「選取或輸入您的服務帳戶」旁的核取方塊。如果服務帳戶位於您建立解決方案的 Google Cloud 專案中,請選取「從目前的專案選取服務帳戶」,然後從下拉式選單中選取服務帳戶。如果您的服務帳戶位於與解決方案不同的 Google Cloud 專案中,請選取「Enter a service account email from any project」(輸入任意專案中服務帳戶的電子郵件地址),然後輸入服務帳戶的電子郵件地址。

  11. 點選「建立」。

解決方案會建立並顯示在 Service Catalog 管理員的「Solutions」頁面中。

下圖說明如何建立 Terraform 設定:

建立 Terraform 設定

後續步驟