DEV Community

Cover image for Using Terraform to Deploy an OCI Compartment (Easy)
Faris Durrani
Faris Durrani

Posted on • Edited on

7

Using Terraform to Deploy an OCI Compartment (Easy)

Terraform is a devops tool that programmatically deploys cloud resources in a predictable, declarative way. It is a part of the Infrastructure as Code (IaC) environment that aims to safely create, change, and improve infrastructure using APIs without resorting to using the online console.

This is a guide to show an example usage of Terraform with regards to the Oracle Cloud Infrastructure (OCI). Substantially, how to deploy an OCI Compartment.

How to Deploy an OCI Compartment

1. Install Terraform

Follow the official installation page to install Terraform on your machine.

2. Configure the OCI Provider profile

To deploy OCI resources, you need access to manage the resources from your machine. This can be achieved using an API Key. To complete this step, see Setting up the OCI Configuration File using API Keys.

3. Create a new directory, e.g., terraform-test/

4. Declaring the OCI resources

Your final file tree should look like this once this step is completed:

📦terraform-test
 ┣ 📜compartment-vars.tf
 ┣ 📜compartment.tf
 ┣ 📜provider.tf
 ┣ 📜terraform.tf
 ┗ 📜terraform.tfvars
Enter fullscreen mode Exit fullscreen mode

In the file terraform.tf, we shall declare the provider, namely OCI:

# terraform.tf
terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = ">= 6.0.0"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

In the file provider.tf, we declare the profile to be used. This is required if you have multiple OCI profiles configured in ~/.oci/config and the profile in question is not DEFAULT.

# provider.tf
provider "oci" {
  config_file_profile = "DEFAULT"
}
Enter fullscreen mode Exit fullscreen mode

In compartment.tf, we declare the actual resources we want to deploy.

In this example, we want to deploy an empty compartment inside another existing compartment (which may be the tenancy itself) with the name in the format COMP-MyCompartmentName-HHmmss.

# compartment.tf
locals {
  name_prefix = "COMP"
}

resource "oci_identity_compartment" "my_new_compartment" {
  #Required
  compartment_id = var.compartment_id
  description    = var.description
  name           = format("%s-%s", local.name_prefix, var.name)
  #Optional
  # defined_tags  = var.defined_tags
  enable_delete = true
  # freeform_tags = var.freeform_tags
}
Enter fullscreen mode Exit fullscreen mode

Notice we have a few Terraform variables to fill.

In compartment-vars.tf, we declare the variables:

# compartment-vars.tf
#Required
variable "compartment_id" {
  description = "Compartment OCID"
  type        = string
}
variable "description" {
  description = "Compartment Description"
  type        = string
}
variable "name" {
  description = "Compartment Name"
  type        = string
}
Enter fullscreen mode Exit fullscreen mode

And finally in terraform.tfvars, we initialize the variables:

# terraform.tfvars
compartment_id = "ocid1.compartment.oc1..aaaaaaaakbr7t5yqjirrmwm3ycg5jj4hytyaaparentcompartmentrewkhflkhwiuedh"
description    = "Test Compartment Description"
name           = "MyCompartment1"
Enter fullscreen mode Exit fullscreen mode

where:

  • compartment_id is the OCID of the compartment that will contain this new compartment. If the new compartment is a root compartment, put the tenancy OCID found in Profile Picture > Tenancy

Note: using the tfvars file is after all optional if you choose to hardcode the values in compartment.tf

5. Deploy

First, we initialize and validate the files:

terraform init
terraform fmt
terraform validate
Enter fullscreen mode Exit fullscreen mode

Then, we write the plan:

terraform plan --out="plan.out"
Enter fullscreen mode Exit fullscreen mode

And apply it:

terraform apply "plan.out"
Enter fullscreen mode Exit fullscreen mode

If all goes well, we should see a success message:

Terminal showing Terraform successful deployment

And of course, the deployed resource on OCI:

OCI console showing successful deployment of compartment

Destroying a Terraform deployment

To destroy everything deployed in the current plan, simply

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Safe harbor statement
The information provided on this channel/article/story is solely intended for informational purposes and cannot be used as a part of any contractual agreement. The content does not guarantee the delivery of any material, code, or functionality, and should not be the sole basis for making purchasing decisions. The postings on this site are my own and do not necessarily reflect the views or work of Oracle or Mythics, LLC.

This work is licensed under a Creative Commons Attribution 4.0 International License.

Billboard image

The fastest way to detect downtimes

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitoring.

Get started now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay