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) Autonomous Database (ADW, ATP, AJD, APX)

This article describes how to create an autonomous database (ADW, ATP, AJD, APX) 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_adb
cd \git\oraclebase\terraform\oci\oci_adb

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_adb.tf

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

# Variables
variable "compartment_id" { type = string }
variable "db_name"        { type = string }
variable "admin_password" { type = string }
variable "db_version"     { type = string }
# OLTP, DW, AJD, APEX
variable "db_workload"    { type = string }
# Must be false for AJD and APEX
variable "is_free_tier"   { type = string }
# BRING_YOUR_OWN_LICENSE or LICENSE_INCLUDED
variable "license_model"  { type = string }

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

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


# Resources
resource "oci_database_autonomous_database" "tf_adb" {
  compartment_id           = var.compartment_id
  cpu_core_count           = var.cpu_core_count
  data_storage_size_in_tbs = var.data_storage_size_in_tbs
  db_name                  = var.db_name
  admin_password           = var.admin_password
  db_version               = var.db_version
  db_workload              = var.db_workload
  display_name             = var.db_name
  is_free_tier             = var.is_free_tier
  license_model            = var.license_model
}


# Outputs
output "db_name" {
  value = oci_database_autonomous_database.tf_adb.display_name
}

output "db_state" {
  value = oci_database_autonomous_database.tf_adb.state
}

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 CPUs and storage parameters are defaulted. The resources section defines the autonomous database using the input variables. The outputs section allows us to see information about the autonomous database that's been created, including the name and state.

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_adb_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_adb_variables.auto.tfvars". The contents will vary depending on the type autonomous database you want to provision.

For an autonomous data warehouse (ADW) service you would use something like this.

compartment_id = "ocid1.compartment.oc1..aaaaaaaa..."
db_name        = "obadw2"
admin_password = "MyStrongPassword123"
db_version     = "21c"
# OLTP, DW, AJD, APEX
db_workload    = "DW"
# Must be false for AJD and APEX
is_free_tier   = "true"
license_model  = "LICENSE_INCLUDED"

For an autonomous transaction processing (ATP) service you would use something like this.

compartment_id = "ocid1.compartment.oc1..aaaaaaaa..."
db_name        = "obatp2"
admin_password = "MyStrongPassword123"
db_version     = "21c"
# OLTP, DW, AJD, APEX
db_workload    = "OLTP"
# Must be false for AJD and APEX
is_free_tier   = "true"
license_model  = "LICENSE_INCLUDED"

For an autonomous JSON database (AJD) service you would use something like this.

compartment_id = "ocid1.compartment.oc1..aaaaaaaa..."
db_name        = "obajd2"
admin_password = "MyStrongPassword123"
db_version     = "19c"
# OLTP, DW, AJD, APEX
db_workload    = "AJD"
# Must be false for AJD and APEX
is_free_tier   = "false"
license_model  = "LICENSE_INCLUDED"

For an APEX Developer Instance (APX) service you would use something like this.

compartment_id = "ocid1.compartment.oc1..aaaaaaaa..."
db_name        = "obapx2"
admin_password = "MyStrongPassword123"
db_version     = "19c"
# OLTP, DW, AJD, APEX
db_workload    = "APEX"
# Must be false for AJD and APEX
is_free_tier   = "false"
license_model  = "LICENSE_INCLUDED"

The compartment_id is the OCID of the compartment that will house the autonomous database. 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.

Build the OCI Autonomous Database

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_autonomous_database.tf_adb will be created
  + resource "oci_database_autonomous_database" "tf_adb" {
      + admin_password                                 = (sensitive value)
      + apex_details                                   = (known after apply)
      + are_primary_whitelisted_ips_used               = (known after apply)
      + autonomous_container_database_id               = (known after apply)
      + autonomous_database_backup_id                  = (known after apply)
      + autonomous_database_id                         = (known after apply)
      + available_upgrade_versions                     = (known after apply)
      + backup_config                                  = (known after apply)
      + clone_type                                     = (known after apply)
      + compartment_id                                 = "ocid1.compartment.oc1..aaaaaaaa..."
      + connection_strings                             = (known after apply)
      + connection_urls                                = (known after apply)
      + cpu_core_count                                 = 1
      + data_safe_status                               = (known after apply)
      + data_storage_size_in_gb                        = (known after apply)
      + data_storage_size_in_tbs                       = 1
      + db_name                                        = "obadw2"
      + db_version                                     = "21c"
      + db_workload                                    = "DW"
      + defined_tags                                   = (known after apply)
      + display_name                                   = "obadw2"
      + failed_data_recovery_in_seconds                = (known after apply)
      + freeform_tags                                  = (known after apply)
      + id                                             = (known after apply)
      + infrastructure_type                            = (known after apply)
      + is_access_control_enabled                      = (known after apply)
      + is_auto_scaling_enabled                        = (known after apply)
      + is_data_guard_enabled                          = (known after apply)
      + is_dedicated                                   = (known after apply)
      + is_free_tier                                   = true
      + is_preview                                     = (known after apply)
      + is_preview_version_with_service_terms_accepted = (known after apply)
      + is_refreshable_clone                           = (known after apply)
      + key_store_id                                   = (known after apply)
      + key_store_wallet_name                          = (known after apply)
      + license_model                                  = "LICENSE_INCLUDED"
      + lifecycle_details                              = (known after apply)
      + nsg_ids                                        = (known after apply)
      + open_mode                                      = (known after apply)
      + operations_insights_status                     = (known after apply)
      + permission_level                               = (known after apply)
      + private_endpoint                               = (known after apply)
      + private_endpoint_ip                            = (known after apply)
      + private_endpoint_label                         = (known after apply)
      + refreshable_mode                               = (known after apply)
      + refreshable_status                             = (known after apply)
      + role                                           = (known after apply)
      + service_console_url                            = (known after apply)
      + source                                         = (known after apply)
      + source_id                                      = (known after apply)
      + standby_db                                     = (known after apply)
      + standby_whitelisted_ips                        = (known after apply)
      + state                                          = (known after apply)
      + subnet_id                                      = (known after apply)
      + system_tags                                    = (known after apply)
      + time_created                                   = (known after apply)
      + time_deletion_of_free_autonomous_database      = (known after apply)
      + time_maintenance_begin                         = (known after apply)
      + time_maintenance_end                           = (known after apply)
      + time_of_last_failover                          = (known after apply)
      + time_of_last_refresh                           = (known after apply)
      + time_of_last_refresh_point                     = (known after apply)
      + time_of_last_switchover                        = (known after apply)
      + time_of_next_refresh                           = (known after apply)
      + time_reclamation_of_free_autonomous_database   = (known after apply)
      + timestamp                                      = (known after apply)
      + used_data_storage_size_in_tbs                  = (known after apply)
    }

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

Changes to Outputs:
  + db_name  = "obadw2"
  + db_state = (known after apply)

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

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 autonomous database.

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_autonomous_database.tf_adb will be created
  + resource "oci_database_autonomous_database" "tf_adb" {
      + admin_password                                 = (sensitive value)
      + apex_details                                   = (known after apply)
      + are_primary_whitelisted_ips_used               = (known after apply)
      + autonomous_container_database_id               = (known after apply)
      + autonomous_database_backup_id                  = (known after apply)
      + autonomous_database_id                         = (known after apply)
      + available_upgrade_versions                     = (known after apply)
      + backup_config                                  = (known after apply)
      + clone_type                                     = (known after apply)
      + compartment_id                                 = "ocid1.compartment.oc1..aaaaaaaa..."
      + connection_strings                             = (known after apply)
      + connection_urls                                = (known after apply)
      + cpu_core_count                                 = 1
      + data_safe_status                               = (known after apply)
      + data_storage_size_in_gb                        = (known after apply)
      + data_storage_size_in_tbs                       = 1
      + db_name                                        = "obadw2"
      + db_version                                     = "21c"
      + db_workload                                    = "DW"
      + defined_tags                                   = (known after apply)
      + display_name                                   = "obadw2"
      + failed_data_recovery_in_seconds                = (known after apply)
      + freeform_tags                                  = (known after apply)
      + id                                             = (known after apply)
      + infrastructure_type                            = (known after apply)
      + is_access_control_enabled                      = (known after apply)
      + is_auto_scaling_enabled                        = (known after apply)
      + is_data_guard_enabled                          = (known after apply)
      + is_dedicated                                   = (known after apply)
      + is_free_tier                                   = true
      + is_preview                                     = (known after apply)
      + is_preview_version_with_service_terms_accepted = (known after apply)
      + is_refreshable_clone                           = (known after apply)
      + key_store_id                                   = (known after apply)
      + key_store_wallet_name                          = (known after apply)
      + license_model                                  = "LICENSE_INCLUDED"
      + lifecycle_details                              = (known after apply)
      + nsg_ids                                        = (known after apply)
      + open_mode                                      = (known after apply)
      + operations_insights_status                     = (known after apply)
      + permission_level                               = (known after apply)
      + private_endpoint                               = (known after apply)
      + private_endpoint_ip                            = (known after apply)
      + private_endpoint_label                         = (known after apply)
      + refreshable_mode                               = (known after apply)
      + refreshable_status                             = (known after apply)
      + role                                           = (known after apply)
      + service_console_url                            = (known after apply)
      + source                                         = (known after apply)
      + source_id                                      = (known after apply)
      + standby_db                                     = (known after apply)
      + standby_whitelisted_ips                        = (known after apply)
      + state                                          = (known after apply)
      + subnet_id                                      = (known after apply)
      + system_tags                                    = (known after apply)
      + time_created                                   = (known after apply)
      + time_deletion_of_free_autonomous_database      = (known after apply)
      + time_maintenance_begin                         = (known after apply)
      + time_maintenance_end                           = (known after apply)
      + time_of_last_failover                          = (known after apply)
      + time_of_last_refresh                           = (known after apply)
      + time_of_last_refresh_point                     = (known after apply)
      + time_of_last_switchover                        = (known after apply)
      + time_of_next_refresh                           = (known after apply)
      + time_reclamation_of_free_autonomous_database   = (known after apply)
      + timestamp                                      = (known after apply)
      + used_data_storage_size_in_tbs                  = (known after apply)
    }

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

Changes to Outputs:
  + db_name  = "obadw2"
  + db_state = (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_autonomous_database.tf_adb: Creating...
oci_database_autonomous_database.tf_adb: Still creating... [10s elapsed]
oci_database_autonomous_database.tf_adb: Still creating... [20s elapsed]
oci_database_autonomous_database.tf_adb: Still creating... [30s elapsed]
oci_database_autonomous_database.tf_adb: Still creating... [40s elapsed]
oci_database_autonomous_database.tf_adb: Still creating... [50s elapsed]
oci_database_autonomous_database.tf_adb: Still creating... [1m0s elapsed]
oci_database_autonomous_database.tf_adb: Creation complete after 1m8s [id=ocid1.autonomousdatabase.oc1.uk-london-1....]

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

Outputs:

db_name = "obadw2"
db_state = "AVAILABLE"

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

For more information see:

Hope this helps. Regards Tim...

Back to the Top.