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.

For more information, see:
Before You Begin
To successfully perform this tutorial, you must have the following:
- An Oracle Cloud Infrastructure account. See Request and Manage Free Oracle Cloud Promotions.
- A MacOS, Linux, or Windows computer.
- Terraform tutorial scripts:
- Go through all the steps in Set Up OCI Terraform
- Keep the scripts you created in the
following directory:
$HOME/tf-provider/
1. Prepare
Prepare your environment for authenticating and running your Terraform scripts. Also, collect all the information you need to complete the tutorial.
Collect the following information from the Oracle Cloud Console and copy it into your notepad.
- Tenancy OCID:
<tenancy-ocid>
- In the navigation bar, select the Profile menu and then select Tenancy: <your_tenancy_name>.
- Next to OCID, select Copy.
The tenancy OCID is copied to your clipboard.
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.
- Sign in to the Oracle Cloud Console.
- In the navigation bar, select the Profile menu and then select User settings or My profile, depending on the option that you see.
- Select Groups or My groups, depending on the option that you see.
- In a notepad, copy the name of a group that your username belongs to.
- Open the navigation menu and select Identity & Security. Under Identity, select Policies.
- Select the compartment:
<your-tenancy>(root)
- Select Create Policy.
- 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)
- Name:
- For Policy Builder, select Show manual editor.
- Paste in the following policy:
allow group <a-group-your-username-belongs-to> to manage compartments in tenancy
- 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.
Declare an Oracle Cloud Infrastructure compartment resource and then define the specifics for the compartment.
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:
- Go to Oracle Cloud Infrastructure Provider.
- In the Filter box on the upper left, enter
compartment
.Results are returned for both data sources and resources.
- 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
- 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"
- The label can contain letters, digits, underscores (
- 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.
- Declare a resource block with the keyword:
Add output blocks to your code to get information about your compartment after Terraform creates the compartment.
- Go to Attributes Reference (oci_identity_compartment).Note
Attributes are the outputs that you can return for theoci_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"
- The label can contain letters, digits, underscores (
- 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.
- Declare an output block with the keyword:
3. Create a Compartment
Run your Terraform scripts. After your account authenticates the scripts, Terraform creates a compartment in your tenancy.
tf-compartment
directory.
terraform init
Example output:
Initializing the backend...
Initializing provider plugins...
Terraform has been successfully initialized!
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>"
Congratulations! You have successfully signed in and created a compartment in your tenancy, using the Oracle Cloud Infrastructure Terraform provider.
References:
What's Next
For the next Terraform: Get Started tutorial, go to:
To explore more information about development with Oracle products, check out these sites: