If your username is in the Administrators group, then skip this section. Otherwise, ask your administrator to add the following policy to your tenancy:
Copy
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.
First, set up a directory for your Terraform scripts. Then copy the provider and versions scripts from the Set Up OCI Terraform tutorial so your Oracle Cloud Infrastructure account can authenticate the scripts running from this directory.
In your $HOME directory, create a directory called tf-compartment and change to that directory.
Copy
mkdir tf-compartment
Copy
cd tf-compartment
Copy the provider.tf file into the tf-compartment directory.
Copy
cp ../tf-provider/provider.tf .
Copy the versions.tf file into the tf-compartment directory.
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.
Copy
resource "oci_identity_compartment" "tf-compartment" {
# Required
compartment_id = "<tenancy-ocid>"
description = "Compartment for Terraform resources."
name = "<your-compartment-name>"
}
Create an execution plan to check whether the changes shown in the execution plan match your expectations, without changing the real resources.
Copy
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>"