使用 Terraform 部署全域作用中表格

使用 Terraform 輕鬆在 OCI 上部署全域作用中表格。

Terraform 是一種基礎架構即程式碼工具,可讓您安全且有效率地建置、變更及版本雲端和 onPremises 資源。若要對 Terraform 有基本的瞭解,請閱讀簡短的簡介。確定您已安裝 OCI Terraform 提供者並設定

部署全域表格的步驟

下面的 terraform 命令檔會執行下列作業:

  • 如果來源表格不存在,請將它建立為單一表格。
  • 將索引新增到單一表格。
  • 建立表格的區域複本,並使其成為「全域作用中」表格。

步驟:

步驟 1:建立「全域作用中」表格的 Terraform 組態檔:您必須建立下列組態檔來建立「全域作用中」表格。

provider.tf:

在此檔案中,您可以提供 OCI Terraform 提供者定義以及關聯的變數定義。OCI Terraform 提供者只需要 region 引數。不過,您可能必須根據認證方法,以 OCI 帳戶的認證證明資料設定其他引數。OCI Terraform 提供者支援四種認證方法: API 金鑰認證、執行處理主體授權、資源主體授權以及安全權杖認證。

使用 API 金鑰認證的 provider.tf 範例
variable "tenancy_ocid" {
}
variable "user_ocid" {
}
variable "fingerprint" {
}
variable "private_key_path" {
}
variable "region" {
}

provider "oci" {
   region = var.region
   tenancy_ocid = var.tenancy_ocid
   user_ocid = var.user_ocid
   fingerprint = var.fingerprint
   private_key_path = var.private_key_path
}
含有執行處理主體授權的 provider.tf 範例
variable "region" {
}
provider "oci" {
  auth = "InstancePrincipal"
  region = var.region
}
具有資源主體授權的 provider.tf 範例
variable "region" {
}

provider "oci" {
  auth = "ResourcePrincipal"
  region = var.region
}
使用安全記號認證的 provider.tf 範例
variable "region" {
}
variable "config_file_profile" {
}
provider "oci" {
  auth = "SecurityToken"
  config_file_profile = var.config_file_profile
  region = var.region
}
nosql.tf:資源是 Terraform 語言中最重要的元素。Terraform 會將單一表格、索引及表格複本建立為資源。在此檔案中,您可以提供 NoSQL terraform 組態資源的定義,以建立「全域作用中」表格。在下面的範例中,您要新增此表格的區域複本來建立「全域作用中」表格。
  • 下方建立的資源為 oci_nosql_tableoci_nosql_index 以及 oci_nosql_table_replica。
  • 表格 DDL 定義必須包括單一表格上的使用綱要凍結使用綱要凍結強制。如需詳細資訊,請參閱建立全域作用中表格
  • 必須提供寄件者表格的表格限制 (讀取單位、寫入單位以及儲存容量)。
  • 新增區域表格複本時,您可以指定表格的名稱或表格的 OCID。如果您指定表格的名稱,則必須在定義區域複本時指定區間的 OCID 和 depends_on 子句,如下所示。如果您指定表格的 OCID,則 depends_on 子句和區間 OCID 為選擇性項目。
請參閱資源語法,瞭解如何在 Terraform 中建構資源區塊。資源命令的語法包括資源 oci_nosql_table_replica 的類型,即表格複本和資源的名稱,例如 replica_montreal。如需如何在 Terraform 命令檔中定義資源名稱的詳細資訊,請參閱 ID

附註:

您可以在 nosql.tf 命令檔中使用以下兩個選項之一 (選項 1 - 使用表格的 OCID 或選項 2 - 使用表格的名稱)。
#Option 1 of adding a regional replica of the table
resource "oci_nosql_table_replica" "replica_montreal" {
  table_name_or_id = oci_nosql_table.mr_test.id
  region = "ca-montreal-1"
#Optional
  max_read_units     = "60"
  max_write_units    = "60"
}
# Option 2 of adding a regional replica of the table
resource "oci_nosql_table_replica" "replica_toronto" {
compartment_id = var.compartment_ocid
table_name_or_id = "mr_test"
region = "ca-toronto-1"  
depends_on = [oci_nosql_table.mr_test]
}
範例 nosql.tf 檔案:
variable "compartment_ocid" {
}

variable "table_ddl_statement" {
  default = "CREATE TABLE IF NOT EXISTS mr_test(id INTEGER,
                            name STRING, info JSON,PRIMARY KEY(id))
                           using TTL 10 days with schema frozen"
}
#Add index resource "idx_age"
resource "oci_nosql_index" "idx_age" {
  table_name_or_id = oci_nosql_table.test_mrtable.id
  name = "idx_age"
  keys {
    column_name = "name"
  }
  keys {
    column_name = "info"
    json_path = "age"
    json_field_type = "anyatomic"
  }
}
resource "oci_nosql_table" "mr_test" {
  #Required
  compartment_id = var.compartment_ocid
  ddl_statement  = var.table_ddl_statement
  name           = "mr_test"

  table_limits {
    #Required
    max_read_units = 51
    max_write_units = 51
    max_storage_in_gbs = 2     
  }
}
#add a regional replica
resource "oci_nosql_table_replica" "replica_montreal" {
  table_name_or_id = oci_nosql_table.mr_test.id
  region = "ca-montreal-1"
  #Optional
  max_read_units     = "60"
  max_write_units    = "60"
}

附註:

即使來源表格已經存在,單一表格 (CREATE TABLE IF NOT EXISTS mr_test...) 的定義必須一律包含在 terraform 命令檔中。從 terraform 命令檔移除 CREATE TABLE 定義會從區域刪除表格。

terraform.tfvars:在此檔案中,根據認證方法提供必要 OCI Terraform 提供者引數的值。

使用 API 金鑰認證的 terraform.tfvars 範例:
tenancy_ocid = "<tenancy_your_ocid>"
user_ocid = "<your_user_ocid">
fingerprint = "your_fingerprint>"
private_key_path = "<path_private_key>"
compartment_ocid = "<your_compartment_ocid>"
region = "<name of your region>"
含有執行處理主體授權的 terraform.tfvars 範例:
compartment_ocid = "<your_compartment_ocid>"
region = "<name of your region>"
具有資源主體授權的 terraform.tfvars 範例:
compartment_ocid = "<your_compartment_ocid>"
region = "<name of your region>"
使用安全記號認證的 terraform.tfvars 範例:
compartment_ocid = "<your_compartment_ocid>"
region = "<name of your region>"
config_file_profile = "PROFILE"

附註:

您可以有一個可以新增整個組態詳細資訊的單一 terraform.tf 檔案,但建議您分別建立如上所示的組態檔,以提供清晰且容易維護。
步驟 2:叫用地形並初始化設定。
terraform init
步驟 3:執行下列命令來呼叫 terraform 命令檔。
terraform apply
Terraform 會顯示要套用的計畫並提示確認,如下所示。
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

確認時,會建立單一表格並新增索引。接著會建立表格的區域複本,將單一表格轉換為 GAT。