my-homelab-configs/bootstrap/oci/main.tf

403 lines
15 KiB
HCL

locals {
target_compartment_id = var.enabled && var.create_compartment ? oci_identity_compartment.homelab[0].id : var.compartment_id
email_compartment_id = var.enabled && var.email_create_compartment ? oci_identity_compartment.email[0].id : (trimspace(var.email_compartment_id) != "" ? var.email_compartment_id : local.target_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
email_subnet_id = trimspace(var.existing_email_subnet_id) != "" ? var.existing_email_subnet_id : oci_core_subnet.email[0].id
email_route_table_id = trimspace(var.existing_vcn_id) != "" ? data.oci_core_subnet.existing_public[0].route_table_id : local.public_route_table_id
bastion_target_subnet_id = trimspace(var.bastion_target_subnet_id) != "" ? var.bastion_target_subnet_id : local.public_subnet_id
bastion_client_cidrs = length(var.bastion_client_cidr_block_allow_list) > 0 ? var.bastion_client_cidr_block_allow_list : [var.admin_cidr]
bastion_ssh_source_cidr = trimspace(var.bastion_ssh_source_cidr) != "" ? var.bastion_ssh_source_cidr : (trimspace(var.existing_vcn_id) != "" ? try(data.oci_core_subnet.existing_public[0].cidr_block, "") : var.public_subnet_cidr)
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" })
}
data "oci_core_subnet" "existing_public" {
count = var.enabled && trimspace(var.existing_public_subnet_id) != "" ? 1 : 0
subnet_id = var.existing_public_subnet_id
}
resource "oci_identity_compartment" "homelab" {
count = var.enabled && var.create_compartment ? 1 : 0
compartment_id = var.tenancy_id
name = var.compartment_name
description = var.compartment_description
enable_delete = true
freeform_tags = local.base_tags
}
resource "oci_identity_compartment" "email" {
count = var.enabled && var.email_enabled && var.email_create_compartment ? 1 : 0
compartment_id = var.tenancy_id
name = var.email_compartment_name
description = "Jeannie-managed email service resources"
enable_delete = true
freeform_tags = local.base_tags
}
resource "oci_core_vcn" "homelab" {
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
dns_label = "homelab"
freeform_tags = local.base_tags
}
resource "oci_core_internet_gateway" "homelab" {
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_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 && var.nat_gateway_enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_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 && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = local.network_vcn_id
display_name = "homelab-public-routes"
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.homelab[0].id
}
freeform_tags = local.base_tags
}
resource "oci_core_route_table" "private" {
count = var.enabled && var.nat_gateway_enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = local.network_vcn_id
display_name = "homelab-private-routes"
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_nat_gateway.homelab[0].id
}
freeform_tags = local.base_tags
}
resource "oci_core_subnet" "public" {
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = local.network_vcn_id
cidr_block = var.public_subnet_cidr
display_name = "homelab-public-subnet"
dns_label = "public"
route_table_id = local.public_route_table_id
prohibit_public_ip_on_vnic = false
freeform_tags = local.base_tags
}
resource "oci_core_subnet" "private" {
count = var.enabled && trimspace(var.existing_vcn_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = local.network_vcn_id
cidr_block = var.private_subnet_cidr
display_name = "homelab-private-subnet"
dns_label = "private"
route_table_id = local.private_route_table_id
prohibit_public_ip_on_vnic = true
freeform_tags = local.base_tags
}
resource "oci_core_security_list" "email_subnet" {
count = var.enabled && var.email_enabled && trimspace(var.existing_email_subnet_id) == "" ? 1 : 0
compartment_id = local.email_compartment_id
vcn_id = local.network_vcn_id
display_name = "homelab-email-subnet-security"
freeform_tags = local.base_tags
egress_security_rules {
protocol = "all"
destination = "0.0.0.0/0"
}
}
resource "oci_core_subnet" "email" {
count = var.enabled && var.email_enabled && trimspace(var.existing_email_subnet_id) == "" ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = local.network_vcn_id
cidr_block = var.email_subnet_cidr
display_name = "homelab-email-subnet"
dns_label = "email"
route_table_id = local.email_route_table_id
security_list_ids = [oci_core_security_list.email_subnet[0].id]
prohibit_public_ip_on_vnic = false
freeform_tags = local.base_tags
}
resource "oci_core_network_security_group" "edge" {
count = var.enabled && var.edge_enabled ? 1 : 0
compartment_id = local.target_compartment_id
vcn_id = local.network_vcn_id
display_name = "homelab-edge-nsg"
freeform_tags = local.base_tags
}
resource "oci_core_network_security_group" "email" {
count = var.enabled && var.email_enabled ? 1 : 0
compartment_id = local.email_compartment_id
vcn_id = local.network_vcn_id
display_name = "homelab-email-nsg"
freeform_tags = local.base_tags
}
resource "oci_bastion_bastion" "homelab" {
count = var.enabled && var.bastion_enabled ? 1 : 0
bastion_type = "standard"
compartment_id = local.target_compartment_id
target_subnet_id = local.bastion_target_subnet_id
name = var.bastion_name
client_cidr_block_allow_list = local.bastion_client_cidrs
max_session_ttl_in_seconds = var.bastion_max_session_ttl_in_seconds
freeform_tags = local.base_tags
}
resource "oci_core_network_security_group_security_rule" "edge_ssh" {
count = var.enabled && var.edge_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
direction = "INGRESS"
protocol = "6"
source = var.admin_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 22
max = 22
}
}
}
resource "oci_core_network_security_group_security_rule" "edge_bastion_ssh" {
count = var.enabled && var.edge_enabled && var.bastion_enabled && trimspace(local.bastion_ssh_source_cidr) != "" ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
direction = "INGRESS"
protocol = "6"
source = local.bastion_ssh_source_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 22
max = 22
}
}
}
resource "oci_core_network_security_group_security_rule" "edge_web" {
count = var.enabled && var.edge_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
direction = "INGRESS"
protocol = "6"
source = "0.0.0.0/0"
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 80
max = 80
}
}
}
resource "oci_core_network_security_group_security_rule" "edge_https" {
count = var.enabled && var.edge_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
direction = "INGRESS"
protocol = "6"
source = "0.0.0.0/0"
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 443
max = 443
}
}
}
resource "oci_core_network_security_group_security_rule" "email_ssh" {
count = var.enabled && var.email_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = var.admin_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 22
max = 22
}
}
}
resource "oci_core_network_security_group_security_rule" "email_bastion_ssh" {
count = var.enabled && var.email_enabled && var.bastion_enabled && trimspace(local.bastion_ssh_source_cidr) != "" ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = local.bastion_ssh_source_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 22
max = 22
}
}
}
resource "oci_core_network_security_group_security_rule" "email_ports" {
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = "0.0.0.0/0"
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 25
max = 25
}
}
}
resource "oci_core_network_security_group_security_rule" "email_submission" {
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = "0.0.0.0/0"
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 465
max = 465
}
}
}
resource "oci_core_network_security_group_security_rule" "email_submission_starttls" {
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = "0.0.0.0/0"
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 587
max = 587
}
}
}
resource "oci_core_network_security_group_security_rule" "email_imaps" {
count = var.enabled && var.email_enabled && var.open_mail_ports ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = "0.0.0.0/0"
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 993
max = 993
}
}
}
resource "oci_core_network_security_group_security_rule" "edge_monitoring" {
count = var.enabled && var.edge_enabled && var.monitoring_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.edge[0].id
direction = "INGRESS"
protocol = "6"
source = trimspace(var.monitoring_cidr) != "" ? var.monitoring_cidr : var.admin_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 9100
max = 9100
}
}
}
resource "oci_core_network_security_group_security_rule" "email_monitoring" {
count = var.enabled && var.email_enabled && var.monitoring_enabled ? 1 : 0
network_security_group_id = oci_core_network_security_group.email[0].id
direction = "INGRESS"
protocol = "6"
source = trimspace(var.monitoring_cidr) != "" ? var.monitoring_cidr : var.admin_cidr
source_type = "CIDR_BLOCK"
tcp_options {
destination_port_range {
min = 9100
max = 9100
}
}
}
data "oci_core_instance" "existing_edge" {
count = var.enabled && var.compute_enabled && var.edge_enabled && trimspace(var.edge_existing_instance_id) != "" ? 1 : 0
instance_id = var.edge_existing_instance_id
}
resource "oci_core_instance" "edge" {
count = var.enabled && var.compute_enabled && var.edge_enabled && trimspace(var.edge_existing_instance_id) == "" ? 1 : 0
availability_domain = var.edge_availability_domain
compartment_id = local.target_compartment_id
display_name = var.edge_display_name
shape = var.edge_shape
shape_config {
ocpus = var.edge_ocpus
memory_in_gbs = var.edge_memory_in_gbs
}
source_details {
source_type = "image"
source_id = var.edge_image_id
boot_volume_size_in_gbs = var.edge_boot_volume_size_in_gbs
}
create_vnic_details {
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]
}
metadata = { ssh_authorized_keys = var.ssh_public_key }
freeform_tags = merge(local.base_tags, { role = "edge" })
}
resource "oci_core_instance" "email" {
count = var.enabled && var.compute_enabled && var.email_enabled ? 1 : 0
availability_domain = var.email_availability_domain
compartment_id = local.email_compartment_id
display_name = var.email_display_name
shape = var.email_shape
shape_config {
ocpus = var.email_ocpus
memory_in_gbs = var.email_memory_in_gbs
}
source_details {
source_type = "image"
source_id = var.email_image_id
boot_volume_size_in_gbs = var.email_boot_volume_size_in_gbs
}
create_vnic_details {
subnet_id = local.email_subnet_id
assign_public_ip = true
hostname_label = var.email_hostname_label
nsg_ids = [oci_core_network_security_group.email[0].id]
}
metadata = { ssh_authorized_keys = var.ssh_public_key }
freeform_tags = merge(local.base_tags, { role = "email" })
}