One of my old colleague whom I had pleasure to work with many years ago asked recently if there would be guide on getting a database running on Oracle Cloud Infrastructure with Terraform using Infrastructure as Code.

That led me to an idea that I could write such guide and describe each step why it’s required and what are the key details on that service.

Different areas on this 10-part series will be:

  • OCI provider and Terraform setup
  • Compartments
  • VCN
  • Internet & NAT Gateway
  • Routetables
  • Subnets
  • Securitylists
  • Compute Instance
  • Database Instance
  • Wrapping it all up

Each post will have the required Terraform code and finally in the last post I will combine everything together so you can use it as a baseline. I’ll post the source code as well in the wrap-up.

Contrary to normal where I use Terraform modules this setup will not use modules but just a single folder having all necessary files in place to make the overall setup easier to understand.

After this series you will have a basic network with database running on private network and a compute instance to be used as a jump server in the public network which has access to the private network as well.

Setting up Terraform and OCI provider

This one is the easy part! Unless you are using Oracle Linux which automatically includes Terraform in the repository you will need to download Terraform from https://www.terraform.io/downloads.html .

Earlier you also had to download the OCI provider but now since OCI is an official provider it is included without further steps. Documentation and setup steps are described in the Terraform OCI provider page https://www.terraform.io/docs/providers/oci/index.html.

Creation of API key is described in detail here. On the same link you can see how to assign it for your user.

In short (and copied from above documentation) the values you need to setup as environment variables are:

  • tenancy_ocid – Can be found from the console under Administration->Tenancy Information
  • user_ocid – OCID for your user found under Indentity -> Users -> <My Username>
  • private_key_path – Location of created key on your machine
  • fingerprint – copy the fingerprint section from the added API key on your users info page
  • region – The region where you will be operating (I use eu-frankfurt-1)

On windows you can set these using setx command or by opening Environment variables under control panel and setting them.

Once these are set we can set the variables to be used in our Terraform file. I will have three files in this project – main.tf, variables.tf and outputs.tf.

In the main.tf I define environment variables in the provider section:

provider "oci" {  
tenancy_ocid = "${var.tenancy_ocid}"  
user_ocid = "${var.user_ocid}"  
fingerprint = "${var.fingerprint}"  
private_key_path = "${var.private_key_path}"  
region = "${var.region}"} 

These variables come from variables.tf where they are referenced to come from the environment variables we have set.

variable "tenancy_ocid" {} // Your tenancys OCID
variable "user_ocid" {} // Your user's OCID
variable "fingerprint" {} // Fingerprint for the user key, can be found under user in console
variable "private_key_path" {} // Where your private key is located on the server you are running these scripts
variable "region" {} // Which region is used in OCI eg. eu-frankfurt-1 


Now all the necessary pre-requisistes have been done and we can start creating the resources in the next parts. Part 2 will discuss creation of compartments.

2 thoughts on “Series – Get your database running with Terraform part 1: Setup”

Leave a Reply

Your email address will not be published. Required fields are marked *