Riaan's SysAdmin Blog

My tips, howtos, gotchas, snippets and stuff. Use at your own risk!

OCIOracle Bare Metal Cloud ServicesTerraform

Compute Instance in OCI using terraform

Begin Update 9/26/17.

It is possible to reference the subnet as follow:

subnet_id = "${oci_core_subnet.PrivSubnetAD1.id}"

My problem and workaround originally is because I am using modules. I would prefer modules so I can organize and subdivide better but it caused above to not work. Plus subdividing the work may cause more issues around losing access to variables/references.
End Update 9/26/17

Most likely there is a better way to do this but since I spent some time on it I am jotting down my notes. When creating compute instances with terraform in Oracle Cloud Infrastructure(Oracle Bare Metal Services) you have to specify the subnet_id. The id or ocid as called in OCI is a long unique string.

So if you are looking at automating the terraform build you may struggle with not knowing subnet_id when creating a compute instance. As I said there may be better ways to do this and maybe the AWS plugin for terraform handles this already. I did this with the OCI plugin and came up with the below script and using some custom API calls to create terraform variables of the subnets. Just showing the bash script for some ideas on flow and how it glues together. The terraform source and api calls not shown here.

#!/bin/bash

build_root="/home/rrosso/.terraform.d/MY-PROTO"
api_snippets_location="/home/rrosso/oraclebmc"
terraform_bin="/home/rrosso/.terraform.d/terraform"

function pause(){
   read -p "$*"
}

## Check if environment variables are set. Make very sure correct tenancy, compartment etc
## Or if hard coding these look in main.tf files for correct tenancy, compartment etc
env | grep TF_

pause "Press [Enter] key if Variables look ok and if ready to proceed with networking build"

cd "$build_root/networking"
$terraform_bin apply

pause "Press [Enter] key if networking build went ok and ready to generate a list of subnet ocid's"

cd "$api_snippets_location"
python get_subnet_ocids.py -t ocid1.tenancy.oc1..<cut_long_number_here> -c MYPROTO -v DEV >> $build_root/compute/webservers/variables.tf 
python get_subnet_ocids.py -t ocid1.tenancy.oc1..<cut_long_number_here> -c MYPROTO -v DEV >> $build_root/compute/bastions/variables.tf 

pause "Press [Enter] key if variables.tf looks ok with new subnet ocid's and ready to proceed building compute instances"

cd "$build_root/compute"
$terraform_bin apply

admin

Bio Info for Riaan