Updating Global Active tables using Terraform

You can use Terraform to modify an existing Global Active Table

Add a regional replica

You can add a regional replica of an existing Global Active Table.

In this example, the Global Active table mr_test already exists and has a regional replica in the Canada Southeast(Montreal) region. To add a regional replica of this table in another region Canada Southeast(Toronto), use the following nosql.tf file to define the regional replica.

Note:

The definitions of the singleton table (CREATE TABLE IF NOT EXISTS mr_test...) and the existing replicas must always be included in the terraform script even if the source table and replicas already exist. Removing the CREATE TABLE definition from the terraform script drops the table from the region. Similarly removing the existing replica definition from the terraform script drops the regional table replica.
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"
}
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     
  }
}
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"
}
#add a regional replica
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]
}

Follow the same steps as described in Use Terraform to create a Global Active table to add a regional replica of the table in the new region. You get a prompt from Terraform that a new replica resource is being added. Once you confirm, Terraform will add the new regional replica of the Global Active table.

Drop a regional replica

You can drop a regional replica of an existing Global Active Table.

In this example, the Global Active table mr_test already exists and has a replica in the region Canada Southeast(Montreal) and Canada Southeast(Toronto). To drop the replica from the Canada Southeast(Toronto) region, use the following nosql.tf file where you need to comment (or remove) the code pertaining to adding the replica in Canada Southeast(Toronto).

Note:

The definition of the singleton table (CREATE TABLE IF NOT EXISTS mr_test...) must always be included in the terraform script even if the source table already exists. Removing the CREATE TABLE definition from the terraform script drops the table from the region.
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"
}
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     
  }
}
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"
}
#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]
#}

Follow the same steps as described in Use Terraform to create a Global Active table to drop a replica of the table from the Canada Southeast(Toronto) region. You get a prompt from Terraform that the replica oci_nosql_table_replica.replica_toronto is being destroyed. Once you confirm, Terraform will drop the regional replica of the Global Active table.

Change the table capacity of a Global Active Table

You can change the read units, write units, storage capacity, and table TTL for a Global Active table using Terraform.

In a Global Active table, changing the read capacity limit or write capacity limit applies only to the local region where it is changed. However, changing the storage capacity or changing the default table level TTL value applies the changes to all the regional replicas of the table.

To change the table capacity of the mr_test table, use the following nosql.tf file to change the table properties(default table TTL) and change the table limits.
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 15 days with schema frozen"
}
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 = 60
    max_write_units = 60
    max_storage_in_gbs = 2     
  }
}
resource "oci_nosql_table_replica" "replica_montreal" {
  table_name_or_id = oci_nosql_table.mr_test.id
  region = "ca-montreal-1"
}
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]
}
Follow the same steps as described in Use Terraform to create a Global Active table with the modified nosql.tf file as shown above.

Note:

A Global Active table has a symmetrical table definition including schema, index, TTL, and storage size in all the regional replicas. If you make a change to an index, TTL or storage size in one regional replica, it is automatically applied to all other regional replicas. So it is recommended that you manage these table definitions from one region only.

Edit the table capacity of a single regional replica

Using Terraform, you cannot directly edit the capacity of a regional table replica.

For example, you cannot directly change the read unit and write unit for the replica in the Canada Southeast(Toronto) region directly using Terraform. In this case, you need to import the table resource first and then change the limits as shown in the example below.

Note:

The definition of the singleton table (CREATE TABLE IF NOT EXISTS mr_test...) must always be included in the terraform script even if the source table already exists. Removing the CREATE TABLE definition from the terraform script drops the table from the region.
#Table has been created on ca-montreal-1
#update table read/write units to 100/100
resource "oci_nosql_table" "mr_test" {
  #Required
  compartment_id = var.compartment_ocid
  ddl_statement  = "CREATE TABLE IF NOT EXISTS mr_test(id INTEGER,
                    name STRING, info JSON, PRIMARY KEY(id))
                    with schema frozen"
  name           = "mr_test"
  table_limits {
    #Required
    max_read_units     = "100"
    max_write_units    = "100"
    max_storage_in_gbs = "1"
  }
}

#Run terraform import 
#Import table on ca-montreal-1 to bring it under Terraform management
$terraform import oci_nosql_table.mr_test <OCID>
$terraform apply

Terraform shows the plan to be applied and prompts for confirmation as shown below.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.