feat: reuse existing OCI VCN and subnet

This commit is contained in:
juvenal.diaz 2026-10-23 20:53:12 -06:00
parent 5a25c3a856
commit b81bb4d415
4 changed files with 36 additions and 19 deletions

View File

@ -1,5 +1,7 @@
locals {
target_compartment_id = var.enabled && var.create_compartment ? oci_identity_compartment.homelab[0].id : var.compartment_id
network_vcn_id = trimspace(var.existing_vcn_id) != "" ? var.existing_vcn_id : oci_core_vcn.homelab[0].id
public_subnet_id = trimspace(var.existing_public_subnet_id) != "" ? var.existing_public_subnet_id : oci_core_subnet.public[0].id
public_route_table_id = try(oci_core_route_table.public[0].id, null)
private_route_table_id = try(oci_core_route_table.private[0].id, null)
base_tags = merge(var.freeform_tags, { managed_by = "jeannie", purpose = "homelab-learning" })
@ -15,7 +17,7 @@ resource "oci_identity_compartment" "homelab" {
}
resource "oci_core_vcn" "homelab" {
count = var.enabled ? 1 : 0
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
cidr_blocks = [var.vcn_cidr]
display_name = var.vcn_display_name
@ -24,26 +26,26 @@ resource "oci_core_vcn" "homelab" {
}
resource "oci_core_internet_gateway" "homelab" {
count = var.enabled ? 1 : 0
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
display_name = "homelab-internet-gateway"
enabled = true
freeform_tags = local.base_tags
}
resource "oci_core_nat_gateway" "homelab" {
count = var.enabled ? 1 : 0
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
display_name = "homelab-nat-gateway"
freeform_tags = local.base_tags
}
resource "oci_core_route_table" "public" {
count = var.enabled ? 1 : 0
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
display_name = "homelab-public-routes"
route_rules {
destination = "0.0.0.0/0"
@ -54,9 +56,9 @@ resource "oci_core_route_table" "public" {
}
resource "oci_core_route_table" "private" {
count = var.enabled ? 1 : 0
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
display_name = "homelab-private-routes"
route_rules {
destination = "0.0.0.0/0"
@ -67,9 +69,9 @@ resource "oci_core_route_table" "private" {
}
resource "oci_core_subnet" "public" {
count = var.enabled ? 1 : 0
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
cidr_block = var.public_subnet_cidr
display_name = "homelab-public-subnet"
dns_label = "public"
@ -79,9 +81,9 @@ resource "oci_core_subnet" "public" {
}
resource "oci_core_subnet" "private" {
count = var.enabled ? 1 : 0
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
cidr_block = var.private_subnet_cidr
display_name = "homelab-private-subnet"
dns_label = "private"
@ -93,7 +95,7 @@ resource "oci_core_subnet" "private" {
resource "oci_core_network_security_group" "edge" {
count = var.enabled && var.edge_enabled ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
display_name = "homelab-edge-nsg"
freeform_tags = local.base_tags
}
@ -101,7 +103,7 @@ resource "oci_core_network_security_group" "edge" {
resource "oci_core_network_security_group" "email" {
count = var.enabled && var.email_enabled ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = oci_core_vcn.homelab[0].id
vcn_id = local.network_vcn_id
display_name = "homelab-email-nsg"
freeform_tags = local.base_tags
}
@ -247,7 +249,7 @@ resource "oci_core_instance" "edge" {
boot_volume_size_in_gbs = var.edge_boot_volume_size_in_gbs
}
create_vnic_details {
subnet_id = oci_core_subnet.public[0].id
subnet_id = local.public_subnet_id
assign_public_ip = true
hostname_label = var.edge_hostname_label
nsg_ids = [oci_core_network_security_group.edge[0].id]
@ -272,7 +274,7 @@ resource "oci_core_instance" "email" {
boot_volume_size_in_gbs = var.email_boot_volume_size_in_gbs
}
create_vnic_details {
subnet_id = oci_core_subnet.public[0].id
subnet_id = local.public_subnet_id
assign_public_ip = true
hostname_label = var.email_hostname_label
nsg_ids = [oci_core_network_security_group.email[0].id]

View File

@ -1,6 +1,6 @@
output "compartment_id" { value = local.target_compartment_id }
output "vcn_id" { value = try(oci_core_vcn.homelab[0].id, null) }
output "public_subnet_id" { value = try(oci_core_subnet.public[0].id, null) }
output "vcn_id" { value = local.network_vcn_id }
output "public_subnet_id" { value = local.public_subnet_id }
output "private_subnet_id" { value = try(oci_core_subnet.private[0].id, null) }
output "edge_instance_id" { value = try(oci_core_instance.edge[0].id, data.oci_core_instance.existing_edge[0].id, null) }
output "edge_public_ip" { value = try(oci_core_instance.edge[0].public_ip, data.oci_core_instance.existing_edge[0].public_ip, null) }

View File

@ -61,6 +61,18 @@ variable "private_subnet_cidr" {
default = "10.50.20.0/24"
}
variable "existing_vcn_id" {
description = "Existing VCN OCID to reuse instead of creating a VCN and its network primitives."
type = string
default = ""
}
variable "existing_public_subnet_id" {
description = "Existing public subnet OCID to use for newly created instances when reusing a VCN."
type = string
default = ""
}
variable "admin_cidr" {
description = "CIDR allowed to SSH to the edge and email VMs. Do not leave this at 0.0.0.0/0."
type = string
@ -188,6 +200,7 @@ check "foundation_inputs" {
trimspace(var.region) != "" &&
trimspace(var.tenancy_id) != "" &&
(var.create_compartment || trimspace(var.compartment_id) != "") &&
((trimspace(var.existing_vcn_id) == "" && trimspace(var.existing_public_subnet_id) == "") || (trimspace(var.existing_vcn_id) != "" && trimspace(var.existing_public_subnet_id) != "")) &&
trimspace(var.admin_cidr) != "" &&
(!var.edge_enabled || trimspace(var.edge_existing_instance_id) != "" || (trimspace(var.edge_availability_domain) != "" && trimspace(var.edge_image_id) != "")) &&
(!var.email_enabled || (trimspace(var.email_availability_domain) != "" && trimspace(var.email_image_id) != "")) &&

View File

@ -340,6 +340,8 @@ export_oci_foundation_tf_vars() {
export_if_unset TF_VAR_create_compartment "${LAB_OCI_CREATE_COMPARTMENT:-}"
export_if_unset TF_VAR_compartment_id "${LAB_OCI_COMPARTMENT_ID:-}"
export_if_unset TF_VAR_compartment_name "${LAB_OCI_COMPARTMENT_NAME:-}"
export_if_unset TF_VAR_existing_vcn_id "${LAB_OCI_EXISTING_VCN_ID:-}"
export_if_unset TF_VAR_existing_public_subnet_id "${LAB_OCI_EXISTING_PUBLIC_SUBNET_ID:-}"
export_if_unset TF_VAR_admin_cidr "${LAB_OCI_ADMIN_CIDR:-}"
export_if_unset TF_VAR_open_mail_ports "${LAB_OCI_OPEN_MAIL_PORTS:-}"
export_if_unset TF_VAR_edge_enabled "${LAB_OCI_EDGE_ENABLED:-}"