Change infrastructure
In the last tutorial, you created your first infrastructure with Terraform: a single Virtual Cloud Network (VCN) on OCI. In this tutorial, you will modify that resource, and learn how to apply changes to your Terraform projects.
Infrastructure is continuously evolving and Terraform helps you manage that change. As you change Terraform configurations, Terraform builds an execution plan that only modifies what is necessary to reach your desired state.
When using Terraform in production, we recommend that you use a version control system to manage your configuration files, and store your state in a remote backend such as HCP Terraform or Terraform Enterprise.
Prerequisites
This tutorial assumes that you are continuing from the previous tutorial, which we highly recommend. If not, we've summarized the prerequisites here for your convenience. To follow this tutorial you will need:
OCI Tenancy. Note your region, you will use it throughout the tutorial.
The Terraform CLI installed.
The OCI CLI installed. Configure the CLI with a token by following the session authentication flow.
Create a directory named
learn-terraform-oci
and paste the following configuration into a file namedmain.tf
.Customize the
region
andcompartment_id
to match the ones listed on your OCI tenancy page in the web console.Initialize the configuration.
Apply the configuration. Respond to the confirmation prompt with a
yes
.
Once you have successfully applied the configuration, you can continue with the rest of this tutorial.
Add a resource
Now you will add a subnet to your VCN. Open main.tf
in your text editor and add the following configuration under the VCN resource block.
Customize the value of compartment_id
with your own OCID and save the file.
After changing the configuration, run terraform apply
again to see how
Terraform will apply this change to the existing resources.
Not the +
next to the OCI core subnet and its attributes, indicating that Terraform will add the subnet to the existing VCN. The VCN will not change because Terraform already knows it exists and has its information stored in the state file.
Once again, Terraform prompts for approval of the execution plan before
proceeding. Answer yes
to execute the planned steps.
Verify that OCI created your subnet by visiting the OCI Console's VCN page in your region and clicking on "My internal VCN". On the page for your VCN, the OCI Console lists subnets, including "Dev subnet 1".
Modify a resource
Terraform can modify existing resources as well as creating new ones. Modify the new subnet by changing it's display name from Dev subnet 1
to Dev subnet
. Then save the file.
Tip
The below snippet is formatted as a diff to give you context about
which parts of your configuration you need to change. Replace the content
displayed in red with the content displayed in green, leaving out the leading
+
and -
signs.
After changing the configuration, run terraform apply
again to see how Terraform will apply this change to the existing resources.
Notice the ~
prefix next to the subnet and its display name, indicating that Terraform can change this attribute of the resource. It also prints how many resources it will change, create, or destroy in the output.
Terraform can update some resources and attributes in-place, but others it can't change without destroying the resource. The prefix -/+
would mean that Terraform planned to destroy and recreate the resource, rather than updating it in-place. Terraform handles these details for you, and the execution plan displays what Terraform will do.
Approve the change with a yes
.
Check the OCI Console networking page for your internal VCN and note that the displayed name is now "Dev subnet".
Next Steps
You have now added a resource to your Terraform configuration, and updated a resource in place. Learn more about OCI subnets in the subnet resource documentation.