Create the Compartments Module

Define each compartment as a resource, and declare the required variables.

Complete the following steps in the compartments subdirectory:
  1. Create a text file named variables.tf, and paste the following code in the file.
    This code declares the variables used in this module.
    variable "tenancy_ocid" {}
    variable "app_tag" {}
    variable "environment" {}
  2. Create a text file named compartments.tf, and paste the following code in the file.
    resource "oci_identity_compartment" "networks" {
      description = "The networks compartment"
      name        = "${var.app_tag}_${var.environment}_networks"
    }
    
    resource "oci_identity_compartment" "admin" {
      description = "The admin compartment"
      name        = "${var.app_tag}_${var.environment}_admin"
    }
    
    resource "oci_identity_compartment" "shared_services" {
      description = "The shared_services compartment"
      name        = "${var.app_tag}_${var.environment}_shared_services"
    }
    
    resource "oci_identity_compartment" "business_logic" {
      description = "The business_logic compartment"
      name        = "${var.app_tag}_${var.environment}_business_logic"
    }
    
    resource "oci_identity_compartment" "database" {
      description = "The database compartment"
      name        = "${var.app_tag}_${var.environment}_database"
    }
    
  3. Create a text file named compartments_output.tf, and paste the following code in the file.
    This code causes Terraform to display the IDs of the compartments, after they are created.
    output "networks_id" {
      value = "${oci_identity_compartment.networks.id}"
    }
    
    output "admin_id" {
      value = "${oci_identity_compartment.admin.id}"
    }
    
    output "shared_services_id" {
      value = "${oci_identity_compartment.shared_services.id}"
    }
    
    output "business_logic_id" {
      value = "${oci_identity_compartment.business_logic.id}"
    }
    
    output "database_id" {
      value = "${oci_identity_compartment.database.id}"
    }