Oracle Cloud and Terraform
Tags: cloud, terraform, oracle
Published: 2021-07-19 13:24:07

Of the Cloud providers, Oracle remains largely unknown. This is unfortunate, as I pointed out in a [previous post](https://dev.to/muckitymuck/oracle-cloud-free-almost-forever-2odo).  OCI is in high demand from employers and they offer a lot for the Always Free Tier to get started.
(Free VMs, Anyone?)
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbrx4mdteh0jep7ums5j.png)
And if you made an Instance you will notice buttons that reference Terraform configs automatically:
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ig2u0s3ykzagkzxauvr3.png)
 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xe91kl73r7deqzqe7wo.png)
 
Unzip it and move it here:
```
sudo mv terraform /usr/local/bin
```
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
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxtklhp2xiohdra8gxen.png)

And in your $HOME folder you will make a directory for your provider information. 
```
mkdir tf-provider
```
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>"
}
```
Now comes the fun part.  You just put in a few commands to automate the creation process.
```
terraform init
```
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
```
This will output the changes you want to make without actually doing real changes yet.  A Compartment might look something like this:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx3cvqo3zcmwj8bexynq.png)
 
 Finally, we do an apply:
```
terraform apply
```
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.
```
Cleaning up is just as easy. You use a literal Destroy command.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9s0qstq6c9qtqyogqvnb.png)
 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rgzm9p5943jrr7tlyot.png)
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