---
description: Terraform only implementation
---

# 2.1.3 VCN & public subnet (new compartment)



> _**n.b:** This example will be implemented under a child compartment of root compartment_



### Creation of new compartment&#x20;

For creation of compartment, you use the Terraform resource[ oci identity compartment](https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/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_ ](2.1.2-writing-terraform-step-by-step)_(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`**`:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more variables.tf

# provided here the root compartment 

variable "compartment_ocid" {
  default = "ocid1.tenancy.oc1..aaaaaaaahereIaddedTheOcidOfR00tCompartment"
}

#for vcn block

variable "cidrblockz" {
  type = list(string)
  default = ["10.0.0.0/16"]
}

#for subnet

variable "cidrsubnet" {
  default = "10.0.1.0/24"
}

# for ingress


variable "cidr_ingress" {
  default = "10.0.0.0/16"
}

# for security list

variable "portz" {
 default = [22,3306,33060]
}

```

`Content of`` `**`provider.tf`**`:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more provider.tf

provider "oci" {
  tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaafaketenancyocidhahahah123234234"
  user_ocid = "ocid1.user.oc1..aaaaaaafakeuserocidhahahah12312434"
  private_key_path = "/root/.oci/oci_api_key.pem"
  fingerprint = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
  region = "eu-frankfurt-1"
}
```

`Content of`` `**`compartment.tf:`**

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more compartment.tf

# using the root compartment OCID at compartment_id
# WildTestCompartment will be the root's child

resource "oci_identity_compartment" "WildTestCompartment" {
    compartment_id = var.compartment_ocid
    description = "Compartment test for VCN"
    name = "WildTestCompartment"
}
```

`Content of`` `**`vcn.tf`**`:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more vcn.tf

resource "oci_core_virtual_network" "WildTestVCN" {
  cidr_blocks = var.cidrblockz
  compartment_id = oci_identity_compartment.WildTestCompartment.id
  display_name = "WildTestVCN"
  dns_label = "WildTestVCN"
}
```

`Content of`` `**`subnet.tf`**`:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more subnet.tf

resource "oci_core_subnet" "WildTestSubnet"{

  cidr_block = var.cidrsubnet
  compartment_id = oci_identity_compartment.WildTestCompartment.id
  vcn_id = oci_core_virtual_network.WildTestVCN.id

  display_name = "WildTestSubnet"

  # security list

  security_list_ids = [oci_core_security_list.WildTestSecurityList.id]

  # route table

  route_table_id = oci_core_route_table.WildTestRouteTable.id

  # dhcp
  dhcp_options_id = oci_core_dhcp_options.WildTestDHCPOptions.id

  # dns
  dns_label = "WildTest"

}
```

`Content of creating an internet gateway,`**`int_gateway.tf`**`:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more int_gateway.tf

resource "oci_core_internet_gateway" "WildTestInternetGateway" {
  compartment_id = oci_identity_compartment.WildTestCompartment.id
  display_name = "WildTestInternetGateway"
  vcn_id = oci_core_virtual_network.WildTestVCN.id
}
```

`Content of creating security list,`` `**`security_list.tf`**`:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more security_list.tf

resource "oci_core_security_list" "WildTestSecurityList" {

  compartment_id = oci_identity_compartment.WildTestCompartment.id

  display_name = "WildTestSecurityList"

  vcn_id = oci_core_virtual_network.WildTestVCN.id

  egress_security_rules {
    stateless = false
    protocol = "6"
    destination = "0.0.0.0/0"
 }

   # apply ingress tcp rules for each port
   # of variable portz

   dynamic "ingress_security_rules" {
    for_each = toset(var.portz)
      content {
        protocol = "6"
        source = "0.0.0.0/0"
        tcp_options {
           max = ingress_security_rules.value
           min = ingress_security_rules.value
       }
    }
  }



  ingress_security_rules {
    stateless = false
    protocol = "6"
    source = var.cidr_ingress
  }
}
```

`Content of`` `**`route.tf`**`:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# more route.tf

resource "oci_core_route_table" "WildTestRouteTable" {
  compartment_id = oci_identity_compartment.WildTestCompartment.id
  vcn_id = oci_core_virtual_network.WildTestVCN.id
  display_name = "WildTestRouteTable"

  route_rules {
    destination = "0.0.0.0/0"
    network_entity_id = oci_core_internet_gateway.WildTestInternetGateway.id
  }
}
```

`Content of creating dhcp options,`` `**`dhcp_opt.tf`**`:`

```
root@deploymentmachine:/home/tests/terra/test6# more dhcp_opt.tf
resource "oci_core_dhcp_options" "WildTestDHCPOptions" {

  compartment_id = oci_identity_compartment.WildTestCompartment.id
  vcn_id = oci_core_virtual_network.WildTestVCN.id
  display_name = "WildTestDHCPOptions"

  options {
    type = "DomainNameServer"
    server_type = "VcnLocalPlusInternet"
  }

  options {
    type = "SearchDomain"
    search_domain_names = ["wildtest.com"]
  }

 }

```



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

`The`` `**`"terraform apply"`**` ``would show the following output:`

```
root@deploymentmachine:/home/terra/vcn_new_compartment# terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create
  Enter a value: yes

oci_identity_compartment.WildTestCompartment: Creating...
oci_identity_compartment.WildTestCompartment: Still creating... [10s elapsed]
oci_identity_compartment.WildTestCompartment: Still creating... [20s elapsed]
oci_identity_compartment.WildTestCompartment: Still creating... [30s elapsed]
oci_identity_compartment.WildTestCompartment: Still creating... [40s elapsed]
oci_identity_compartment.WildTestCompartment: Creation complete after 43s [id=ocid1.compartment.oc1..aaaaaaaaqvdbwn7ajc6bl37ggvpom4qvqtjeeqtcmorwsjhu5qbrnpgdor5a]
oci_core_virtual_network.WildTestVCN: Creating...
oci_core_virtual_network.WildTestVCN: Creation complete after 1s [id=ocid1.vcn.oc1.eu-frankfurt-1.amaaaaaa34qs2dyaxzfotfa5jixzhoxtocxpahat6iuzhwwxsk2dn5lwtcfa]
oci_core_dhcp_options.WildTestDHCPOptions: Creating...
oci_core_internet_gateway.WildTestInternetGateway: Creating...
oci_core_security_list.WildTestSecurityList: Creating...
oci_core_internet_gateway.WildTestInternetGateway: Creation complete after 0s [id=ocid1.internetgateway.oc1.eu-frankfurt-1.aaaaaaaa4einp73aif27e55jlipzktwqk3iutz4tlyd7tnphxfmyvsnur65a]
oci_core_route_table.WildTestRouteTable: Creating...
oci_core_security_list.WildTestSecurityList: Creation complete after 0s [id=ocid1.securitylist.oc1.eu-frankfurt-1.aaaaaaaaxldsobthhdwtdsv4jxej4rkedxvunsgnypvoid4g55z6drrwm62q]
oci_core_dhcp_options.WildTestDHCPOptions: Creation complete after 0s [id=ocid1.dhcpoptions.oc1.eu-frankfurt-1.aaaaaaaa7dphusxukgrfrvcf6bex2p6nyslkpji42ooos3tkz4g5t53ew74q]
oci_core_route_table.WildTestRouteTable: Creation complete after 0s [id=ocid1.routetable.oc1.eu-frankfurt-1.aaaaaaaa7s24oegshleoear5nw6vreachy7td4q7oirf2kl65tcgn3qfx5ma]
oci_core_subnet.WildTestSubnet: Creating...
oci_core_subnet.WildTestSubnet: Creation complete after 5s [id=ocid1.subnet.oc1.eu-frankfurt-1.aaaaaaaallavbxwnlaijq6lymtxw7h5dgmrd6vamwjyol6mf7wkh6xthhcwq]

Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
```

###

### Back to the Cloud UI

`And let's check in the Cloud UI:`

`Compartment created:`

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTB1ATBypkKyHYfLatB%2F-MTB1_SoeVw_1uCxZIoQ%2Fcomp.png?alt=media\&token=3c314331-addc-445e-b92a-1c4c48461074)

`The VCN created along with its resources:`

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAhm2MSNa57qD-7OW3%2F-MTAjHnBwpnLBYUWLz2s%2F1.png?alt=media\&token=b5585704-c6a8-47cd-a580-6dcfdb0a0f35)

**`a)`**` ``Public Subnet`

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAhm2MSNa57qD-7OW3%2F-MTAk-DKqQiEbmN01W1s%2F2.png?alt=media\&token=f404ee54-725f-4928-ac0b-8d7bb00287a1)

**`b)`**` ``Security List`

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAhm2MSNa57qD-7OW3%2F-MTAk66snUO55i98wlgx%2F3.png?alt=media\&token=5ba3fbcd-9a48-4e9c-a55a-70a767dbce79)

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAhm2MSNa57qD-7OW3%2F-MTAk8azn7610FVzGwGr%2F4.png?alt=media\&token=ef483ad7-1b2f-40fd-809c-898cf524ab46)

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAhm2MSNa57qD-7OW3%2F-MTAkBRNT9XQVH2ICaor%2F5.png?alt=media\&token=3c5dbf39-cb0c-4660-85c2-09ed1bd70781)

**`c)`**` ``Internet Gateway`

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAhm2MSNa57qD-7OW3%2F-MTAkRl23tLl4BHyHRKu%2F6.png?alt=media\&token=b70ed5bd-aeb8-45fc-b7f2-5d32b7f344e1)

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

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAhm2MSNa57qD-7OW3%2F-MTAklHLZUoMUBob8iDL%2F7.png?alt=media\&token=6d421ec8-d6aa-419b-9841-c302a873305a)

###

### And one simple test&#x20;

`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:`

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTAz5W-ajMjvWk0iRw-%2F-MTAzLunnZnrhDotR3ru%2F8.png?alt=media\&token=b18ed051-1526-4be4-9e0f-4e261b9b8e56)

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTB-11OBoPgwRYbd7Tf%2F-MTB-KaJ73ehOkj5clXx%2F10.png?alt=media\&token=e6ae9512-08cc-46a4-8902-cdedaa574353)

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTB-11OBoPgwRYbd7Tf%2F-MTB-N2dkmjv8hdc2BeL%2F12.png?alt=media\&token=62fc3d38-3419-41f6-a909-e6a4dddafe41)



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

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTB-11OBoPgwRYbd7Tf%2F-MTB-VaGuYb-LBGRojCl%2F13.png?alt=media\&token=3735be20-3af3-4b7a-982a-c66eb8745434)

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTB-11OBoPgwRYbd7Tf%2F-MTB0TaeDHlkCt9R032T%2F15.png?alt=media\&token=cbfba03d-7650-438e-8c3f-0ec783f2dd7c)



### Destroy resources

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

![](https://3964595292-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MRFinYAjIA976nxMf0U%2F-MTB1h1xl0FBkuFYvXKt%2F-MTB2FPaLERL0HZPWNIL%2Fterm.png?alt=media\&token=43cd0ae7-a7d3-4ebd-9d24-1825d0a2a615)

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

```
root@deploymentmachine:/home/terra/vcn_new_compartment# terraform destroy

[ ............ snip .............. ]

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

oci_core_subnet.WildTestSubnet: Destroying... [id=ocid1.subnet.oc1.eu-frankfurt-1.aaaaaaaacgxxxxxxxxxxxxxxxxxxxxxql
zyyocfitn5rbre2sta
7l3]
oci_core_subnet.WildTestSubnet: Destruction complete after 1s
oci_core_route_table.WildTestRouteTable: Destroying... [id=ocid1.routetable.oc1.eu-frankfurt-1.aaaaaaaagxxxxxxxxxxxxxxxxxxxxxq]
oci_core_security_list.WildTestSecurityList: Destroying... [id=ocid1.securitylist.oc1.eu-frankfurt-1.aaaaaaaccxxxxxxxxxxxxxxxxxxq]
oci_core_dhcp_options.WildTestDHCPOptions: Destroying... [id=ocid1.dhcpoptions.oc1.eu-frankfurt-1.aaaaaaaaccoqpo7zwehfc44ogxxxxxxxxxxxxxxxxxxxxxq]
oci_core_dhcp_options.WildTestDHCPOptions: Destruction complete after 0s
oci_core_security_list.WildTestSecurityList: Destruction complete after 0s
oci_core_route_table.WildTestRouteTable: Destruction complete after 0s
oci_core_internet_gateway.WildTestInternetGateway: Destroying... [id=ocid1.internetgateway.oc1.eu-frankfurt-1.aaaaaaaasdfsdf2hkrxk3367ucs3tgxxxxxxxxxxxxxxxxxxxxxq]
oci_core_internet_gateway.WildTestInternetGateway: Destruction complete after 0s
oci_core_virtual_network.WildTestVCN: Destroying... [id=ocid1.vcn.oc1.eu-frankfurt-1.asdaagovrtjfjztyshbo2swuhjgxxxxxxxxxxxxxxxxxxxxxq]
oci_core_virtual_network.WildTestVCN: Destruction complete after 1s
oci_identity_compartment.WildTestCompartment: Destroying... [id=ocid1.compartment.oc1..aaagxxxxxxxxxxxxxxxxxxxxxqoq5crsyqa3iw5wpf6su3ds4z3q6figk3xa5su5l2agxxxxxxxxxxxxxxxxxxxxxq

Destroy complete! Resources: 7 destroyed.
root@deploymentmachine:/home/terra/vcn_new_compartment# 
```

