Skip to content

LAB 45 (Create compartment using modular approach)

arunsinghchauhan38-arch edited this page Jul 4, 2026 · 5 revisions

Steps to Create Compartment Using Modular Approach

Step 1. Create Compartment_template folder

“This folder contains reusable Terraform configurations for creating Oracle Cloud Infrastructure (OCI) compartments.”

image

Step 2. Create provider.tf file

“Create a provider.tf file inside the compartment_template folder and add the OCI provider configuration.

image

Step 3. Create private key file in compartment_template folder

image

Step 4. Create variable.tf file inside compartment_template folder

Create a variables.tf file inside the compartment_template folder.

Define all input variables required for creating the compartment in this file. These variables will be referenced in the main.tf file, enabling the Terraform module to be reusable, configurable, and easier to maintain.

variable "parent_compartment_ocid" {
  type = string
description = "the ocid of parent compartment where the new compartment will be created"
}

variable "compartment_name" {
  type = string
  description = "the name assigned to compartment"
}

variable "compartment_description" {
type = string
description = "the description of the comparment"
}

variable "enable_delete" {
  type = bool
  description = "specified whether the compartment deleted"
}
image

Step 5. Create main.tf file inside the compartment_template folder

Define the OCI Identity Compartment resource in this file and reference the input variables declared in the variables.tf file using the var.<variable_name> syntax. This approach ensures the module remains reusable, configurable, and easy to maintain.

resource "oci_identity_compartment" "compartment" {
  compartment_id = var.parent_compartment_ocid  
  name           = var.compartment_name
  description    = var.compartment_description
  enable_delete  = var.enable_delete
}
image

Step 6. Create New folder - Compartment

image

Step 7. Create provider.tf file inside compartment folder

image image

Step 8. Create file mantra.tf inside the compartment folder

Configure a module block that references the reusable Terraform module located in the compartment_template folder. Provide all required input values for the module.

The module configuration must include the following parameters:

  • The module source path that references the reusable Terraform module.
  • The Parent Compartment OCID where the new compartment will be created.
  • The compartment name.
  • The compartment description.
  • The enable delete option, which specifies whether the compartment can be deleted.

Step 9. Initialize the Terraform

terraform init
image image

Step 10. Review the Execution Plan

terraform plan
image image

Step 11. Create mantra compartment

terraform apply

When prompted, type:

yes
image image

Step 12. Verify the mantra Compartment

Now navigate to the oracle console and verifiy in the comparment section, our mantra comparment successfully created with terraform moduler approach.

image image

Step 13. View the Terraform State

terraform state list
image

Step 14. Destroy the mantra compartment

terraform destroy -target=<resource_address>
terraform destroy -target=module.parent_compartment.oci_identity_compartment.compartment
image

Step 15. Creating Child Compartment by same terraform approach

Here we use same method to create child compartment.

Module configuration for compute and storage compartment

module "mantra_compute_and_storage_compartment" {

source = "/home/arunsinghc/compartment_template"
## mantra ocid will given as parent compartment ocid
parent_compartment_ocid = "ocid1.compartment.oc1..aaaaaaaa4hideke6wljorp4annfsgcv2flrcueptqppo5ymg7wf6mlqoulja"
compartment_name        = "mantra_compute_and_storage"
compartment_description = "this compartment use for mantra application"
enable_delete           = true
}

Module configuration for network compartment

module "mantra_network" {

source = "/home/arunsinghc/compartment_template"
## mantra ocid will given as parent compartment ocid
parent_compartment_ocid = "ocid1.compartment.oc1..aaaaaaaa4hideke6wljorp4annfsgcv2flrcueptqppo5ymg7wf6mlqoulja"
compartment_name        = "mantra_network"
compartment_description = "this compartment use for mantra application"
enable_delete           = true
}
image image image

After Configure all details we will process for comparment creation by ussing command

terraform init
terraform plan
terraform apply

When prompted, type:

yes
image

Clone this wiki locally