共用方式為


Databricks Terraform 供應商

HashiCorp Terraform 是一種熱門的開放原始碼工具,可跨數個雲端提供者建立安全且可預測的雲端基礎結構。 您可以使用 Databricks Terraform 提供者,使用彈性且功能強大的工具來管理 Azure Databricks 工作區和相關聯的雲端基礎結構。 Databricks Terraform 提供者的目標是支援所有 Databricks REST API,支援部署和管理資料平台最複雜層面的自動化。 Databricks 客戶會使用 Databricks Terraform 提供者來部署和管理叢集和工作,以及設定資料存取。 您可以使用 Azure 提供者 來佈建 Azure Databricks 工作區。

開始使用

在本節中,您會在本機開發機器上安裝和設定使用 Terraform 和 Databricks Terraform 提供者的需求。 接著,您可以設定 Terraform 驗證。 在本節之後,本文提供範例組態,您可以嘗試佈建 Azure Databricks 筆記本、叢集,以及可在現有 Azure Databricks 工作區中的叢集上執行筆記本的工作。

需求

  1. 您必須擁有 Terraform CLI。 請參閱 Terraform 網站上的下載 Terraform

  2. 您必須有 Terraform 專案。 在您的終端機中,建立空白目錄,然後切換至該目錄。 (每個個別的 Terraform 組態檔都必須位於自己的目錄中,稱為 Terraform 專案。例如:mkdir terraform_demo && cd terraform_demo

    mkdir terraform_demo && cd terraform_demo
    

    在 Terraform 專案中的一或多個設定檔中包含專案的 Terraform 組態。 如需設定檔語法的相關資訊,請參閱 Terraform 網站上的 Terraform 語言文件

  3. 您必須將 Databricks Terraform 提供者的相依性新增至 Terraform 專案。 將下列內容新增至 Terraform 專案中的其中一個設定檔:

    terraform {
      required_providers {
        databricks = {
          source = "databricks/databricks"
        }
      }
    }
    
  4. 您必須設定 Terraform 專案的驗證。 請參閱 Databricks Terraform 提供者文件中的驗證

範例設定

本節提供範例設定,您可以嘗試在現有的 Azure Databricks 工作區中佈建 Azure Databricks 筆記本、叢集和在叢集上執行筆記本的工作。 它假設您已設定 需求,以及建立 Terraform 專案,並使用 Terraform 驗證設定專案,如上一節所述。

  1. 在您的 Terraform 專案中建立名為 me.tf 的檔案,並新增下列程式碼。 此檔案會取得目前使用者 (您) 的相關資訊:

    # Retrieve information about the current user.
    data "databricks_current_user" "me" {}
    
  2. 建立另一個名為 notebook.tf 的 HTML 檔案,並新增下列程式碼。 此檔案代表筆記本。

    variable "notebook_subdirectory" {
      description = "A name for the subdirectory to store the notebook."
      type        = string
      default     = "Terraform"
    }
    
    variable "notebook_filename" {
      description = "The notebook's filename."
      type        = string
    }
    
    variable "notebook_language" {
      description = "The language of the notebook."
      type        = string
    }
    
    resource "databricks_notebook" "this" {
      path     = "${data.databricks_current_user.me.home}/${var.notebook_subdirectory}/${var.notebook_filename}"
      language = var.notebook_language
      source   = "./${var.notebook_filename}"
    }
    
    output "notebook_url" {
     value = databricks_notebook.this.url
    }
    
  3. 建立另一個名為 notebook.auto.tfvars 的 HTML 檔案,並新增下列程式碼。 此檔案會指定筆記本的屬性。

    notebook_subdirectory = "Terraform"
    notebook_filename     = "notebook-getting-started.py"
    notebook_language     = "PYTHON"
    
  4. 建立另一個名為 notebook-getting-started.py 的 HTML 檔案,並新增下列程式碼。 此檔案代表筆記本的內容。

    display(spark.range(10))
    
  5. 建立另一個名為 cluster.tf 的 HTML 檔案,並新增下列程式碼。 此檔案代表叢集。

    variable "cluster_name" {
      description = "A name for the cluster."
      type        = string
      default     = "My Cluster"
    }
    
    variable "cluster_autotermination_minutes" {
      description = "How many minutes before automatically terminating due to inactivity."
      type        = number
      default     = 60
    }
    
    variable "cluster_num_workers" {
      description = "The number of workers."
      type        = number
      default     = 1
    }
    
    # Create the cluster with the "smallest" amount
    # of resources allowed.
    data "databricks_node_type" "smallest" {
      local_disk = true
    }
    
    # Use the latest Databricks Runtime
    # Long Term Support (LTS) version.
    data "databricks_spark_version" "latest_lts" {
      long_term_support = true
    }
    
    resource "databricks_cluster" "this" {
      cluster_name            = var.cluster_name
      node_type_id            = data.databricks_node_type.smallest.id
      spark_version           = data.databricks_spark_version.latest_lts.id
      autotermination_minutes = var.cluster_autotermination_minutes
      num_workers             = var.cluster_num_workers
    }
    
    output "cluster_url" {
     value = databricks_cluster.this.url
    }
    
  6. 建立另一個名為 cluster.auto.tfvars 的 HTML 檔案,並新增下列程式碼。 此檔案會指定叢集的屬性。

    cluster_name                    = "My Cluster"
    cluster_autotermination_minutes = 60
    cluster_num_workers             = 1
    
  7. 建立另一個名為 job.tf 的 HTML 檔案,並新增下列程式碼。 此檔案代表在叢集上執行筆記本的工作。

    variable "job_name" {
      description = "A name for the job."
      type        = string
      default     = "My Job"
    }
    
    variable "task_key" {
      description = "A name for the task."
      type        = string
      default     = "my_task"
    }
    
    resource "databricks_job" "this" {
      name = var.job_name
      task {
        task_key = var.task_key
        existing_cluster_id = databricks_cluster.this.cluster_id
        notebook_task {
          notebook_path = databricks_notebook.this.path
        }
      }
      email_notifications {
        on_success = [ data.databricks_current_user.me.user_name ]
        on_failure = [ data.databricks_current_user.me.user_name ]
      }
    }
    
    output "job_url" {
      value = databricks_job.this.url
    }
    
  8. 建立另一個名為 job.auto.tfvars 的 HTML 檔案,並新增下列程式碼。 此檔案會指定工作的屬性。

    job_name = "My Job"
    task_key = "my_task"
    
  9. 執行 terraform plan。 如果有任何錯誤,請加以修正,然後再次執行命令。

  10. 執行 terraform apply

  11. 驗證建立的筆記本、叢集和工作:在 terraform apply 命令的輸出中,尋找 notebook_urlcluster_urljob_url 的網址,然後移至它們。

  12. 執行工作:在 [工作] 頁面上,按下 [立即執行]。 工作完成後,請檢查您的電子郵件收件匣。

  13. 當您完成此範例時,請執行 terraform destroy 來從 Azure Databricks 工作區刪除筆記本、叢集和工作。

    注意

    如需 terraform planterraform applyterraform destroy 命令的詳細資訊,請參閱 Terraform 文件中的 Terraform CLI 文件

  14. 確認筆記本、叢集和作業已刪除:請重新整理筆記本、叢集和 作業 頁面,以確保每個頁面顯示資源無法找到的訊息。

測試

在您部署 Terraform 組態之前或之後進行測試。 部署資源之前,您可以執行類似單元測試的測試。 您也可以在部署資源之後執行類似整合測試的測試。 請參閱 Terraform 文件中的測試

遵循此程序,針對本文的範例設定執行類似整合測試的測試:

  1. 建立名為 cluster.tftest.hcl 的檔案,並新增下列程式碼。 此檔案會測試已部署的叢集是否具有預期的叢集名稱。

    # Filename: cluster.tftest.hcl
    
    run "cluster_name_test" {
      command = apply
    
      assert {
        condition     = databricks_cluster.this.cluster_name == var.cluster_name
        error_message = "Cluster name did not match expected name"
      }
    }
    
  2. 建立名為 job.tftest.hcl 的檔案,並新增下列程式碼。 此檔案會測試已部署的工作是否具有預期的工作名稱。

    run "job_name_test" {
      command = apply
    
      assert {
        condition     = databricks_job.this.name == var.job_name
        error_message = "Job name did not match expected name"
      }
    }
    
  3. 建立名為 notebook.tftest.hcl 的檔案,並新增下列程式碼。 此檔案會測試已部署的筆記本是否具有預期的工作區路徑。

    run "notebook_path_test" {
      command = apply
    
      assert {
        condition     = databricks_notebook.this.path == "${data.databricks_current_user.me.home}/${var.notebook_subdirectory}/${var.notebook_filename}"
        error_message = "Notebook path did not match expected path"
      }
    }
    
  4. 執行 terraform test。 Terraform 會將每個資源部署至 Azure Databricks 工作區、執行每個相關的測試並報告其測試結果,然後卸載已部署的資源。

使用下列程序,針對本文的範例設定執行類似單元測試的測試:

  • 將上述每個測試中的行 command = apply 變更為 command = plan,然後執行 terraform test。 Terraform 會執行每個相關的測試,並報告其測試結果,但不會部署任何資源。
  • 模擬 Databricks Terraform 驅動程式,允許您在不部署資源且無需任何驗證憑證的情況下執行 terraform test。 請參閱 Terraform 文件中的模擬。 若要執行模擬測試,其中一種方法是將行 mock_provider "databricks" {} 新增至您的測試,並移除行 command = applycommand = plan,例如:
# Filename: cluster.tftest.hcl

mock_provider "databricks" {}

run "cluster_mock_name_test" {
  assert {
    condition     = databricks_cluster.this.cluster_name == var.cluster_name
    error_message = "Cluster name did not match expected name"
  }
}
# Filename: job.tftest.hcl

mock_provider "databricks" {}

run "job_mock_name_test" {
  assert {
    condition     = databricks_job.this.name == var.job_name
    error_message = "Job name did not match expected name"
  }
}
# Filename: notebook.tftest.hcl

mock_provider "databricks" {}

run "notebook_mock_path_test" {
  assert {
    condition     = databricks_notebook.this.path == "${data.databricks_current_user.me.home}/${var.notebook_subdirectory}/${var.notebook_filename}"
    error_message = "Notebook path did not match expected path"
  }
}

下一步

其他資源