62_Terraform: Provisioning OCI VCN and Internet Gateway with Terraform - Nirvan-Pandey/OCI_DOC GitHub Wiki

62_1: Introduction

This lab demonstrates how to provision a Virtual Cloud Network (VCN) and an Internet Gateway in Oracle Cloud Infrastructure (OCI) using Terraform.

We will use Terraform to automate the provisioning of networking components in OCI. This includes writing configuration files, initializing Terraform, and applying the configuration to deploy the VCN and Internet Gateway.

62_2: Reference Documentation

Refer to the official OCI Terraform provider documentation:

https://docs.oracle.com/en-us/iaas/tools/terraform-provider-oci/6.31.0/docs/r/identity_compartment.html

Search for oci_core_vcn to find the required schema.

62_3: Setup Terraform Configuration Files

main.tf

provider "oci" {
  region = "us-ashburn-1"
}

resource "oci_core_vcn" "example_vcn" {
  cidr_block     = "10.0.0.0/16"
  display_name   = "example-vcn"
  compartment_id = "<your_compartment_ocid>"
  dns_label      = "examplevcn"
}

variables.tf

variable "compartment_id" {
  description = "The OCID of the compartment where the VCN will be created"
  type        = string
}

variable "region" {
  description = "The OCI region"
  type        = string
  default     = "us-ashburn-1"
}

variable "vcn_cidr" {
  description = "The CIDR block for the VCN"
  type        = string
  default     = "10.0.0.0/16"
}

outputs.tf

output "vcn_id" {
  value = oci_core_vcn.example_vcn.id
}

output "vcn_cidr_block" {
  value = oci_core_vcn.example_vcn.cidr_block
}

62_4: Initialize Terraform

terraform init

62_5: Validate and Plan

terraform validate
terraform plan -var="compartment_id=<your_compartment_ocid>"

62_6: Apply Configuration

terraform apply -var="compartment_id=<your_compartment_ocid>"

62_7: Verify the VCN

After applying, verify in the OCI console under Networking > Virtual Cloud Networks. Output will also show VCN ID and CIDR block.

62_8: Clean Up (Optional)

terraform destroy -var="compartment_id=<your_compartment_ocid>"