Terraform for Oracle Cloud infrastructure

This post provide steps for downloading and installing both Terraform and the Oracle Cloud Infrastructure Terraform provider.

Terraform Overview

Terraform is “infrastructure-as-code” software that allows you to define your infrastructure resources in files that you can persist, version, and share. These files describe the steps required to provision your infrastructure and maintain its desired state; it then executes these steps and builds out the described infrastructure.

Infrastructure as Code is becoming very popular. It allows you to describe a complete blueprint of a datacentre using a high-level configuration syntax, that can be versioned and script-automated, Terraform can seamlessly work with major cloud vendors, including Oracle, AWS, MS Azure, Google, etc

Download and Install Terraform

In this section, i will show and explain how to download and install Terraform on  your laptop/PC Host Operating System, you can download using the below link :-

Terraform Download
  • After you download the terraform, Unzip the Terraform to whatever location you want to run it from. Then, add that location to your OS PATH.
    • Windows : By adding to Path –> environment variables
    • Linux : Profile –> export Path

You can check by run the CMD and check the version:-

Check Terraform commands

Download the OCI  Terraform Provider

Prerequisites:-

  • OCI User credentials that has  sufficient permission to  execute a Terraform plan.
  • Required keys and Oracle Cloud Infrastructure IDs (OCIDs).
  • The correct Terraform binary file for your operating system

Installing and Configuring the Terraform Provider

In my personal opioion about this section (The title of the section same as Oracle Documentation) I found it wrong, i worked with Terraform in different cloud vendor, AWS, Azure and OCI so Terraform will recognize it and automatically install the provider for you.

to do that, all of you have to do is create folder , then create file “variables.tf” that only contains

provider "oci" {<br>}

and run terraform command

terraform init

Now Let’s Talk small examples about OCI and Terraform, First you have to read “Creating Module” to understand the rest of this post here.

I will upload to my Github here Small Sample for OCI Terraform to allow you underatand how we can use it instead of the GUI and make it easy for you.

I upload to my github example of Terraform for OCI Proiver, In the this example i will create autonomous database but not using the GUI,

to work with Terraform, you have to understand what is the OCI Provider and the parameters of it.

The Terraform configuration resides in two files: variables.tf (which defines the provider oci) and main.tf (which defines the resource).

For more terraform examples here

Configuration File Requirements

Terraform configuration (.tf) files have specific requirements, depending on the components that are defined in the file. For example, you might have your Terraform provider defined in one file (provider.tf), your variables defined in another (variables.tf), your data sources defined in yet another.

Some of the examples for Terraform files here

Provider Definitions

The provider definition relies on variables so that the configuration file itself does not contain sensitive data. Including sensitive data creates a security risk when exchanging or sharing configuration files.

To understand more about provider read here

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}"
}

Variable Definitions

Variables in Terraform represent parameters for Terraform modules. In variable definitions, each block configures a single input variable, and each definition can take any or all of three optional arguments:

  • Type (Optional): Defines the variable type as one of three allowed values: string, list, and map. If this argument is not used, the variable type is inferred based on default. If no default is provided, the type is assumed to be string
  • Default (Optional) : Sets the default value for the variable. If no default value is provided, the caller must provide a value or Terraform throws an error.
  • Description (Optional) : A human-readable description of the variable.

More information here

For example

variable "AD" {
    default     = "1"
    description = "Availability Domain"
}

Output Configuration

Output variables provide a means to support Terraform end-user queries. This allows users to extract meaningful data from among the potentially massive amount of data associated with a complex infrastructure.

More information here

Example

output "InstancePublicIPs" {
value = ["${oci_core_instance.TFInstance.*.public_ip}"]
}

Resource Configuration

Resources are components of your Oracle Cloud Infrastructure. These resources include everything from low-level components such as physical and virtual servers, to higher-level components such as email and database providers, your DNS record.

For more information here

One of the example :-

resource "oci_core_virtual_network" "vcn1" {
   cidr_block = "10.0.0.0/16"
   dns_label = "vcn1"
   compartment_id = "${var.compartment_ocid}"
   display_name = "vcn1"
}

Data Source Configuration

Data sources represent read-only views of existing infrastructure intended for semantic use in Terraform configurations, for example Get DB node list

data "oci_database_db_nodes" "DBNodeList" {
  compartment_id = "${var.compartment_ocid}"
  db_system_id = "${oci_database_db_system.TFDBNode.id}"
}

Another example, Gets the OCID of the first (default) vNIC


data "oci_core_vnic" "DBNodeVnic" {
  vnic_id = "${data.oci_database_db_node.DBNodeDetails.vnic_id}"
}

Follow me on GitHub here

Cheers

Osama