DBA Hub

📋Steps in this guide1/4

Terraform : Oracle Cloud Infrastructure (OCI) Database VM

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

oracle miscconfigurationintermediate
by OracleDba
16 views
1

Create Working Directory

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

Code/Command (click line numbers to comment):

1
2
3
4
5
mkdir \git\oraclebase\terraform\oci\oci_db
cd \git\oraclebase\terraform\oci\oci_db

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

oci_db.tf

Create a file called "oci_db.tf" with the following contents. 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 .

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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
}
3

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. The 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. - Top-Left Hamburger > Identity > Compartments - Click on the compartment of interest. - Click the "Copy" link next to "OCID". The is the OCID of the subnet the database VM will be connected to. - Top-Left Hamburger > Networking > Virtual Cloud Networks - Click on the VCN of interest. - Click on the kebab menu to the far right of the subnet of interest. - Select the "Copy OCID" option on the resulting popup menu.

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
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
4

Build the OCI Database VM

Initialize the working directory using the command. Use the command to test the execution plan. Use the command to create the OCI database VM. Check the Oracle Cloud account to see the new database VM in the compartment you chose. For more information see: - oci_database_db_system - Terraform : All Articles Hope this helps. Regards Tim...

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
terraform init

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.

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"

Comments (0)

Please to add comments

No comments yet. Be the first to comment!