|
| 1 | +[rootvariables]:https://github.com/oracle/terraform-oci-base/blob/master/examples/db/variables.tf |
| 2 | +[rootlocals]:https://github.com/oracle/terraform-oci-base/blob/master/examples/db/locals.tf |
| 3 | +[terraformoptions]:https://github.com/oracle/terraform-oci-base/blob/master/docs/terraformoptions.adoc |
| 4 | +[dbvariables]:https://github.com/oracle/terraform-oci-base/blob/master/examples/db/modules/db/variables.tf |
| 5 | +[dbvariables]:https://github.com/oracle/terraform-oci-base/blob/master/examples/db/modules/db/security.tf |
| 6 | +[dbsubnet]:https://github.com/oracle/terraform-oci-base/blob/master/examples/db/modules/db/subnets.tf |
| 7 | + |
| 8 | +Example reusing terraform-oci-base and extending to create a db instance from OCI Marketplace |
| 9 | + |
| 10 | +## Create a new Terraform project |
| 11 | + |
| 12 | +As an example, we’ll be using terraform-oci-base to create an Oracle Database on OCI. The steps required are the following: |
| 13 | + |
| 14 | +1. Create a new directory for your project e.g. oracle-database |
| 15 | + |
| 16 | +2. Create the following files in root directory of your project: |
| 17 | + |
| 18 | +- variables.tf |
| 19 | +- locals.tf |
| 20 | +- provider.tf |
| 21 | +- main.tf |
| 22 | +- terraform.tfvars |
| 23 | + |
| 24 | +3. Define the oci provider |
| 25 | + |
| 26 | +``` |
| 27 | +provider "oci" { |
| 28 | + tenancy_ocid = var.tenancy_id |
| 29 | + user_ocid = var.user_id |
| 30 | + fingerprint = var.api_fingerprint |
| 31 | + private_key_path = var.api_private_key_path |
| 32 | + region = var.region |
| 33 | + disable_auto_retries = false |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +4. Create the modules directory |
| 38 | + |
| 39 | +``` |
| 40 | +mkdir modules |
| 41 | +cd modules |
| 42 | +``` |
| 43 | + |
| 44 | +5. Add the terraform-oci-base module |
| 45 | + |
| 46 | +``` |
| 47 | +git clone https://github.com/oracle/terraform-oci-base.git base |
| 48 | +``` |
| 49 | + |
| 50 | +N.B. Cloning will be required until the module is published in Hashicorp's registry |
| 51 | + |
| 52 | +## Define project variables |
| 53 | + |
| 54 | +### Variables to reuse the base module |
| 55 | + |
| 56 | +1. Define the base parameters in the root variables.tf. You can choose to keep the same object-oriented structure like in the base’s variables.tf or keep it flat. |
| 57 | + |
| 58 | +See [variables.tf][rootvariables] in this directory. |
| 59 | + |
| 60 | +2. Initialize the variables as in [locals.tf][rootlocals] |
| 61 | + |
| 62 | +## Define your modules |
| 63 | + |
| 64 | +1. Define the base module in root main.tf |
| 65 | + |
| 66 | +``` |
| 67 | +module "base" { |
| 68 | + source = "./modules/base" |
| 69 | +
|
| 70 | + # identity |
| 71 | + oci_base_identity = local.oci_base_identity |
| 72 | +
|
| 73 | + # general oci parameters |
| 74 | + oci_base_general = local.oci_base_general |
| 75 | +
|
| 76 | + # vcn parameters |
| 77 | + oci_base_vcn = local.oci_base_vcn |
| 78 | +
|
| 79 | + # bastion parameters |
| 80 | + oci_base_bastion = local.oci_base_bastion |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +2. Enter appropriate values for terraform.tfvars. Review [Terraform Options][terraformoptions] for reference |
| 85 | + |
| 86 | +## Add your own modules |
| 87 | + |
| 88 | +1. Create your own module e.g. db. In modules directory, create a db directory: |
| 89 | + |
| 90 | +``` |
| 91 | +mkdir db |
| 92 | +``` |
| 93 | + |
| 94 | +2. Define the necessary variables, resources (e.g [security lists][dbvariables], [subnets][dbsubnet], [compute][dbcompute]) and [data sources][dbdatasources]. |
| 95 | + |
| 96 | +3. Update the [locals.tf][[rootlocals]] to initialize the db variables |
| 97 | + |
| 98 | +4. Add the db module in the main.tf |
| 99 | + |
| 100 | +``` |
| 101 | +module "db" { |
| 102 | + source = "./modules/db" |
| 103 | +
|
| 104 | + db_identity = local.db_identity |
| 105 | +
|
| 106 | + db_ssh_keys = local.oci_base_ssh_keys |
| 107 | +
|
| 108 | + db_oci_general = local.oci_base_general |
| 109 | +
|
| 110 | + db_bastion = local.db_bastion |
| 111 | +
|
| 112 | + db_network = local.db_network |
| 113 | +
|
| 114 | + db_config = local.db_config |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +5. Update your terraform variable file and add the database parameters: |
| 119 | + |
| 120 | +``` |
| 121 | +# db |
| 122 | +
|
| 123 | +db_system_shape = "VM.Standard2.8" |
| 124 | +
|
| 125 | +cpu_core_count = 2 |
| 126 | +
|
| 127 | +db_edition = "ENTERPRISE_EDITION" |
| 128 | +
|
| 129 | +db_admin_password = "BEstrO0ng_#12" |
| 130 | +
|
| 131 | +db_name = "basedb" |
| 132 | +
|
| 133 | +db_home_db_name = "basedb2" |
| 134 | +
|
| 135 | +db_version = "19.0.0.0" |
| 136 | +
|
| 137 | +db_home_display_name = "basedbhome" |
| 138 | +
|
| 139 | +db_disk_redundancy = "HIGH" |
| 140 | +
|
| 141 | +db_system_display_name = "basedb_system" |
| 142 | +
|
| 143 | +hostname = "myoracledb" |
| 144 | +
|
| 145 | +n_character_set = "AL16UTF16" |
| 146 | +
|
| 147 | +character_set = "AL32UTF8" |
| 148 | +
|
| 149 | +db_workload = "OLTP" |
| 150 | +
|
| 151 | +pdb_name = "pdb1" |
| 152 | +
|
| 153 | +data_storage_size_in_gb = 256 |
| 154 | +
|
| 155 | +license_model = "LICENSE_INCLUDED" |
| 156 | +
|
| 157 | +node_count = 2 |
| 158 | +
|
| 159 | +data_storage_percentage = 40 |
| 160 | +``` |
| 161 | + |
| 162 | +## Test access to your database: |
| 163 | + |
| 164 | +1. Login to the OCI Console and note down the IP address of 1 of the database nodes. |
| 165 | + |
| 166 | +2. ssh to the Database node: |
| 167 | + |
| 168 | +``` |
| 169 | +ssh -i </path/to/private_ssh_key> -J opc<bastion_public_ip_address> opc@<database_private_ip_address> |
| 170 | +``` |
| 171 | + |
| 172 | +3. Login to your database: |
| 173 | + |
| 174 | +``` |
| 175 | +sudo su - oracle |
| 176 | +sqlplus / as sysdba |
| 177 | +``` |
0 commit comments