Terraform: Create a Compartment
In this tutorial, you 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 additional information, see:
Before You Begin
To successfully perform this tutorial, you must have the following:
- An Oracle Cloud Infrastructure account. See Signing Up for Oracle Cloud Infrastructure.
- A MacOS, Linux, or Windows computer.
- Terraform tutorial scripts:
- Go through all the steps in Terraform Tutorial: 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 Infrastructure Console and copy it into your notepad.
- Tenancy OCID:
<tenancy-ocid>
- From your user avatar, go to Tenancy: <your-tenancy> and copy OCID.
If your username is in the Administrators group, then skip this section. Otherwise, have your administrator add the following policy to your tenancy:
allow group <the-group-your-username-belongs> to manage compartments in tenancy
With this privilege, you can create a compartment for all the resources in your tutorial.
- In the top navigation bar, open the Profile menu.
- Click your username.
- In the left pane, click Groups.
- In a notepad, copy the Group Name that your username belongs.
- Open the navigation menu and click Identity & Security. Under Identity, click Policies.
- Select your compartment from the Compartment drop-down.
- Click Create Policy.
- Fill in the following information:
- Name:
manage-compartments
- Description:
Allow the group <the-group-your-username-belongs> to list, create, update, delete and recover compartments in the tenancy.
- Compartment:
<your-tenancy>(root)
- Name:
- For Policy Builder, click Show manual editor.
- Paste in the following policy:
allow group <the-group-your-username-belongs> to manage compartments in tenancy
- Click Create.
Reference: The compartments
resource-type in Verbs + Resource-Type Combinations for IAM
2. Create Scripts
Create three scripts: one for authentication, one to create a compartment, and one 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 left navigation Filter, enter
compartment
.Results are returned for both Data Sources and Resources.
- Under Identity, go to Resources and click oci_identity_compartment.
- Find the Resource Type from the title of the page:
- Type:
oci_identity_compartment
- Type:
- In the Argument Reference section, find all arguments (inputs) labeled as
(Required):
- 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"
- Inside the code block, provide a value for the required arguments. They don't have a default value.
- 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.
- On the Resource: oci_identity_compartment
page, go to Attributes Reference.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.
- 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.
Congratulations! You have successfully logged 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: