Repository navigation
LAB 45 (Create compartment using modular approach)
“This folder contains reusable Terraform configurations for creating Oracle Cloud Infrastructure (OCI) compartments.”
“Create a provider.tf file inside the compartment_template folder and add the OCI provider configuration.
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"
}
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
}
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.
terraform init
terraform plan
terraform apply
When prompted, type:
yes
Now navigate to the oracle console and verifiy in the comparment section, our mantra comparment successfully created with terraform moduler approach.
terraform state list
terraform destroy -target=<resource_address>
terraform destroy -target=module.parent_compartment.oci_identity_compartment.compartment
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
}
After Configure all details we will process for comparment creation by ussing command
terraform init
terraform plan
terraform apply
When prompted, type:
yes