DBA Hub

📋Steps in this guide1/4

Terraform : Oracle Cloud Infrastructure (OCI) Compute Instance

This article describes how to create a compute instance on Oracle Cloud Infrastructure (OCI) using Terraform.

oracle miscconfigurationintermediate
by OracleDba
13 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_compute
cd \git\oraclebase\terraform\oci\oci_compute

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

oci_compute.tf

Create a file called "oci_compute.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. Many of the parameters are defaulted. The resources section defines the compute instance using the input variables. The outputs section allows us to see information about the compute instance 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 . You can also display the relevant information using the script defined 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
# Variables
variable "compartment_id"              { type = string }
variable "compute_name"                { type = string }
variable "compute_subnet_id"           { type = string }
variable "compute_image_id"            { type = string }
variable "compute_ssh_authorized_keys" { type = string }

variable "compute_shape" {
  type    = string
  default = "VM.Standard.E2.1.Micro"
}

variable "compute_cpus" {
  type    = string
  default = "1"
}

variable "compute_memory_in_gbs" {
  type    = string
  default = "1"
}


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

resource "oci_core_instance" "tf_compute" {
  # Required
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_id
  shape               = var.compute_shape

  source_details {
    source_id         = var.compute_image_id
    source_type       = "image"
  }

  # Optional
  display_name        = var.compute_name

  shape_config {
    ocpus         = var.compute_cpus
    memory_in_gbs = var.compute_memory_in_gbs
  }

  create_vnic_details {
    subnet_id         = var.compute_subnet_id
    assign_public_ip  = true
  }

  metadata = {
    ssh_authorized_keys = file(var.compute_ssh_authorized_keys)
  } 

  preserve_boot_volume = false
  agent_config {
    #Optional
    is_management_disabled = false
    plugins_config {
      #Required
      desired_state = "ENABLED"
      name          = "Block Volume Management"
    }
  }
}


# Outputs
output "compute_id" {
  value = oci_core_instance.tf_compute.id
}

output "db_state" {
  value = oci_core_instance.tf_compute.state
}

output "compute_public_ip" {
  value = oci_core_instance.tf_compute.public_ip
}
3

oci_compute_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_compute_variables.auto.tfvars". The is the OCID of the compartment that will house the compute instance. 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 & Security > Compartments - Click on the compartment of interest. - Click the "Copy" link next to "OCID". The is the OCID of the subnet the compute instance 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. The is the OCID of the OS image the compute instance will be based upon. The image IDs can be found here .

Code/Command (click line numbers to comment):

1
2
3
4
5
6
compartment_id              = "ocid1.compartment.oc1..aaaaaaaa..."
compute_shape               = "VM.Standard.E2.1.Micro"
compute_name                = "obvm1"
compute_subnet_id           = "ocid1.subnet.oc1.uk-london-1.aaaaaaaa..."
compute_image_id            = "ocid1.image.oc1.uk-london-1.aaaaaaaa..."
compute_ssh_authorized_keys = "./myOracleCloudKey.pub"
4

Build the OCI Compute Instance

Initialize the working directory using the command. Use the command to test the execution plan. Use the command to create the OCI compute instance. Check the Oracle Cloud account to see the new compute instance in the compartment you chose. For more information see: - oci_core_instance - 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
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_core_instance.tf_compute will be created
  + resource "oci_core_instance" "tf_compute" {
      + availability_domain                 = "oVQK:UK-LONDON-1-AD-1"
      + boot_volume_id                      = (known after apply)
      + compartment_id                      = "ocid1.compartment.oc1..aaaaaaaa.."
      + dedicated_vm_host_id                = (known after apply)
      + defined_tags                        = (known after apply)
      + display_name                        = "obvm2"
      + fault_domain                        = (known after apply)
      + freeform_tags                       = (known after apply)
      + hostname_label                      = (known after apply)
      + id                                  = (known after apply)
      + image                               = (known after apply)
      + ipxe_script                         = (known after apply)
      + is_pv_encryption_in_transit_enabled = (known after apply)
      + launch_mode                         = (known after apply)
      + metadata                            = {
          + "ssh_authorized_keys" = <<-EOT
                ssh-rsa AAAAB3Nza...nElEbgK/ username@machine-name
            EOT
        }
      + preserve_boot_volume                = false
      + private_ip                          = (known after apply)
      + public_ip                           = (known after apply)
      + region                              = (known after apply)
      + shape                               = "VM.Standard.E2.1.Micro"
      + state                               = (known after apply)
      + subnet_id                           = (known after apply)
      + system_tags                         = (known after apply)
      + time_created                        = (known after apply)
      + time_maintenance_reboot_due         = (known after apply)

      + agent_config {
          + are_all_plugins_disabled = (known after apply)
          + is_management_disabled   = (known after apply)
          + is_monitoring_disabled   = (known after apply)

          + plugins_config {
              + desired_state = (known after apply)
              + name          = (known after apply)
            }
        }

      + availability_config {
          + recovery_action = (known after apply)
        }

      + create_vnic_details {
          + assign_public_ip       = "true"
          + defined_tags           = (known after apply)
          + display_name           = (known after apply)
          + freeform_tags          = (known after apply)
          + hostname_label         = (known after apply)
          + private_ip             = (known after apply)
          + skip_source_dest_check = (known after apply)
          + subnet_id              = "ocid1.subnet.oc1.uk-london-1.aaaaaaaa..."
          + vlan_id                = (known after apply)
        }

      + instance_options {
          + are_legacy_imds_endpoints_disabled = (known after apply)
        }

      + launch_options {
          + boot_volume_type                    = (known after apply)
          + firmware                            = (known after apply)
          + is_consistent_volume_naming_enabled = (known after apply)
          + is_pv_encryption_in_transit_enabled = (known after apply)
          + network_type                        = (known after apply)
          + remote_data_volume_type             = (known after apply)
        }

      + platform_config {
          + numa_nodes_per_socket = (known after apply)
          + type                  = (known after apply)
        }

      + shape_config {
          + gpu_description               = (known after apply)
          + gpus                          = (known after apply)
          + local_disk_description        = (known after apply)
          + local_disks                   = (known after apply)
          + local_disks_total_size_in_gbs = (known after apply)
          + max_vnic_attachments          = (known after apply)
          + memory_in_gbs                 = 1
          + networking_bandwidth_in_gbps  = (known after apply)
          + ocpus                         = 1
          + processor_description         = (known after apply)
        }

      + source_details {
          + boot_volume_size_in_gbs = (known after apply)
          + kms_key_id              = (known after apply)
          + source_id               = "ocid1.image.oc1.uk-london-1.aaaaaaaa..."
          + source_type             = "image"
        }
    }

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

Changes to Outputs:
  + compute_id        = (known after apply)
  + compute_public_ip = (known after apply)
  + 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.

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_core_instance.tf_compute will be created
  + resource "oci_core_instance" "tf_compute" {
      + availability_domain                 = "oVQK:UK-LONDON-1-AD-1"
      + boot_volume_id                      = (known after apply)
      + compartment_id                      = "ocid1.compartment.oc1..aaaaaaaa..."
      + dedicated_vm_host_id                = (known after apply)
      + defined_tags                        = (known after apply)
      + display_name                        = "obvm2"
      + fault_domain                        = (known after apply)
      + freeform_tags                       = (known after apply)
      + hostname_label                      = (known after apply)
      + id                                  = (known after apply)
      + image                               = (known after apply)
      + ipxe_script                         = (known after apply)
      + is_pv_encryption_in_transit_enabled = (known after apply)
      + launch_mode                         = (known after apply)
      + metadata                            = {
          + "ssh_authorized_keys" = <<-EOT
                ssh-rsa AAAAB3Nza...nElEbgK/ username@machine-name
            EOT
        }
      + preserve_boot_volume                = false
      + private_ip                          = (known after apply)
      + public_ip                           = (known after apply)
      + region                              = (known after apply)
      + shape                               = "VM.Standard.E2.1.Micro"
      + state                               = (known after apply)
      + subnet_id                           = (known after apply)
      + system_tags                         = (known after apply)
      + time_created                        = (known after apply)
      + time_maintenance_reboot_due         = (known after apply)

      + agent_config {
          + are_all_plugins_disabled = (known after apply)
          + is_management_disabled   = (known after apply)
          + is_monitoring_disabled   = (known after apply)

          + plugins_config {
              + desired_state = (known after apply)
              + name          = (known after apply)
            }
        }

      + availability_config {
          + recovery_action = (known after apply)
        }

      + create_vnic_details {
          + assign_public_ip       = "true"
          + defined_tags           = (known after apply)
          + display_name           = (known after apply)
          + freeform_tags          = (known after apply)
          + hostname_label         = (known after apply)
          + private_ip             = (known after apply)
          + skip_source_dest_check = (known after apply)
          + subnet_id              = "ocid1.subnet.oc1.uk-london-1.aaaaaaaa..."
          + vlan_id                = (known after apply)
        }

      + instance_options {
          + are_legacy_imds_endpoints_disabled = (known after apply)
        }

      + launch_options {
          + boot_volume_type                    = (known after apply)
          + firmware                            = (known after apply)
          + is_consistent_volume_naming_enabled = (known after apply)
          + is_pv_encryption_in_transit_enabled = (known after apply)
          + network_type                        = (known after apply)
          + remote_data_volume_type             = (known after apply)
        }

      + platform_config {
          + numa_nodes_per_socket = (known after apply)
          + type                  = (known after apply)
        }

      + shape_config {
          + gpu_description               = (known after apply)
          + gpus                          = (known after apply)
          + local_disk_description        = (known after apply)
          + local_disks                   = (known after apply)
          + local_disks_total_size_in_gbs = (known after apply)
          + max_vnic_attachments          = (known after apply)
          + memory_in_gbs                 = 1
          + networking_bandwidth_in_gbps  = (known after apply)
          + ocpus                         = 1
          + processor_description         = (known after apply)
        }

      + source_details {
          + boot_volume_size_in_gbs = (known after apply)
          + kms_key_id              = (known after apply)
          + source_id               = "ocid1.image.oc1.uk-london-1.aaaaaaaa..."
          + source_type             = "image"
        }
    }

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

Changes to Outputs:
  + compute_id        = (known after apply)
  + compute_public_ip = (known after apply)
  + 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_core_instance.tf_compute: Creating...
oci_core_instance.tf_compute: Still creating... [10s elapsed]
oci_core_instance.tf_compute: Still creating... [20s elapsed]
oci_core_instance.tf_compute: Still creating... [30s elapsed]
oci_core_instance.tf_compute: Still creating... [40s elapsed]
oci_core_instance.tf_compute: Still creating... [50s elapsed]
oci_core_instance.tf_compute: Still creating... [1m0s elapsed]
oci_core_instance.tf_compute: Still creating... [1m10s elapsed]
oci_core_instance.tf_compute: Still creating... [1m20s elapsed]
oci_core_instance.tf_compute: Still creating... [1m30s elapsed]
oci_core_instance.tf_compute: Creation complete after 1m37s [id=ocid1.instance.oc1.uk-london-1.anwgiljt...]

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

Outputs:

compute_id = "ocid1.instance.oc1.uk-london-1.anwgiljt..."
compute_public_ip = "XXX.XXX.XX.XX"
db_state = "RUNNING"

Comments (0)

Please to add comments

No comments yet. Be the first to comment!