Skip links

Building OCI Compute Nodes via Code

Over the last couple of posts, we’ve been showing how to use the OCI provider and upgrading the associated OCI modules.  In this post, we’ll show you how to use the OCI modules that were upgraded to build OCI compute node(s) through code.  The node(s) that will be built are just standard Oracle Linux 7.8 node(s); there are no additional software installed on the node(s) but gives you the sense of how easy it is to build a compute node(s) on Oracle Cloud Infrastructure (OCI).

In the post on how to upgrade Terraform OCI Modules (here), we showed you how to pull the modules from the Terraform Registry and then how to upgrade them.  These same steps have to be performed, but we are going to go a step further in this post.  This post will focus in on the module and how to use it after it has been upgraded to 0.12 format.

Terraform Registry – OCI Module

The OCI module that is going to reviewed is the Compute-Instance module.  This module takes up-to twenty-three (23) inputs and outputs five (5) data points. 

Inputs – Required

The inputs that are listed here are required and used to pass values to the input variables in the variables.tf file for the module.

compartment_ocid  – OCID where VCN/Compute-Instance will be created
source_ocid  – OCID of the image or a boot volume to use, depending on the value of source_type.  Default source_type is “image”
ssh_authorized_keys  – Public SSH keys to be included in the ~/.ssh/authorized_keys file.  File must be *.pub format.  *.pem format doesn’t work
subnet_ocids  – OCIDs (one or more) of the subnets in which the instance primary VNIC is created.  Must be the OCID for the public subnet is wanting public access.

The optional inputs can be feed as well, but referent to the Terraform Registry for the module for these items (here).  Additionally, you could update the variables.tf file with required/optional information.

Base Code

The Terraform Registry give you the base code that is required for this module to work, as follows:

module "compute-instance" {
    source = "oracle-terraform-modules/compute-instance/oci"
    version = "2.0.1"
    # insert the 4 required variables here
}

Notice that there is comment line that says “insert the 4 required variables”.  These four (4) required variables will provide the input information needed to build a base compute-instance.  With the supplied variables, the base cod would look similar to this:

module "compute-instance" {
    source = "oracle-terraform-modules/compute-instance/oci"
    version = "2.0.1"

    #Required Info
    compartment_ocid    = "ocid1.compartment.oc1..aaaaaaaade5bxtniugmwuiynso…………………qu3lzw2xybym4svyhq"
    source_ocid         = "ocid1.image.oc1.iad.aaaaaaaawtb4qxiwri5z2qjeey…………………w2e2c2jevmthva"
    ssh_authorized_keys = "~/.ssh/id_rsa.pub"
    subnet_ocids        = ["ocid1.subnet.oc1.iad.aaaaaaaamlgotv3goqjfihx……………………nsvwoqhqmouda"]
}

Notice that the required variables are taking sting values.  For compartment_ocid and source_ocid, we are providing the OCID for the compartment where the node(s) plus VCN are created. The source_ocid is the OCID for the linux image that is available in the region that we are using (us-ashburn-1) (here).  The information for the ssh_authorized_keys is actually the directory path + file name of the public key needing to be used.  Lastly, the subnet_ocids is an list of OCIDs that are used to provide network access/IP for the node(s).  In our case, the VCN-Subnet is a public subnet and will provide public IP address to access the node(s) once built.

This is the bare minimum that is needed to build a compute-instance on the OCI backend using Terraform.  

Extending Base Code

Before building any nodes, this module can be extended with the optional items. As previously mentioned, not going to spend a lot of time on the optional items for the module.  In this case, though we wanted to provided a bit more details on specifics needed for the compute-instance(s). The optional items we decided to use were:

instance_count  – Number of instances to launch
shape  – The shape of the instance
instance_display_name  – Name of the instance

By using these optional variables, we are able to specify how many node(s) to spin up, what size machines/shapes should be used, and the name of the machine that will be displayed. If we have more than one node built, then that name will be post-fixed with the instance_count for that node.  The module code looks similar to this:

module "compute-instance" {
    source = "oracle-terraform-modules/compute-instance/oci"
    version = "2.0.1"

    #Required Info
    compartment_ocid    = "ocid1.compartment.oc1..aaaaaaaade5bxtniugmwuiynso…………………qu3lzw2xybym4svyhq"
    source_ocid         = "ocid1.image.oc1.iad.aaaaaaaawtb4qxiwri5z2qjeey…………………w2e2c2jevmthva"
    ssh_authorized_keys = "~/.ssh/id_rsa.pub"
    subnet_ocids        = ["ocid1.subnet.oc1.iad.aaaaaaaamlgotv3goqjfihx……………………nsvwoqhqmouda"]

    #Optional Info
    instance_count = "1"
    shape = "VM.Standard2.1"
    instance_display_name = "Test-Linux"
}

Outputs

After building the node(s) we wanted to ensure that we knew the ID, private IP, and public IP of the node(s) that have been built.  The compute-instance module provides these exact items as outputs. By updating our code, we can list all this information simply in an output statement.  See the following code:

output "compute_info” {
     value = [
          "ID", module.compute-instance.instance_id,
          "Private", module.compute-instance.private_ip,
          "Public", module.compute-instance.public_ip
    ]
}

Notice that we are specifying this in a single return value.  Once the node(s) are built, it will provide us with the OCID for the ID of the instance(s), the private IP, and the public IP for each of them.

Running

With the code written and ready to use, simply run the terraform files as you would any other terraform scripts.  This will pull any updates needed for the module, plan the build and then apply.  Once it is applied, you will see an OCI compute-instance built within OCI under Compute -> Instances.  Remember to make sure you select the correct compartment to see the node(s).

Outputs

compute_info = [
"ID",
[
[
"ocid1.instance.oc1.iad.anuwcljsxbdlgqicurefoz4n……………….otio7ck7nbcfwiixqa",
],
],
"Private",
[
[
“10.x.x.8",
],
],
"Public",
[
[
“193.122.139.x",
],
],
]

oci_terraform_compute_instance.png

Summary

In this post, we took a look at how to use the OCI compute-instance module for Terraform to build a compute node within OCI.  After understanding what the module does it is pretty easy to build Linux machine in the OCI cloud platform.

Enjoy!!!

Leave a comment