Terraformを使用したグローバル・アクティブ表のデプロイ

Terraformを使用して、OCIにグローバル・アクティブ表を簡単にデプロイできます。

Terraformは、クラウドおよびオンプレミスのリソースを安全かつ効率的に構築、変更およびバージョニングできるInfrastructure-as-Codeツールです。Terraformの基本を理解するには、簡単な概要を参照してください。OCI Terraformプロバイダがインストールされ、APIキー認証、Instance Principal認可、リソース・プリンシパル認可およびセキュリティ・トークン認証が構成されていることを確認します。

グローバル表をデプロイするステップ

次のterraformスクリプトでは、次の処理が実行されます。

手順:

ステップ1:グローバル・アクティブ表のTerraform構成ファイルを作成します: グローバル・アクティブ表を作成するには、次の構成ファイルを作成する必要があります。

provider.tf:このファイルには、OCI Terraformプロバイダ定義および関連する変数定義を指定します。OCI Terraformプロバイダに必要なのは、region引数のみです。ただし、認証方法に基づいて、OCIアカウントの認証資格証明を使用して追加の引数を構成する必要がある場合があります。

OCI Terraformプロバイダは、APIキー認証、Instance Principal認可、リソース・プリンシパル認可およびセキュリティ・トークン認証の4つの認証方法をサポートしています。

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
}

Instance Principal認可の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構成リソースの定義を指定します。次の例では、この表のリージョナル・レプリカを追加して、グローバル・アクティブ表を作成します。

Terraformでリソース・ブロックを構築する方法を理解するには、リソース構文を参照してください。リソース・コマンドの構文には、表レプリカであるリソースoci_nosql_table_replicaのタイプとリソースの名前(replica_montrealなど)が含まれます。Terraformスクリプトでリソース名を定義する方法の詳細は、識別子を参照してください。

注意: nosql.tfスクリプトでは、次の2つのオプションのいずれかを使用できます(オプション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スクリプトに含まれている必要があります。テラフォーム・スクリプトから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>"

Instance Principal認可の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に変換されます。