使用 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。