DBA Hub

📋Steps in this guide1/4

Terraform : Oracle Cloud Infrastructure (OCI) Compartment

This article describes how to create a compartment on Oracle Cloud Infrastructure (OCI) using Terraform.

oracle miscconfigurationintermediate
by OracleDba
12 views
1

Create Working Directory

Create a new working directory and switch to that directory. In a previous article ( here ) we discussed the creation of an OCI provider. Copy the OCI provider information into this new working directory.

Code/Command (click line numbers to comment):

1
2
3
4
5
mkdir \git\oraclebase\terraform\oci\oci_compartment
cd \git\oraclebase\terraform\oci\oci_compartment

copy \git\oraclebase\terraform\oci\oci_provider\*.tf .
copy \git\oraclebase\terraform\oci\oci_provider\*.tfvars .
2

oci_compartment.tf

Create a file called "oci_container.tf" with the following contents. The file begins with variable definitions. We could set default values for these variables, or use literal values directly in the provider definition, but we don't want sensitive information checked into version control, so it makes sense to separate out variable values from the script. The resources section defines the compartment using the input variables. The outputs section allows us to see information about the compartment that's been created, including the ID of the compartment. The variables, resources and outputs sections can be split into separate files if you find that organisation easier. It may help for more complex definitions. The parameter is not defined here, as it's already defined in the file. The full list of parameters and outputs available can be found here .

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Variables
variable "compartment_name" { type = string }
variable "compartment_desc" { type = string }


# Resources
resource "oci_identity_compartment" "tf_compartment" {
  # Required
  compartment_id = var.root_compartment_id
  description    = var.compartment_desc
  name           = var.compartment_name
}


# Outputs
output "compartment_name" {
  value = oci_identity_compartment.tf_compartment.name
}

output "compartment_id" {
  value = oci_identity_compartment.tf_compartment.id
}
3

oci_compartment_variables.auto.tfvars

There are a number of ways to supply values for input variables ( see here ). In this example we'll use a ".auto.tfvars" file. We won't check this script into version control as it contains sensitive information. Create a file called "oci_compartment_variables.auto.tfvars" with the following contents. Adjust the values to match your desired compartment details.

Code/Command (click line numbers to comment):

1
2
compartment_name = "obcomp2"
compartment_desc = "Oracle-Base Compartment 2"
4

Build the OCI Compartment

Initialize the working directory using the command. Use the command to test the execution plan. Use the command to create the OCI compartment. Check the Oracle Cloud account to see the new compartment. For more information see: - oci_identity_compartment - Terraform : All Articles Hope this helps. Regards Tim...

Code/Command (click line numbers to comment):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
terraform init

terraform plan
An execution plan has been generated and is shown below.
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.oc1..aaaaaaaa..."
      + defined_tags   = (known after apply)
      + description    = "Oracle-Base Compartment 2"
      + freeform_tags  = (known after apply)
      + id             = (known after apply)
      + inactive_state = (known after apply)
      + is_accessible  = (known after apply)
      + name           = "obcomp2"
      + state          = (known after apply)
      + time_created   = (known after apply)
    }

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

Changes to Outputs:
  + compartment_id   = (known after apply)
  + compartment_name = "obcomp2"

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

terraform apply
An execution plan has been generated and is shown below.
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.oc1..aaaaaaaa..."
      + defined_tags   = (known after apply)
      + description    = "Oracle-Base Compartment 2"
      + freeform_tags  = (known after apply)
      + id             = (known after apply)
      + inactive_state = (known after apply)
      + is_accessible  = (known after apply)
      + name           = "obcomp2"
      + state          = (known after apply)
      + time_created   = (known after apply)
    }

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

Changes to Outputs:
  + compartment_id   = (known after apply)
  + compartment_name = "obcomp2"

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

oci_identity_compartment.tf_compartment: Creating...
oci_identity_compartment.tf_compartment: Still creating... [10s elapsed]
oci_identity_compartment.tf_compartment: Still creating... [20s elapsed]
oci_identity_compartment.tf_compartment: Still creating... [30s elapsed]
oci_identity_compartment.tf_compartment: Still creating... [40s elapsed]
oci_identity_compartment.tf_compartment: Still creating... [50s elapsed]
oci_identity_compartment.tf_compartment: Still creating... [1m0s elapsed]
oci_identity_compartment.tf_compartment: Creation complete after 1m7s [id=ocid1.compartment.oc1..aaaaaaaa...]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:

compartment_id = "ocid1.compartment.oc1..aaaaaaaa..."
compartment_name = "obcomp2"

Comments (0)

Please to add comments

No comments yet. Be the first to comment!