Intro
What’s the point of moving a database to the Cloud if we can’t automatically deploy it. After blogging about web compute provisioning on AWS,OCI, Azure, & GCP using terraform. The least I could do as a (future-ex) DBA is to terraform database provisioning in Oracle Cloud. On top of that, I also wanted to include a bastion service session to connect to the DBCS instance in the private subnet .

Here’s a link to my GitHub repo linked to this lab: brokedba/terraform-provider-oci/database-system
Where do I find a good OCI Database deployment sample?
I explored the official Oracle Cloud GitHub repository, but I couldn’t find a simple stack with no frills. I mean, when you want to deploy a database for the first time, you don’t want to spin 10 other app components that have nothing to do with your DB. I then decided to gather the bare minimum using a function called terraform import that will retro-engineer the code from an existing database and carried on from there.
Overview & topology
The above illustration shows the different components involved in this OCI Terraform Database stack.
VCN
Database Cloud system 21c with 1 PDB
Bastion service + Bastion session using port forwarding SSH targeting the DB instance
2x Subnets
DB Subnet: private / linked to default route table
App Subnet: public / linked to app route table
2x route tables
Default route table > routes to NAT Gateway &Service gateway
App route table > routes to internet gateway
2x security lists
db sec list ports : ingress 22/1521 from app subnet
App sec list ports ": ingress 22/80/443 , egress 1521
I.Terraform setup
Windows: Download and run the installer from their website (32-bit ,64-bit)
Linux : Download, unzip and move the binary to the local bin directory
$ wget https://releases.hashicorp.com/terraform/1.0.3/terraform_1.0.3_linux_amd64.zip
$ unzip terraform_1.0.3_linux_amd64.zip
$ mv terraform /usr/local/bin/
$ terraform --version
Terraform v1.0.3
II. Clone the repository
Pick an empty directory on your file system and issue the following command
$ git clone https://github.com/brokedba/terraform-examples.git
You will find a sub-directory called database-system in the repository where the DBCS stack is located:
III. terraform config content
# Adapt the below variables to your own tenancy authentication configuration
$ vi terraform.tfvars
export TF_VAR_tenancy_ocid="ocid1.tenancy.oc1..aaaaaaaa" # change me
export TF_VAR_user_ocid="ocid1.user.oc1..aaaaaaaa" # change me
export TF_VAR_compartment_ocid="ocid1.tenancy.oc1..aaaaaaaa" # change me
export TF_VAR_fingerprint=$(cat PATH_To_Fing/oci_api_key_fingerprint)# change me
export TF_VAR_private_key_path=PATH_To_APIKEY/oci_api_key.pem # change me
export TF_VAR_ssh_public_key=$(cat PATH_To_PublicSSH/id_rsa.pub) # change me export TF_VAR_region="ca-toronto-1" # change me
export TF_VAR_db_admin_password="DBwelcome2022##"
$ . terraform.tfvars
Terraform files OVERVIEW:
I will only show excerpts from database.tf /output.tf to have an idea but all *.tf files are accessible on my Repo.
database.tf
All variables can of course be changed to your liking in the variables.tf
output.tf
IV. DBCS Stack deployment
Make sure you copied the adjusted terraform-tfvars file and sourced it. You can then run the plan command (output is truncated for more readability)
$ terraform plan
------------------------------------------------------------------------
Terraform will perform the following actions:
... # VCN declaration
# oci_bastion_bastion.mybastion will be created
+ resource "oci_bastion_bastion" "mybastion" { ...
# oci_bastion_session.mybastion_session will be created
+ resource "oci_bastion_session" "mybastion_session" {
...
+ target_resource_details {
+ session_type = "PORT_FORWARDING"
+ target_resource_display_name = (known after apply)
+ target_resource_operating_system_user_name = (known after apply)
+ target_resource_port = 22
+ target_resource_private_ip_address = "192.168.78.10"
}
}
# oci_core_subnet.terraApp will be created
+ resource "oci_core_subnet" "terraApp" { ...
# oci_core_subnet.terraDB will be created
+ resource "oci_core_subnet" "terraDB" {
+ availability_domain = "gwmA:CA-TORONTO-1-AD-1"
+ cidr_block = "192.168.78.0/24"
...
# oci_core_virtual_network.vcnterra will be created
+ resource "oci_core_virtual_network" "vcnterra" {
+ cidr_block = "192.168.64.0/20"
+ display_name = "db-vcn"
+ dns_label = "terravcn"
...}
..
# oci_database_db_system.MYDBSYS will be created
+ resource "oci_database_db_system" "MYDBSYS" {
+ availability_domain = "gwmA:CA-TORONTO-1-AD-1"
+ database_edition = "STANDARD_EDITION"
+ data_storage_size_in_gb = 256
+ display_name = "DBCSDEMO"
+ hostname = "hopsdb-oci"
+ license_model = "LICENSE_INCLUDED"
+ private_ip = "192.168.78.10"
+ shape = "VM.Standard2.4"
+ ssh_public_keys =[...
+ db_home {
+ db_version = "21.0.0.0"
...
+ database {
+ db_workload = "OLTP"
+ db_name = "MYCDB"
+ pdb_name = "PDB1"
...
+ db_backup_config { …
+ backup_destination_details {
}
}
}
}
+ db_system_options {
+ storage_management = (known after apply) }