使用 Terraform 來配置 Schematics

IBM Cloud® 上的 Terraform 可根據 Infrastructure as Code ( IaC ) 原則,以可預測且一致的方式提供 IBM Cloud 服務,以快速建立複雜的多層雲端環境。 同樣地,您可以使用 Terraform 自動配置、更新和刪除 IBM Cloud Schematics 工作區和動作實例。

正在尋找受管理的 Terraform on IBM Cloud 解決方案? 嘗試 IBM Cloud Schematics. 有了 Schematics,您可以使用熟悉的 Terraform 語言,但不必擔心設定和維護 Terraform 命令列和 IBM Cloud Provider 外掛。Schematics 也提供預定義的 Terraform 模版,您可以從 IBM Cloud 目錄中安裝。

開始之前,請確定您具有 必要存取權,可建立及使用 IBM Cloud Schematics 工作區。

範例-使用 Terraform 建立 Schematics 工作區

完成下列步驟,以使用 Terraform 建立 Schematics 工作區:

  1. 遵循 Terraform on IBM Cloud 入門指導教學,以安裝 Terraform CLI 並配置 IBM Cloud Provider Plug-in for Terraform。 外掛程式會摘要用來佈建、更新或刪除 Schematics 資源的 IBM Cloud APIs

  2. 建立名為 main.tfterraform.tfvarsversions.tf 的 Terraform 配置檔。

    您可以使用 Terraform 示例 Git URL https://github.com/IBM-Cloud/terraform-provider-ibm/tree/master/examples/ibm-api-gateway。 本範例使用服務實例為您選擇的 IBM Cloud 服務設定 API。 您可以指定要用來存取服務的 API 端點,並定義訂閱金鑰,以便安全地使用 API。

    如果您在 us 以外的地區中建立了其他工作區,則必須將 API 端點設為該地區。 例如,如果您指定的地區是 eu,則 API 端點應該在環境變數中指定為 IBMCLOUD_SCHEMATICS_API_ENDPOINT=https://eu.schematics.cloud.ibm.com。 有關 Schematics 工作區位置和要使用的端點的詳細資訊,請參閱 我的資訊儲存於何處?

    然後在您地區的 default 資源群組中建立 Schematics 工作區 tf-testwks-apigwy。 此工作區指向您選擇需要 Terraform terraform_v1.5 版的 Terraform 範本。

    versions.tf

    versions.tf 範例檔,用來指定您建立工作區所需的提供者版本。

      terraform {
      required_version = ">=1.0.0, <2.0"
        required_providers {
          ibm = {
            source = "IBM-Cloud/ibm"
          }
        }
      }
    

    terraform.tfvars

    用於儲存機密性資訊 (例如認證) 的範例 terraform.tfvars 檔案。 如需相關資訊,請參閱 terraform.tfvars 檔案參照認證。 若要建立 API 金鑰,請參閱 建立及 API 金鑰

    schematics_workspace_name="tf-testwks-apigwy"
    schematics_workspace_description="Sample workspace created with terraform with URL"
    schematics_workspace_type="terraform_v1.5"
    schematics_workspace_location="us-south"
    schematics_workspace_resource_group="default"
    ibmcloud_api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    

    main.tf

    檢閱下列範例 main.tf 檔案。 該檔案調用變數 terraform.tfvars 使用 Git URL 建立文件,然後使用您的 { IBM Cloud API 金鑰建立一個 Schematics 工作區。

    variable "schematics_workspace_name" {}
    variable "schematics_workspace_description" {}
    variable "schematics_workspace_type" {}
    variable "schematics_workspace_location" {}
    variable "schematics_workspace_resource_group" {}
    variable "ibmcloud_api_key" {}
    
    resource "ibm_schematics_workspace" "schematics_workspace_instance" {
    name = var.schematics_workspace_name
    description = var.schematics_workspace_description
    location = var.schematics_workspace_location
    resource_group = var.schematics_workspace_resource_group
    tags = ["sample"]
    template_env_settings = [
        { env1 = "val1" },
        { env2 = "val2" }
    ]
    template_type= var.schematics_workspace_type
    template_git_url = "https://github.com/IBM-Cloud/terraform-provider-ibm/tree/master/examples/ibm-api-gateway"
    }
    
    provider "ibm" {
    region           = "us-south"
    ibmcloud_api_key = var.ibmcloud_api_key
    }
    
    

    下表列出使用 Terraform 建立及起始設定服務實例時支援的參數。 如需建立工作區之詳細參數的相關資訊,請參閱 ibm_schematics_workspace 資源。

    使用 Terraform 建立 Schematics 工作區時支援的參數。
    參數 說明
    description 工作區的說明。
    location 您要建立 Schematics 工作區並執行 Schematics 動作的位置。
    resource_group 您要在其中佈建工作區的資源群組 ID。
    name 工作區的名稱。 名稱的長度最多可以為 128 個字元,而且可以包括英數字元、空格、橫線和底線。 當您為自己的 Terraform 範本建立工作區時,請考慮包含您與 Terraform 範本一起建立的微服務元件,以及您要以您的名義其中部署資源的 IBM Cloud 環境。
    tags 與工作區相關聯的標籤清單。
    template_env_settings 您要在執行 Terraform 動作期間套用的環境變數清單。
    template_git_url Git 儲存庫 URL,您可在此取得配置資源的詳細資訊。
    template_type 在 Schematics 工作區中指定您要套用的 Terraform 版本。
  3. 起始設定 Terraform CLI。

    terraform init
    

    如果未設定 Terraform 的環境變數路徑,您可能會看到 command not found: terraform 錯誤。 透過設定 Terraform 安裝目錄 的路徑來修正錯誤。

  4. 建立 Terraform 執行計劃。 Terraform 執行計劃彙總需要執行以在您帳戶中建立 Schematics 工作區的所有動作。

    terraform plan
    
  5. 在 IBM Cloud中建立 Schematics 工作區實例及 IAM 存取原則。

    terraform apply
    

    如需更多有關 terraform apply 指令錯誤的疑難排解資訊,請參閱 找出 Schematics apply 失敗的根本原因

  6. Schematics 儀表板,檢查是否已建立 tf-testwks-apigwy 工作區。 並且會從 IBM Cloud Schematics 資源清單佈建資源。

  7. 驗證已順利指派存取原則。 如需相關資訊,請參閱 在主控台中檢閱指派的存取權

下一步為何?

既然您已順利在 IBM Cloud上使用 Terraform 建立第一個 Schematics 工作區,您可以選擇下列作業: