2.1.3 VCN & public subnet (new compartment)

Terraform only implementation

n.b: This example will be implemented under a child compartment of root compartment

Creation of new compartment

For creation of compartment, you use the Terraform resource oci identity compartment

The terraform implementation

Those that need a better understanding for what is happening in this tutorial, check my previous lesson, 2.1.2 VCN &public subnet (step-by-step in Terraform). This tutorial is focused strictly on the automation for creating VCN and its resources in a new compartment.

My new working directory contains the following files:

root@deploymentmachine:/home/terra/vcn_new_compartment# tree . 
.
├── compartment.tf
├── dhcp_opt.tf
├── int_gateway.tf
├── provider.tf
├── route.tf
├── security_list.tf
├── subnet.tf
├── variables.tf
└── vcn.tf

0 directories, 9 files

Content of variables.tf:

Content of provider.tf:

Content of compartment.tf:

Content of vcn.tf:

Content of subnet.tf:

Content of creating an internet gateway,int_gateway.tf:

Content of creating security list, security_list.tf:

Content of route.tf:

Content of creating dhcp options, dhcp_opt.tf:

Proceed with the known terraform command: "terraform init" ; "terraform plan"; "terraform plan"

The "terraform apply" would show the following output:

Back to the Cloud UI

And let's check in the Cloud UI:

Compartment created:

The VCN created along with its resources:

a) Public Subnet

b) Security List

c) Internet Gateway

d) DHCP Options (and DNS, but you can already notice the DNS in previous photos)

And one simple test

Let's check if we can reach a compute instance deployed in the new VCN.

The steps for creating a new compute instance in the WildTestCompartment & WildTestVCN, from UI:

Once the Instance is created and available, try to log in via ssh, by using the Public IP:

Destroy resources

Wait until the termination status of the test instance we deployed in the WildTestVCN:

... and destroy the VCN & its resources by using the "terraform destroy"

Last updated