為 Code Engine 設定 Terraform

IBM Cloud® 上的 Terraform 可讓您以可預測且一致的方式建立 IBM Cloud 服務,因此您可以依循 Infrastructure as Code ( IaC ) 原則,快速建立複雜的多層雲端環境。 與使用 IBM Cloud CLI 或 API 和 SDK 相似,您可以使用 HashiCorp Configuration Language (HCL) 自動建立、更新和刪除 Code Engine 工作負載。

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

安裝 Terraform 並配置 Code Engine 的資源

在您可以使用 Terraform 建立工作量之前,請確定您已完成下列必要條件。

  • 確保您擁有 所需的存取權限,才能建立和使用 Code Engine 資源。
  • 安裝 Terraform CLI 並配置 Terraform 的 IBM Cloud 提供者外掛程式。 如需相關資訊,請參閱 開始使用 Terraform on IBM Cloud® 的指導教學。 外掛程式會將用來完成此作業的 IBM Cloud API 抽象化。
  • 建立名為 main.tf 的 Terraform 配置檔案。 在此檔案中,您可以使用 HashiCorp 配置語言來定義資源。 如需相關資訊,請參閱 Terraform 文件
  1. 使用 main.tf 檔案中的 code_engine_project 資源引數來建立 Code Engine 專案。

    data "ibm_resource_group" "resource_group" {
       name = "<your_resource_group_name>"
    }
    
    resource "ibm_code_engine_project" "code_engine_project_instance" {
       name = "<your_project_name>"
       resource_group_id = data.ibm_resource_group.resource_group.id
    }
    
  2. 完成建置配置檔之後,請起始設定 Terraform CLI。 如需相關資訊,請參閱 起始設定工作目錄

    terraform init
    
  3. main.tf 檔案供應專案。 如需相關資訊,請參閱 使用 Terraform 佈建基礎架構

    1. 執行 terraform plan 以產生 Terraform 執行計劃來預覽提出的動作。

      terraform plan
      
      data.ibm_resource_group.resource_group: Reading...
      data.ibm_resource_group.resource_group: Read complete after 1s [id=<your_resource_group_id>]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
         + create
      
      Terraform will perform the following actions:
      
         # ibm_code_engine_project.code_engine_project_instance will be created
         + resource "ibm_code_engine_project" "code_engine_project_instance" {
            + account_id        = (known after apply)
            + created_at        = (known after apply)
            + crn               = (known after apply)
            + href              = (known after apply)
            + id                = (known after apply)
            + name              = "<your_project_name>"
            + project_id        = (known after apply)
            + region            = (known after apply)
            + resource_group_id = "<your_resource_group_id>"
            + resource_type     = (known after apply)
            + status            = (known after apply)
         }
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
    2. 執行 terraform apply,以建立計劃中定義的資源。

      terraform apply
      
      data.ibm_resource_group.resource_group: Reading...
      data.ibm_resource_group.resource_group: Read complete after 1s [id=<your_resource_group_id>]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
         + create
      
      Terraform will perform the following actions:
      
         # ibm_code_engine_project.code_engine_project_instance will be created
         + resource "ibm_code_engine_project" "code_engine_project_instance" {
            + account_id        = (known after apply)
            + created_at        = (known after apply)
            + crn               = (known after apply)
            + href              = (known after apply)
            + id                = (known after apply)
            + name              = "my-project"
            + project_id        = (known after apply)
            + region            = (known after apply)
            + resource_group_id = "5218b5c8c749454b9fc8be28f32e458e"
            + resource_type     = (known after apply)
            + status            = (known after apply)
         }
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
         + ibm_code_engine_project = {
            + account_id        = (known after apply)
            + created_at        = (known after apply)
            + crn               = (known after apply)
            + href              = (known after apply)
            + id                = (known after apply)
            + name              = "my-project"
            + project_id        = (known after apply)
            + region            = (known after apply)
            + resource_group_id = "5218b5c8c749454b9fc8be28f32e458e"
            + resource_type     = (known after apply)
            + status            = (known after apply)
            + timeouts          = null
         }
      
      Do you want to perform these actions?
         Terraform will perform the actions described above.
         Only 'yes' will be accepted to approve.
      
         Enter a value: yes
      
      ibm_code_engine_project.code_engine_project_instance: Creating...
      ibm_code_engine_project.code_engine_project_instance: Still creating... [10s elapsed]
      ibm_code_engine_project.code_engine_project_instance: Still creating... [20s elapsed]
      ibm_code_engine_project.code_engine_project_instance: Still creating... [30s elapsed]
      ibm_code_engine_project.code_engine_project_instance: Creation complete after 30s [id=13468527-84sa-619g-gr99-168fe364w98f]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      ibm_code_engine_project = {
         "account_id" = "<your_account_id>"
         "created_at" = "2023-03-16T10:44:24.150838188Z"
         "crn" = "crn:v1:bluemix:public:codeengine:eu-de:a/dc5e6392e5d14021846031b83e95f190:13468527-84sa-619g-gr99-168fe364w98f::"
         "href" = "https://api.eu-de.codeengine.cloud.ibm.com/v2/projects/13468527-84sa-619g-gr99-168fe364w98f"
         "id" = "13468527-84sa-619g-gr99-168fe364w98f"
         "name" = "my-project"
         "project_id" = "<your_project_id>"
         "region" = "eu-de"
         "resource_group_id" = "<your_resource_group_id>"
         "resource_type" = "project_v2"
         "status" = "active"
         "timeouts" = null /* object */
      }
      
  4. 您可以在 Code Engine 主控台的專案清單中看到您的專案。 或者,您可以在 IBM Cloud 資源清單中查看您的專案,方法是展開種類 Containers 並尋找您的專案名稱。

下一步為何?

既然您已順利在 IBM Cloud上使用 Terraform 建立第一個 Code Engine 專案,您可以建立其他資源。

資源

資料來源

如需 IBM Cloud 和 Terraform 的相關資訊,請參閱 IBM Cloud 提供者