8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux
Terraform : Oracle Cloud Infrastructure (OCI) Compute Instance
This article describes how to create a compute instance on Oracle Cloud Infrastructure (OCI) using Terraform.
This script only builds the base compute instance. It doesn't include additional disks or configuration.
- Create Working Directory
- oci_compute.tf
- oci_compute_variables.auto.tfvars
- Build the OCI Compute Instance
Related articles.
Create Working Directory
Create a new working directory and switch to that directory.
mkdir \git\oraclebase\terraform\oci\oci_compute cd \git\oraclebase\terraform\oci\oci_compute
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_compute.tf
Create a file called "oci_compute.tf" with the following contents.
# 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 } # 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 }
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.
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".
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"
The compartment_id
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 > Compartments
- Click on the compartment of interest.
- Click the "Copy" link next to "OCID".
The db_subnet_id
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 compute_image_id
is the OCID of the OS image the compute instance will be based upon. The image IDs can be hounr here.
Build the OCI Compute Instance
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_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.
Use the terraform apply
command to create the OCI compute instance.
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"
Check the Oracle Cloud account to see the new compute instance in the compartment you chose.
For more information see:
Hope this helps. Regards Tim...