Create a Compartment

Use Terraform to connect to your Oracle Cloud Infrastructure account and create a compartment in your tenancy.

Key tasks include how to:

  • Use Oracle Cloud Infrastructure Terraform provider resources to:
    • Declare a compartment with your specifics.
    • Create the compartment in your tenancy.
A diagram of a user connected from a local machine to an Oracle Cloud Infrastructure tenancy. The local environment is Linux and has Terraform installed. There is an arrow from Terraform in the local environment,to Terraform Registry, and to the tenancy, pointing to a compartment. These arrows suggest that the user has created a compartment in the tenancy by using Terraform and Terraform Registry.

For more information, see:

1. Prepare

Prepare your environment for authenticating and running your Terraform scripts. Also, collect all the information you need to complete the tutorial.

Get Tenancy Information

Collect the following information from the Oracle Cloud Console and copy it into your notepad.

  • Tenancy OCID: <tenancy-ocid>
    1. In the navigation bar, select the Profile menu and then select Tenancy: <your_tenancy_name>.
    2. Next to OCID, select Copy.

      The tenancy OCID is copied to your clipboard.

Add Compartment Policy

If your username is in the Administrators group, then skip this section. Otherwise, ask your administrator to add the following policy to your tenancy:

allow group <a-group-your-username-belongs-to> to manage compartments in tenancy

With this privilege, you can create a compartment for all the resources in your tutorial.

Steps to Add the Policy
  1. Sign in to the Oracle Cloud Console.
  2. In the navigation bar, select the Profile menu and then select User settings or My profile, depending on the option that you see.
  3. Select Groups or My groups, depending on the option that you see.
  4. In a notepad, copy the name of a group that your username belongs to.
  5. Open the navigation menu  and select Identity & Security. Under Identity, select Policies.
  6. Select the compartment: <your-tenancy>(root)
  7. Select Create Policy.
  8. On the Create Policy page, enter the following values:
    • Name: manage-compartments
    • Description: Allow the group <a-group-your-username-belongs-to> to list, create, update, delete and recover compartments in the tenancy.
    • Compartment: <your-tenancy>(root)
  9. For Policy Builder, select Show manual editor.
  10. Paste in the following policy:
    allow group <a-group-your-username-belongs-to> to manage compartments in tenancy
  11. Select Create.

Reference: Details for Verbs + Resource-Type Combinations (see the compartments resource-type)

2. Create Scripts

Create scripts for authentication, to create a compartment, and to print outputs.

Add Authentication
First, set up a directory for your Terraform scripts. Then add a provider script so your Oracle Cloud Infrastructure account can authenticate the scripts running from this directory.
  1. In your $HOME directory, create a directory called tf-compartment and change to that directory.
    mkdir tf-compartment
    cd tf-compartment
  2. Copy the provider.tf file into the tf-compartment directory.
    cp ../tf-provider/provider.tf .
    Note

    You only need the provider.tf file from the Set Up OCI Terraform tutorial.
Declare a Compartment Resource

Declare an Oracle Cloud Infrastructure compartment resource and then define the specifics for the compartment.

  1. Create a file called compartment.tf.
  2. Add the following code to compartment.tf.
    • Replace <tenancy-ocid>, with the information you gathered at Get Tenancy Information.

      The compartment_id is the OCID for the parent compartment. Use the root compartment as the parent. The tenancy OCID is the compartment OCID for the root compartment.

    • Replace <your-compartment-name> with a name of your choice.
    
    resource "oci_identity_compartment" "tf-compartment" {
        # Required
        compartment_id = "<tenancy-ocid>"
        description = "Compartment for Terraform resources."
        name = "<your-compartment-name>"
    }
  3. Save the compartment.tf file.
Explanation

In Terraform, resources are objects such as virtual cloud networks or compute instances. You can create, update, and delete them with Terraform.

To declare a compartment resource:

  1. Go to Oracle Cloud Infrastructure Provider.
  2. In the Filter box on the upper left, enter compartment.

    Results are returned for both data sources and resources.

  3. Under Identity, go to Resources and select oci_identity_compartment.

    The title of the page is the resource type: oci_identity_compartment

    Required arguments are listed under Argument Reference:

    • compartment_id
    • description
    • name
  4. Construct a resource block:
    • Declare a resource block with the keyword: resource
    • Add a label for resource type: "oci_identity_compartment"
    • Add a label for a local name of your choice:
      • The label can contain letters, digits, underscores (_), and hyphens (-). The first character must not be a digit.
      • Example: "tf-compartment"
    • Inside the code block, provide a value for the required arguments. They don't have default values.
    • For optional arguments, provide values for the ones you want to override. Otherwise, their default values are used.
Add Outputs

Add output blocks to your code to get information about your compartment after Terraform creates the compartment.

  1. In the tf-compartment directory, create a file called outputs.tf.
    Note

    Ensure that outputs.tf, provider.tf, and compartment.tf are in the same directory.
  2. Add the following code to outputs.tf.
    # Outputs for compartment
    
    output "compartment-name" {
      value = oci_identity_compartment.tf-compartment.name
    }
    
    output "compartment-OCID" {
      value = oci_identity_compartment.tf-compartment.id
    }
  3. Save the outputs.tf file.
Explanation
  • Go to Attributes Reference (oci_identity_compartment).
    Note

    Attributes are the outputs that you can return for the oci_identity_compartment resource.
  • Decide which attributes to output.
  • Construct a resource output block:
    • Declare an output block with the keyword: output
    • Add a label to be printed with the output results:
      • The label can contain letters, digits, underscores (_), and hyphens (-). The first character must not be a digit.
      • Example: "compartment-name"
    • Inside the code block, enter a value for the resource output with the expression:
      • value = <type>.<local-name-for-resource>.<attribute>
      • Example: value = oci_identity_compartment.tf-compartment.id
    • Create an output block for each output.

3. Create a Compartment

Run your Terraform scripts. After your account authenticates the scripts, Terraform creates a compartment in your tenancy.

Initialize
Initialize a working directory in the tf-compartment directory.
terraform init

Example output:

Initializing the backend...

Initializing provider plugins...

Terraform has been successfully initialized!
Plan
Create an execution plan to check whether the changes shown in the execution plan match your expectations, without changing the real resources.
terraform plan

The expected output includes the line Plan: 1 to add, 0 to change, 0 to destroy.

Example output:

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with
the following symbols:
  + create

Terraform will perform the following actions:

  # oci_identity_compartment.tf-compartment will be created
  + resource "oci_identity_compartment" "tf-compartment" {
      + compartment_id = "ocid1.tenancy.xxx"
      + defined_tags   = (known after apply)
      + description    = "Compartment for Terraform resources."
      + freeform_tags  = (known after apply)
      + id             = (known after apply)
      + inactive_state = (known after apply)
      + is_accessible  = (known after apply)
      + name           = "<your-compartment-name>"
      + state          = (known after apply)
      + time_created   = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + compartment-OCID = (known after apply)
  + compartment-name = "<your-compartment-name>"
Apply
  1. Create your compartment with Terraform:
    terraform apply

    When prompted for confirmation, enter yes, for your resource to be created.

  2. (Optional) Watch the creation from the Console:
    • Open the navigation menu  and select Identity & Security. Under Identity, select Compartments.
    • Refresh the page until you see the compartment name.
    • Select the compartment name to see its details, such as its OCID.
  3. In the output terminal, review your defined outputs.

    Example output:

    oci_identity_compartment.tf-compartment: Creating...
    oci_identity_compartment.tf-compartment: Creation complete after 9s [id=xxx]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    compartment-OCID = ocid1.compartment.xxx
    compartment-name = <your-compartment-name>

Congratulations! You have successfully signed in and created a compartment in your tenancy, using the Oracle Cloud Infrastructure Terraform provider.

References: