Applying Configurations

After the Oracle Cloud Infrastructure (OCI) Terraform provider is installed and configured, and you have created Terraform configurations that include the provider and a resource or data source, you can run Terraform against your OCI infrastructure.

Tip

See Tutorials for step-by-step instructions and examples.

Initializing Terraform

Run the following command from a directory that contains your Terraform configuration files to initialize Terraform:

terraform init

Example output:

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/oci...
- Installing hashicorp/oci v3.96.0...
- Installed hashicorp/oci v3.96.0 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.
...
* hashicorp/oci: version = "~> 3.96.0"

Terraform has been successfully initialized!

If the command results in an error, verify that the OCI Terraform provider is configured properly.

Reviewing Changes

Without making any changes to your existing OCI resources, Terraform can return a list of changes that will be made based on your configurations if you choose to apply them. This list is called an execution plan. To view the execution plan, run the following command:

terraform plan

Example output:

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 = "exampleuniqueID"
      + 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.

Review the plan and check whether the changes shown in the plan match your expectations.

Destructive Changes

Some OCI resources have properties that cannot be updated by Terraform without destroying the existing resource and re-creating it with the new property value. When you run terraform plan and attempt to modify a non-updatable property, Terraform indicates what resource will be replaced and what property or properties cannot be updated.

For example, the following execution plan prefixes a resource that will be deleted and re-created with -/+ and appends the # forces replacement comment to the property that cannot be updated:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
 
Terraform will perform the following actions:
 
  # oci_core_vcn.vcn must be replaced
-/+ resource "oci_core_vcn" "vcn" {
      ~ cidr_block               = "10.0.0.0/16" -> "11.0.0.0/16" # forces replacement
      ~ cidr_blocks              = [
          - "10.0.0.0/16",
        ] -> (known after apply)
        compartment_id           = "ocid1.compartment.oc1..exampleuniqueID"
      ~ default_dhcp_options_id  = "ocid1.dhcpoptions.oc1.phx.exampleuniqueID" -> (known after apply)
      ~ default_route_table_id   = "ocid1.routetable.oc1.phx.exampleuniqueID" -> (known after apply)
      ~ default_security_list_id = "ocid1.securitylist.oc1.phx.exampleuniqueID" -> (known after apply)
      ~ defined_tags             = {} -> (known after apply)
        display_name             = "vcn"
        dns_label                = "vcn"
      ~ freeform_tags            = {} -> (known after apply)
      ~ id                       = "ocid1.vcn.oc1.phx.exampleuniqueID" -> (known after apply)
      + ipv6cidr_block           = (known after apply)
      + ipv6public_cidr_block    = (known after apply)
      + is_ipv6enabled           = (known after apply)
      ~ state                    = "AVAILABLE" -> (known after apply)
      ~ time_created             = "2021-01-25 20:54:53.255 +0000 UTC" -> (known after apply)
      ~ vcn_domain_name          = "vcn.oraclevcn.com" -> (known after apply)
    }
 
Plan: 1 to add, 0 to change, 1 to destroy.
Caution

A forced deletion and re-creation of a resource might also result in the replacement of a parent resource. Always run terraform plan before you run terraform apply to see what resources will be affected.

The full reference of the OCI Terraform provider's supported resources and data sources contains usage, argument, and attribute details. The full reference is available at docs.oracle.com and Terraform Registry.

The reference indicates which properties are updatable.

Applying Changes

Once you're confident that your configurations will result in your expected changes, you can apply your Terraform configurations by running the following command:

terraform apply

When prompted for confirmation, enter yes, for your resource to be created. After you run the apply comand, the output is displayed in the terminal. Example output:

oci_identity_compartment.tf-compartment: Creating...
oci_identity_compartment.tf-compartment: Creation complete after 9s [id=exampleuniqueID]

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

Outputs:

compartment-OCID = ocid1.compartment.exampleuniqueID
compartment-name = <your-compartment-name>
Caution

If you make changes to resources you manage with Terraform outside of Terraform, those changes will be overwritten the next time you use Terraform to apply configurations. Add the ignore_changes parameter to resources in your configuration file that you do not want to overwrite.