DEV Community

muckitymuck
muckitymuck

Posted on

Oracle Cloud and Terraform

Of the Cloud providers, Oracle remains largely unknown. This is unfortunate, as I pointed out in a previous post. OCI is in high demand from employers and they offer a lot for the Always Free Tier to get started.
(Free VMs, Anyone?)
image
And if you made an Instance you will notice buttons that reference Terraform configs automatically:
image
Oracle Cloud was apparently built out with Devops tools in mind. And if there is one thing I love about being an end user, it's being spoiled.

In this post, I will preview for you how to get started with Terraform on Oracle Cloud. I will be referencing Oracle's Docs often because they built out robust steps to get basic resources out in a quick way.
https://docs.oracle.com/en-us/iaas/developer-tutorials/tutorials/home.htm

If you have Linux, you are in luck because that is the correct platform for this. If you are running Windows, you will need the WSL and its annoying installation process:
https://docs.microsoft.com/en-us/windows/wsl/install-win10
It requires some restarts so be prepared.

We will be running Terraform out of the Linux terminal.

Download the Terraform files:
https://releases.hashicorp.com/terraform/0.13.1/terraform_0.13.1_linux_amd64.zip
image

Unzip it and move it here:

sudo mv terraform /usr/local/bin
Enter fullscreen mode Exit fullscreen mode

Generate some RSA keys to your $HOME folder:

mkdir $HOME/.oci
openssl genrsa -out $HOME/.oci/<your-rsa-key-name>.pem 2048
chmod 600 $HOME/.oci/<your-rsa-key-name>.pem
openssl rsa -pubout -in $HOME/.oci/<your-rsa-key-name>.pem -out $HOME/.oci/<your-rsa-key-name>_public.pem
cat $HOME/.oci/<your-rsa-key-name>_public.pem
Enter fullscreen mode Exit fullscreen mode

And then add the keys to your Oracle Cloud:

Add the public key to your user account.
From your user avatar, go to User Settings.
Click API Keys.
Click Add Public Key.
Select Paste Public Keys.
Paste value from previous step, including the lines with BEGIN >PUBLIC KEY and END PUBLIC KEY
Click Add.

I am just going to cap this list of info you need to gather and keep on hand because you will be constantly plugging them into configs:
image

And in your $HOME folder you will make a directory for your provider information.

mkdir tf-provider
Enter fullscreen mode Exit fullscreen mode

And in that dir you will make a provider.tf file. In that file plug in the info you gathered from above:

provider "oci" {
  tenancy_ocid = "<tenancy-ocid>"
  user_ocid = "<user-ocid>" 
  private_key_path = "<rsa-private-key-path>"
  fingerprint = "<fingerprint>"
  region = "<region-identifier>"
}
Enter fullscreen mode Exit fullscreen mode

Now comes the fun part. You just put in a few commands to automate the creation process.

terraform init
Enter fullscreen mode Exit fullscreen mode

If it is successful, you will notice a .terraform directory is now in the $HOME folder. This shows you are able to make infrastructure changes from here.

terraform plan
Enter fullscreen mode Exit fullscreen mode

This will output the changes you want to make without actually doing real changes yet. A Compartment might look something like this:
image

Finally, we do an apply:

terraform apply
Enter fullscreen mode Exit fullscreen mode

If this is your first resource, you shouldn't see anything. If you do, don't sweat it.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Enter fullscreen mode Exit fullscreen mode

Cleaning up is just as easy. You use a literal Destroy command.
image
The full progression steps to build a full out Infrastructure is probably best described like they do the Oracle Docs:
Provider(Cloud identity) -> Compartment -> Compute(VM Instances) -> VCN(Virtual Cloud Network) -> Infrastructure

The best way to use Terraform is to contain the projects in their own folders as I did above. The building blocks of a project are the .tf files that document settings.
image
The Compartment resource is the simplest and easiest to

Your provider.tf will contain info to access your Oracle Cloud account. Compartment.tf is resource specific file. For other resources they will carry the resource type File name with information like, Name, ID, and Description in the file.
Lastly the .tfstate file is the State that the real world resources are recorded as. By default, this file is locally stored, but it can be stored remotely for team environments.

And that is the quick tour of Terraform on Oracle Cloud. OC really put the effort in to integrate Terraform and make Infrastructure a snap to manage.

This was a blast to learn about and would recommend anyone interested in Cloud to give Oracle Cloud a trial run. They make it easy and the Always Free tier is a license to play without breaking the bank.

See you Cloud Cowboy

Top comments (0)