Terraform basics for oracle cloud infrastructure(OCI)
Oracle Cloud infrastructure resources can be managed by console,API’s etc. Console is better used when creating,maintaining individual resource.
They are not suitable, if we want to launch,or want to change configuration of thousands of resources at a time.
For managing multiple resource at a time , we can use API’s ,which again require significant time programming the API’s.
Terraform is a tool that allows you to programmatically manage, version, and persist your IT infrastructure as “infrastructure as code.” Terraform uses declarative syntax to describe your infrastructure and then persist it in configuration files that can be shared, reviewed, edited, versioned, preserved, and reused.
Terraform compares current resource definition and the definition present in terraform config file and performs the actions required.
Terraform did not touch any other resources apart from those mentioned in the terraform config file during its plan,apply and destroy operation
First thing first , let us first install the Terraform on our local oci vm node. Terraform is a command line tool that you install locally. Locally can be anywhere really, including a VM on OCI.
NOTE: You do not have to run Terraform inside OCI in order to manage resources on OCI.
1.I have used a project on GitHub that provides a script for automated installation of Terraform.
git clone https://github.com/robertpeteuil/terraform-installer
for me during installation git was not working so I directly went to the url and downloaded the terraform_installler.sh script and run this on my VM.
[opc@parwezweb2 ~]$ ./terraform_installler.sh
Terraform Installer
Specify install directory (a,b or c):
(a) ‘~/bin’ (b) ‘/usr/local/bin’ as root (c) abort : a
Terraform Version 0.12.24 installed to /home/opc/bin
[opc@parwezweb2 ~]$
[opc@parwezweb2 ~]$
Once installed check the version to make sure it is installed correctly
[opc@parwezweb2 ~]$ terraform version
Terraform v0.12.24
Once installed you can start to download or create Terraform configuration files that define resources and have Terraform manage those resources.
After downloading the terraform software, we need to download the the provider plugins from terraform using terraform init command.
with provider details for oci like below .
[root@ip-172-31-37-152 oracle]# cat provider.tf
provider “oci” {
region = “us-ashburn-1”
}
Now we need to make Terraform OCI aware , how do we do that ?
We define a credential file with our OCI configuration which have auth details and tenancy details about OCI cloud accounts.
This is the variable file you will define to make sure Terraform understand
[opc@parwezweb2 provider]$ cat credential.tf
provider “oci”{
user=”ocid1.user.oc1..aaaaaaaai4datv2roc5txem43wuza46ra7titis7s4ifx5cafzysryphhrua”
fingerprint=”41:14:64:4a:96:c7:89:0a:e3:78:85:61:67:05:94:72″
key_file=”/home/opc/.oci/oci_api_key.pem”
tenancy=”ocid1.tenancy.oc1..aaaaaaaam2zqolqbbmdj24kt4vdozxmqftb32etffj4oqc554sa25udpsamq”
region=”us-ashburn-1″
}
NOTE : You can define both credential and provider in a single file or multiple file , depending upon your convenience. terraform init will only work if in any file you define the provider name eg :OCI
Now you can initialize Terraform with your tenancy details.
How does Terraform know which files it should even look at when I run this init command ?
It looks at all the files in the current, working directory that have an extension of .tf.
Whether you spread the resource definitions over multiple files in the same directory (with the extension .tf) or have them all in the same file, that does not matter . But as per manageability ease you try to keep separate directory . For example as below
Now we have a working environment with Terraform and the OCI provider set up . We will now play with some oci resource and will try creating and destroying the resource in our oci tenancy with the help of Terraform . We will create a simple object storage bucket named Terraform_bucket in my object storage .
[opc@parwezweb2 ~]$ ls -ltr
total 12
drwxrwxr-x. 3 opc opc 24 Apr 28 18:36 lib
-rwxr-xr-x. 1 root root 6711 May 1 21:57 terraform_installler.sh
drwxr-xr-x. 2 root root 26 May 1 21:57 provider_terafrm
drwxrwxr-x. 3 opc opc 57 May 1 21:58 bin
drwxrwxr-x. 4 opc opc 4096 May 1 22:56 new_provider
This are the available bucket in my oci object storage currently

I will create .tf file to create a object storage bucket using terraform .
$[opc@parwezweb2 new_provider] cat osbkct.tf
#the bucket to be managed by Terraform
resource “oci_objectstorage_bucket” “Terraform_bucket” {
compartment_id = “ocid1.tenancy.oc1..aaaaaaaam2zqolqbbmdj24kt4vdozxmqftb32etffj4oqc554sa25udpsamq”
name = “Terraform_bucket”
namespace = “idvcxgiklvcu”
}

I am giving the bucket creation file definition a name called osbkct.tf , with the help of this osbkct.tf file Terraform will create a OS bucket .
Right now we have two .tf file one is provider.tf which is config file to directs Terraform to know and access my tenancy and other one is osbkt.tf which instruct Terraform what to do , on this case create a OS bucket .
First we will run Terraform plan to check if it is referring to the correct .tf file to create a os bucket or not , plan will details everything the Terraform will be doing once you run the actual terraform apply to create the os bucket . you can think plan as a preview .

Since the output is telling me what it will do , it will change one resource i.e it will create a bucket named Terraform_bucket . which is exactly what I want . I will run the apply command instructing Terraform to actually create a os bucket.
# terraform apply

You can also check the resource status using terraform show after apply has been done, just to recheck everything worked well

Now go to your OCI account and check if the bucket with name Terraform_bucket is create or not

Boom !!!! The bucket is created
Similarly you can delete this bucket by running Terraform destroy. Please note , I will only execute terraform destroy and it will only destroy the bucket which is mentioned in the .tf file and it will not touch any other bucket in OCI object storage .

Check your oci OS

924 total views, 2 views today