Following document will provide detailed steps to setup Terraform for OCI on Oracle Linux server and create a VCN in OCI compartment.
Terraform Installation:
OL7:
You can setup using YUM or follow manual steps provided below.
- Using YUM:
sudo yum install -y terraform terraform-provider-oci
- Manual Steps:
- Download terraform and terraform-provider-oci RPM’s from below URL’s.
https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/terraform-0.12.20-1.el7.x86_64.rpm
https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/getPackage/terraform-provider-oci-3.61.0-1.el7.x86_64.rpm - Run following commands to install RPM’s
sudo rpm -ivh terraform-0.12.20-1.el7.x86_64.rpm
sudo rpm -ivh terraform-provider-oci-3.61.0-1.el7.x86_64.rpm
OL6:
In case of OL6, we need to follow manual steps to setup Terraform, and Terrform Provider for OCI.
- Manual Steps:
- Setup Terraform:
- Login to your machine and switch to home directory:
cd ~ - Download Terraform Binary
wget https://releases.hashicorp.com/terraform/0.12.20/terraform_0.12.20_linux_amd64.zip - Unzip Binary:
unzip terraform_0.12.20_linux_amd64.zip - Remove Binary:
rm terraform_0.12.20_linux_amd64.zip
- Login to your machine and switch to home directory:
- Setup Terraform OCI Provider:
- Login to your machine and switch to home directory:
cd ~ - Create directory for plugins:
mkdir -p .terraform.d/plugins - Switch to newly created directory:
cd .terraform.d/plugins/ - Download Terraform OCI Provider Binary:
wget https://releases.hashicorp.com/terraform-provider-oci/3.61.0/terraform-provider-oci_3.61.0_linux_amd64.zip - Unzip Binary:
unzip terraform-provider-oci_3.61.0_linux_amd64.zip - Remove Binary:
rm terraform-provider-oci_3.61.0_linux_amd64.zip
- Login to your machine and switch to home directory:
- Add Terraform home to Path
export TF_HOME=~
export PATH=$TF_HOME:$PATH
Note: You can make it permanent by adding it to ~/.bashrc file and then source it. - Run below command to test Terraform setup
terraform version
OCI Console Setup:
- Setup API Signing Key:
- Login to host and run following commands to generate API signing key for OCI user.
- Go to home directory and create
cd ~ - mkdir ~/.oci
openssl genrsa -out ~/.oci/oci_api_key.pem -aes128 2048
Enter Passphrase: - Secure private key:
chmod go-rwx ~/.oci/oci_api_key.pem
Get Finger print - Generate Fingerprint:
openssl rsa -pubout -outform DER -in ~/.oci/oci_api_key.pem | openssl md5 -c
This fingerprint value required for Terraform OCI provider. - Generate Public key:
openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem - Copy Public key:
cat ~/.oci/oci_api_key_public.pem
- Add credentials to OCI console
- Login to the respective OCI Console:
- Ashburn Console: https://console.us-ashburn-1.oraclecloud.com/
- Go to Governance and Administration --> Identity --> Users
- Click on your username (email id)
- Click on Add Public Key and paste your key from oci_api_key_public.pem. And add it.
- Please note the Fingerprint shown on the console.It should match the Fingerprint generated during API signing key generation.
- Login to the respective OCI Console:
Terraform OCI Provider Setup:
- Create Variable File:
- Login to host (where terraform & terraform OCI provider already installed) and create a directory for terraform scripts.
mkdir –p ~/tfscripts - Switch to newly created directory
cd ~/tfscripts - Create a file named terraform.tfvars and add below content.
tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaaqms4qy6kxsxsdoaocxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" user_ocid = "ocid1.user.oc1..aaaaaaaag4jmbpa2pg5pzea4qsxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" fingerprint = "43:3f:3b:ce:d4:49:31:9c:3f:ef:2a:84:9f:eb:7b:3d" private_key_path = "~/.oci/oci_api_key.pem" compartment_ocid = "ocid1.tenancy.oc1..aaaaaaaaqms4qy6kxsxsdoaocxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" region = "us-ashburn-1" private_key_password = "<password>"
- tenancy_ocid:
- Login to OCI console and click on top right user icon and select Tenancy name to find tenancy OCID
- user_ocid:
- Login to OCI console and Go to Governance and Administration --> Identity --> Users. Click on your username to find user OCID
- compartment_ocid:
- Login to OCI console and Go to Governance and Administration --> Identity --> Compartments. Click on your username to find user OCID
- fingerprint:
- Copy the fingerprint generated during API signing key generation.
- private_key_path:
- location of Private Key
- private_key_password:
- Private Key passphrase
- Login to host (where terraform & terraform OCI provider already installed) and create a directory for terraform scripts.
- Create Provider File:
- Create a file named provider.tf with below content.
variable "tenancy_ocid" { } variable "user_ocid" { } variable "fingerprint" { } variable "region" { } variable "private_key_path" { } variable "private_key_password" { } variable "compartment_ocid" { } 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 }
- Initialize Terraform
- Run below command to initialize Terraform and load OCI plugin
terraform init
- Run below command to initialize Terraform and load OCI plugin
- Validate Terraform configuraiton
- Run below command to validate Terraform setup
terraform validate
- Run below command to validate Terraform setup
Create VCN in OCI:
Following steps will help you to create VCN in OCI using Terraform Provider.
- Switch to terraform scripts directorycd ~/tfscripts
- Create a file named vcn.tf and add below content
resource "oci_core_vcn" "vcn1" { cidr_block = "10.0.0.0/16" dns_label = "vcn1" compartment_id = var.compartment_ocid display_name = "vcn1" } output "vcn_id" { value = oci_core_vcn.vcn1.id }
- Run below command to view the changes to OCI compartment
terraform plan - Apply changes and VCN will be created in OCI compartment.
terraform apply
- Destroy to delete VCN from your compartment.
terraform destroy
Additional References:
- https://www.terraform.io/docs/providers/oci/index.html
- https://github.com/terraform-providers/terraform-provider-oci/tree/master/examples
- https://www.terraform.io/downloads.html
- https://yum.oracle.com/repo/OracleLinux/OL7/developer/x86_64/index.html
- https://www.terraform.io/docs/plugins/basics.html#installing-a-plugin
- https://www.terraform.io/docs/providers/oci/guides/version-2-upgrade.html