8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Misc » Here

Terraform : Oracle Cloud Infrastructure (OCI) Database VM

This article describes how to create a database virtual machine on Oracle Cloud Infrastructure (OCI) using Terraform.

Related articles.

Create Working Directory

Create a new working directory and switch to that directory.

mkdir \git\oraclebase\terraform\oci\oci_db
cd \git\oraclebase\terraform\oci\oci_db

In a previous article (here) we discussed the creation of an OCI provider. Copy the OCI provider information into this new working directory.

copy \git\oraclebase\terraform\oci\oci_provider\*.tf .
copy \git\oraclebase\terraform\oci\oci_provider\*.tfvars .

oci_db.tf

Create a file called "oci_db.tf" with the following contents.

# Variables
variable "compartment_id"      { type = string }
variable "db_admin_password"   { type = string }
variable "db_name"             { type = string }
variable "db_public_keys"      { type = list(string) }
variable "db_subnet_id"        { type = string }
variable "db_display_name"     { type = string }
variable "db_pdb_name"         { type = string }
variable "db_hostname"         { type = string }
variable "db_host_domain"      { type = string }
variable "db_storage_gb"       { type = number }

variable "db_workload" {
  type    = string
  default = "OLTP"
}

variable "db_version" {
  type    = string
  default = "21.1.0.0"
}

variable "db_shape" {
  type    = string
  default = "VM.Standard2.1"
}

variable "db_database_edition" {
  type    = string
  default = "ENTERPRISE_EDITION_EXTREME_PERFORMANCE"
}

variable "db_license_model" {
  type    = string
  default = "LICENSE_INCLUDED"
}

variable "db_node_count" {
  type    = number
  default = 1
}


# Resources
data "oci_identity_availability_domains" "ads" {
  compartment_id = var.compartment_id
}

resource "oci_database_db_system" "tf_db" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_id
  db_home {
    database {
      admin_password = var.db_admin_password
      db_name        = var.db_name
      db_workload    = var.db_workload
      pdb_name       = var.db_pdb_name
    }
    db_version = var.db_version
  }
  hostname                = var.db_hostname
  shape                   = var.db_shape
  ssh_public_keys         = var.db_public_keys
  subnet_id               = var.db_subnet_id
  display_name            = var.db_display_name
  data_storage_size_in_gb = var.db_storage_gb
  database_edition        = var.db_database_edition
  domain                  = var.db_host_domain
  license_model           = var.db_license_model
  node_count              = var.db_node_count
}


# Outputs
output "db_display_name" {
  value = oci_database_db_system.tf_db.display_name
}

output "db_id" {
  value = oci_database_db_system.tf_db.id
}

output "db_state" {
  value = oci_database_db_system.tf_db.state
}

output "first-availability-domain_name" {
  value = data.oci_identity_availability_domains.ads.availability_domains[0].name
}

The file begins with variable definitions. We could set default values for these variables, or use literal values directly in the provider definition, but we don't want sensitive information checked into version control, so it makes sense to separate out variable values from the script. The resources section defines the database VM using the input variables. The outputs section allows us to see information about the database VM that's been created, including the ID and state of the database.

The variables, resources and outputs sections can be split into separate files if you find that organisation easier. It may help for more complex definitions.

The full list of parameters and outputs available can be found here.

oci_db_variables.auto.tfvars

There are a number of ways to supply values for input variables (see here). In this example we'll use a ".auto.tfvars" file. We won't check this script into version control as it contains sensitive information.

Create a file called "oci_db_variables.auto.tfvars" with the following contents. Adjust the values to match your desired compartment details.

compartment_id      = "ocid1.compartment.oc1..aaaaaaaa..."
db_admin_password   = "MyStrongPassword123--"
db_host_domain      = "obdomain"
db_name             = "cdb1"
db_workload         = "OLTP"
db_pdb_name         = "pdb1"
db_version          = "21.1.0.0"
db_hostname         = "obtest2"
db_shape            = "VM.Standard2.1"
db_public_keys      = ["ssh-rsa AAAAB3Nza..."]
db_subnet_id        = "ocid1.subnet.oc1.uk-london-1.aaaaaaaa..."
db_display_name     = "obtest2"
db_storage_gb       = 256

The compartment_id is the OCID of the compartment that will house the database VM. You must adjust it with a valid value from your Oracle Cloud account. You would not normally use the root compartment for this. You can get the ID of a compartment from your Oracle Cloud account as follows.

The db_subnet_id is the OCID of the subnet the database VM will be connected to.

Build the OCI Database VM

Initialize the working directory using the terraform init command.

terraform init

Use the terraform plan command to test the execution plan.

terraform plan

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # oci_database_db_system.tf_db will be created
  + resource "oci_database_db_system" "tf_db" {
      + availability_domain                     = "oVQK:UK-LONDON-1-AD-1"
      + backup_subnet_id                        = (known after apply)
      + cluster_name                            = (known after apply)
      + compartment_id                          = "ocid1.compartment.oc1..aaaaaaaa..."
      + cpu_core_count                          = (known after apply)
      + data_storage_percentage                 = (known after apply)
      + data_storage_size_in_gb                 = 256
      + database_edition                        = "ENTERPRISE_EDITION_EXTREME_PERFORMANCE"
      + defined_tags                            = (known after apply)
      + disk_redundancy                         = (known after apply)
      + display_name                            = "obtest2"
      + domain                                  = "obdomain"
      + fault_domains                           = (known after apply)
      + freeform_tags                           = (known after apply)
      + hostname                                = "obtest2"
      + id                                      = (known after apply)
      + iorm_config_cache                       = (known after apply)
      + kms_key_id                              = (known after apply)
      + kms_key_version_id                      = (known after apply)
      + last_maintenance_run_id                 = (known after apply)
      + last_patch_history_entry_id             = (known after apply)
      + license_model                           = "LICENSE_INCLUDED"
      + lifecycle_details                       = (known after apply)
      + listener_port                           = (known after apply)
      + maintenance_window                      = (known after apply)
      + next_maintenance_run_id                 = (known after apply)
      + node_count                              = 1
      + point_in_time_data_disk_clone_timestamp = (known after apply)
      + private_ip                              = (known after apply)
      + reco_storage_size_in_gb                 = (known after apply)
      + scan_dns_name                           = (known after apply)
      + scan_dns_record_id                      = (known after apply)
      + scan_ip_ids                             = (known after apply)
      + shape                                   = "VM.Standard2.1"
      + source                                  = (known after apply)
      + source_db_system_id                     = (known after apply)
      + sparse_diskgroup                        = (known after apply)
      + ssh_public_keys                         = [
          + "ssh-rsa AAAAB3Nza...",
        ]
      + state                                   = (known after apply)
      + subnet_id                               = "ocid1.subnet.oc1.uk-london-1.aaaaaaaad..."
      + time_created                            = (known after apply)
      + time_zone                               = (known after apply)
      + version                                 = (known after apply)
      + vip_ids                                 = (known after apply)
      + zone_id                                 = (known after apply)

      + db_home {
          + database_software_image_id  = (known after apply)
          + db_home_location            = (known after apply)
          + db_version                  = "21.1.0.0"
          + defined_tags                = (known after apply)
          + display_name                = (known after apply)
          + freeform_tags               = (known after apply)
          + id                          = (known after apply)
          + last_patch_history_entry_id = (known after apply)
          + lifecycle_details           = (known after apply)
          + state                       = (known after apply)
          + time_created                = (known after apply)

          + database {
              + admin_password                        = (sensitive value)
              + backup_id                             = (known after apply)
              + backup_tde_password                   = (sensitive value)
              + character_set                         = (known after apply)
              + connection_strings                    = (known after apply)
              + database_id                           = (known after apply)
              + database_software_image_id            = (known after apply)
              + db_domain                             = (known after apply)
              + db_name                               = "cdb1"
              + db_unique_name                        = (known after apply)
              + db_workload                           = "OLTP"
              + defined_tags                          = (known after apply)
              + freeform_tags                         = (known after apply)
              + id                                    = (known after apply)
              + lifecycle_details                     = (known after apply)
              + ncharacter_set                        = (known after apply)
              + pdb_name                              = "pdb1"
              + state                                 = (known after apply)
              + tde_wallet_password                   = (sensitive value)
              + time_created                          = (known after apply)
              + time_stamp_for_point_in_time_recovery = (known after apply)

              + db_backup_config {
                  + auto_backup_enabled     = (known after apply)
                  + auto_backup_window      = (known after apply)
                  + recovery_window_in_days = (known after apply)

                  + backup_destination_details {
                      + id   = (known after apply)
                      + type = (known after apply)
                    }
                }
            }
        }

      + db_system_options {
          + storage_management = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + db_display_name                = "obtest2"
  + db_id                          = (known after apply)
  + db_state                       = (known after apply)
  + first-availability-domain_name = "oVQK:UK-LONDON-1-AD-1"

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

Use the terraform apply command to create the OCI database VM.

terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # oci_database_db_system.tf_db will be created
  + resource "oci_database_db_system" "tf_db" {
      + availability_domain                     = "oVQK:UK-LONDON-1-AD-1"
      + backup_subnet_id                        = (known after apply)
      + cluster_name                            = (known after apply)
      + compartment_id                          = "ocid1.compartment.oc1..aaaaaaaa..."
      + cpu_core_count                          = (known after apply)
      + data_storage_percentage                 = (known after apply)
      + data_storage_size_in_gb                 = 256
      + database_edition                        = "ENTERPRISE_EDITION_EXTREME_PERFORMANCE"
      + defined_tags                            = (known after apply)
      + disk_redundancy                         = (known after apply)
      + display_name                            = "obtest2"
      + domain                                  = "obdomain"
      + fault_domains                           = (known after apply)
      + freeform_tags                           = (known after apply)
      + hostname                                = "obtest2"
      + id                                      = (known after apply)
      + iorm_config_cache                       = (known after apply)
      + kms_key_id                              = (known after apply)
      + kms_key_version_id                      = (known after apply)
      + last_maintenance_run_id                 = (known after apply)
      + last_patch_history_entry_id             = (known after apply)
      + license_model                           = "LICENSE_INCLUDED"
      + lifecycle_details                       = (known after apply)
      + listener_port                           = (known after apply)
      + maintenance_window                      = (known after apply)
      + next_maintenance_run_id                 = (known after apply)
      + node_count                              = 1
      + point_in_time_data_disk_clone_timestamp = (known after apply)
      + private_ip                              = (known after apply)
      + reco_storage_size_in_gb                 = (known after apply)
      + scan_dns_name                           = (known after apply)
      + scan_dns_record_id                      = (known after apply)
      + scan_ip_ids                             = (known after apply)
      + shape                                   = "VM.Standard2.1"
      + source                                  = (known after apply)
      + source_db_system_id                     = (known after apply)
      + sparse_diskgroup                        = (known after apply)
      + ssh_public_keys                         = [
          + "ssh-rsa AAAAB3NzaC1yc...",
        ]
      + state                                   = (known after apply)
      + subnet_id                               = "ocid1.subnet.oc1.uk-london-1.aaaaaaaad..."
      + time_created                            = (known after apply)
      + time_zone                               = (known after apply)
      + version                                 = (known after apply)
      + vip_ids                                 = (known after apply)
      + zone_id                                 = (known after apply)

      + db_home {
          + database_software_image_id  = (known after apply)
          + db_home_location            = (known after apply)
          + db_version                  = "21.1.0.0"
          + defined_tags                = (known after apply)
          + display_name                = (known after apply)
          + freeform_tags               = (known after apply)
          + id                          = (known after apply)
          + last_patch_history_entry_id = (known after apply)
          + lifecycle_details           = (known after apply)
          + state                       = (known after apply)
          + time_created                = (known after apply)

          + database {
              + admin_password                        = (sensitive value)
              + backup_id                             = (known after apply)
              + backup_tde_password                   = (sensitive value)
              + character_set                         = (known after apply)
              + connection_strings                    = (known after apply)
              + database_id                           = (known after apply)
              + database_software_image_id            = (known after apply)
              + db_domain                             = (known after apply)
              + db_name                               = "cdb1"
              + db_unique_name                        = (known after apply)
              + db_workload                           = "OLTP"
              + defined_tags                          = (known after apply)
              + freeform_tags                         = (known after apply)
              + id                                    = (known after apply)
              + lifecycle_details                     = (known after apply)
              + ncharacter_set                        = (known after apply)
              + pdb_name                              = "pdb1"
              + state                                 = (known after apply)
              + tde_wallet_password                   = (sensitive value)
              + time_created                          = (known after apply)
              + time_stamp_for_point_in_time_recovery = (known after apply)

              + db_backup_config {
                  + auto_backup_enabled     = (known after apply)
                  + auto_backup_window      = (known after apply)
                  + recovery_window_in_days = (known after apply)

                  + backup_destination_details {
                      + id   = (known after apply)
                      + type = (known after apply)
                    }
                }
            }
        }

      + db_system_options {
          + storage_management = (known after apply)
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + db_id = (known after apply)

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

oci_database_db_system.tf_db: Creating...
oci_database_db_system.tf_db: Still creating... [10s elapsed]
oci_database_db_system.tf_db: Still creating... [20s elapsed]
oci_database_db_system.tf_db: Still creating... [30s elapsed]
...
oci_database_db_system.tf_db: Still creating... [1h6m22s elapsed]
oci_database_db_system.tf_db: Still creating... [1h6m32s elapsed]
oci_database_db_system.tf_db: Creation complete after 1h6m33s [id=ocid1.dbsystem.oc1.uk-london-1.abwgil...]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

db_display_name = "obtest2"
db_id = "ocid1.dbsystem.oc1.uk-london-1.abwgil..."
db_state = "AVAILABLE"
first-availability-domain_name = "oVQK:UK-LONDON-1-AD-1"

Check the Oracle Cloud account to see the new database VM in the compartment you chose.

For more information see:

Hope this helps. Regards Tim...

Back to the Top.