使用 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 脚本中定义资源名称的详细信息,请参阅标识符

注意:

您可以在 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。