Create the VCN Module

Define the virtual cloud network (VCN) and the gateways as resources in the Terraform configuration, and declare the variables used in the module.

Complete the following steps in the vcn subdirectory:
  1. Create a text file named variables.tf, and paste the following code in the file.
    This code declares the variables used in this module.
    variable "tenancy_ocid" {}
    variable "compartment_ocid" {}
    variable "app_tag" {}
    variable "environment" {}
    variable "vcn_cidr" {}
  2. Create a text file named vcn.tf, and paste the following code in the file.
    This code specifies the parameters of the VCN, the internet gateway, and the NAT gateway.
    resource "oci_core_virtual_network" "base_vcn" {
      cidr_block     = "${var.vcn_cidr}"
      compartment_id = "${var.compartment_ocid}"
      display_name   = "${var.app_tag}_${var.environment}_vcn"
      dns_label      = "${lower(format("%s", var.app_tag))}"
    }
    
    resource "oci_core_internet_gateway" "base_ig" {
      compartment_id = "${var.compartment_ocid}"
      display_name   = "${var.app_tag}_${var.environment}_internetgateway"
      vcn_id         = "${oci_core_virtual_network.base_vcn.id}"
    }
    
    resource "oci_core_nat_gateway" "nat_gateway" {
      compartment_id = "${var.compartment_ocid}"
      vcn_id         = "${oci_core_virtual_network.base_vcn.id}"
      display_name   = "${var.app_tag}_${var.environment}_nat_gateway"
    }

    This Terraform configuration example doesn't specify any subnets, security lists, and other networking resources. You can customize this configuration as required.

  3. Create a text file named vcn_output.tf, and paste the following code in the file.
    This code causes Terraform to display the IDs of the resources, after they are created.
    output "vcnid" {
      value = "${oci_core_virtual_network.base_vcn.id}"
    }
    
    output "default_dhcp_id" {
      value = "${oci_core_virtual_network.base_vcn.default_dhcp_options_id}"
    }
    
    output "internet_gateway_id" {
      value = "${oci_core_internet_gateway.base_ig.id}"
    }
    
    output "nat_gateway_id" {
      value = "${oci_core_nat_gateway.nat_gateway.id}"
    }