使用 Terraform 部署全域作用中表格
使用 Terraform 在 OCI 上部署全域作用中表格相當簡單。
Terraform 是一種基礎架構即程式碼工具,可讓您以安全且有效率的方式建置、變更及版本雲端和內部部署資源。若要對 Terraform 有基本的瞭解,請閱讀簡短的簡介。確定您已安裝 OCI Terraform 提供者並已設定 API 金鑰認證、執行處理主要項目授權、資源主要項目授權以及安全權杖認證。
部署全域表格的步驟
下面的 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_table 、 oci_nosql_index 。
-
表格 DDL 定義必須包含單一表格上的
with schema frozen或with schema frozen force。如需詳細資訊,請參閱建立全域作用中表格。 -
必須提供寄件者表格的表格限制 (讀取單位、寫入單位以及儲存容量)。
-
當您新增區域表格複本時,可以指定表格的名稱或表格的 OCID。如果您指定表格的名稱,則在定義區域複本時,必須指定區間的 OCID 和
depends_on子句,如下所示。如果指定表格的 OCID,則depends_on子句和區間 OCID 為選擇性。
請參閱資源語法,瞭解如何在 Terraform 中建構資源區塊。資源命令的語法包括資源 oci_nosql_table_replica 的類型 (表格複本和資源的名稱),例如 replica_montreal。如需有關如何在 Terraform 指令碼中定義資源名稱的詳細資訊,請參閱識別碼。
注意:您可以在 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 並初始化設定。
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。