Uncategorized

Setup Terraform for OCI

How to setup Terraform in your local VM for provider OCI.

    1. Download terraform from hashiCorp site,  based on your OS requirements https://www.terraform.io/downloads.html
    2. Unzip the downloaded file , zip will contain only 1 file.
    3. If you are using mac , move the terraform file to your /usr/local/bin path, so its convenient for you to invoke terraform program from anywhere.
            • which terraform
              /usr/local/bin/terraform
            • terraform --version
              Terraform v0.12.21
              + provider.oci v3.63.0
    4. You need to generate public PEM file with a password, and this public PEM will be added in OCI console under your profile settings. You can either create this file in your .ssh folder or as needed. This is needed for authenticating against your tenancy.
        • Generate Private PEM key

          openssl genrsa -out ~api_access_to_oci_key.pem -aes128 2048
          Enter pass phrase for {pathtokeyfilename}:Verifying – Enter pass phrase for {pathtokeyfilename}:

        • Generate Public PEM Key

          openssl rsa -pubout -in ~api_access_to_oci_key.pem -out ~api_access_to_oci_pub.pem
          Enter pass phrase for {pathtokeyfilename}:writing RSA key

        • Cat the public key file that you generated above
        • Login to OCI console , with your username.
        • Click on your profile and go to API Keys.
        • Add your public key to API Keys, fingerprint will be generated. keep a note of it
    5. Create terraform variables, in your bash profile.
        • #Terraformexport TF_VAR_tenancy_ocid=ocid1.tenancy.oc1..aaaaaaaafoosexport TF_VAR_compartment_ocid=ocid1.compartment.oc1..aaaaaaaaexport TF_VAR_user_ocid=ocid1.user.oc1..aaaaaaaanlexport TF_VAR_region=us-ashburn-1export TF_VAR_fingerprint=e7:xx:ef:01:06:f9:xx:yy:dtt01:ww:xx:19:ww:qqexport TF_VAR_private_key_path=~api_access_to_oci_key.pemexport TF_VAR_private_key_password=passwordofprivatekeyfile

        • Source your new/updated profile file. Example – source ~/.bash_profile

           

    6. Create terraform directory and now lets define the oci provider configuration by testing a small code.In terraform directory,   create a file called provider.tf with below content.
        • cat provider.tf
          variable “tenancy_ocid” {}
          variable “user_ocid” {}
          variable “fingerprint” {}
          variable “region” {}
          variable “private_key_path” {}
          variable “private_key_password” {}

          provider “oci” {
          tenancy_ocid = “${var.tenancy_ocid}”
          user_ocid = “${var.user_ocid}”
          fingerprint = “${var.fingerprint}”
          region = “${var.region}”
          private_key_path = “${var.private_key_path}”
          private_key_password = “${var.private_key_password}”
          }

    7.  Initialize your terraform with command terraform init.
    8. terraform apply

 

 

 

Leave a comment