Draw VCN, Subnet and Gateway Responsibilities
A VCN is a regional address plan; a subnet is a configuration unit. The VCN owns one or more IPv4 blocks between /16 and /30, and every subnet must fit inside one of them without overlap. The subnet itself owns no rules: oci_core_subnet points at one route table, up to five security lists and one DHCP options set, and every VNIC placed in it inherits that choice.
Choose regional subnets unless placement is the point. Leave availability_domain unset and the subnet spans every availability domain in the region, so a replacement instance can land in any of them without a second CIDR. An AD-specific subnet is still correct when a shape, a licence or a latency budget pins the instance, but it multiplies the address plan by the number of domains.
Three addresses per subnet are never usable. OCI reserves the network address, the broadcast address and the first host address, which acts as the subnet's default gateway. A /24 therefore yields 251 addresses rather than 254, and a /16 divided into /24s loses three addresses in every slice.
Local traffic never reads a route rule. Traffic between subnets in the same VCN follows an implicit local route that does not appear in the table. Route rules are for destinations outside the VCN, plus the deliberate intra-VCN override that sends traffic through an appliance or a DRG.
Follow one packet from the VNIC outward. The source VNIC's NSGs and its subnet's security lists are evaluated first, as a single union of allow rules. The subnet's route table then selects the longest matching prefix, and a packet with no match is dropped rather than handed to a default. The destination's rules are evaluated only after the packet arrives.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
N["VNIC in a subnet
private IP plus NSGs"] --> F{"Security union
NSGs and subnet lists"}
F -- "no allow rule" --> X["Dropped at the VNIC
timeout, not refused"]
F -- "allowed" --> R["Subnet route table
longest prefix match"]
R -- "VCN CIDR" --> L["Implicit local route
same VCN, any subnet"]
R -- "0.0.0.0/0 on a public subnet" --> I["Internet gateway
inbound and outbound"]
R -- "0.0.0.0/0 on a private subnet" --> NA["NAT gateway
outbound only"]
R -- "service CIDR label" --> SG["Service gateway
OCI services, same region"]
R -- "remote prefix" --> D["DRG or LPG
on-premises or peered VCN"]
R -- "no matching prefix" --> B["Blackhole
no implicit default"]
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class F,R primary;
class N,L,I,NA,SG,D secondary;
class X,B danger;
The figure separates three questions that are easy to merge. Security decides whether the packet may leave, the route table decides which exit it takes, and the gateway decides what that exit can reach. The red branch is the quietest failure of the three: a valid packet with no matching prefix, dropped without a refusal.
A gateway is chosen by destination, not by subnet name. An internet gateway carries traffic in both directions but needs a public address, a subnet that permits one, a route and a matching rule. A NAT gateway carries outbound traffic only and never accepts a connection. A service gateway reaches Oracle services in the same region without any public address at all. A DRG or an LPG is the private exit to an on-premises or peered network, and it works only when both sides route the prefix.
Public is a property, not a label. A subnet is public when it permits public IPs; reaching the internet additionally needs an internet gateway route, an assigned address and a security rule. A subnet named public with no gateway route is private in everything but the name, which is why the route table is the artifact to review.
Choose Route and Security Collection Owners
Two rule collections, one evaluated set. NSGs attach to VNICs and security lists attach to subnets. OCI applies the union of both, so a packet is allowed if any rule in any attached collection allows it. Rules are allow-only, so there is no deny rule that can override a broad allow.
The union makes duplicate rules a defect. A 443 rule written into both the API NSG and the subnet's security list cannot be withdrawn by deleting either copy. A reviewer reads the NSG, sees the rule gone, and concludes the port is closed while the security list still admits it. Choose NSG for per-workload posture, keep the security list as a minimal shared baseline, and never encode one intent twice.
The default security list starts permissive. A new VCN ships a default list that admits SSH and allows all egress, which is convenient for a first login and wrong as a long-lived baseline. Replace it with a custom list, or with an empty one when every workload carries an NSG, and treat the default as scaffolding.
NSG rules are objects; route and list rules are lists. Each oci_core_network_security_group_security_rule is its own resource, so two stacks can add disjoint rules to one group without erasing each other. Terraform manages a route table's rules and a security list's rules as one list per table or list, so the last writer owns the whole collection.
Give every collection exactly one writer. The network stack owns the VCN, subnets, gateways, route tables and the baseline security list. Workload stacks own NSGs, VNICs and instances, and receive subnet and route table identifiers as inputs. A workload module that declares a route table for a shared subnet is a review defect even when the plan is clean.
The default prefix has one author. On a private table, 0.0.0.0/0 usually belongs to the NAT gateway; on a public table, to the internet gateway. It should never have two authors and never a workload. Two authors for the same prefix produce drift rather than a merge, and the plan does not tell you which one the data plane kept.
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
P["Choose the security boundary"] --> Q{"Subnet-wide
or per VNIC?"}
Q -- "subnet-wide" --> SL["Security list
attached to the subnet"]
Q -- "per workload" --> NSG["NSG
attached to the VNIC"]
SL --> DUP{"Same rule also written
into an NSG?"}
DUP -- "yes" --> F1["Union allows it twice
removing one copy does nothing"]
DUP -- "no" --> OK1["One owner
one removal closes the port"]
NSG --> RT{"Who writes the
route table?"}
RT -- "network stack" --> OK2["Single writer
prefixes survive"]
RT -- "workload stack" --> F2["Whole rule list replaced
other prefixes disappear"]
OK1 --> T["Test an allowed port
and a denied one"]
OK2 --> T
classDef primary fill:#132440,color:#ffffff,stroke:#0b172b;
classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a;
classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f;
classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420;
classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f;
class Q,DUP,RT primary;
class P,SL,NSG,T secondary;
class F1,F2 danger;
class OK1,OK2 ok;
The amber decisions are the ones that pass plan and fail review. Duplicating a rule feels harmless because both copies agree today; the failure appears months later, when someone removes one copy and the port stays open. Editing a shared table from a workload module looks local until the next apply removes a prefix another team depended on.
This article targets Terraform 1.17.1 and oracle/oci 9.12.1. In that provider the NSG rule is a separate resource, while oci_core_route_table and oci_core_security_list carry their rules inline. That difference is not cosmetic: it decides which collections two teams can safely share.
Plan Private Service Access and DNS Explicitly
A service gateway is regional, per-VCN and not transitive. One oci_core_service_gateway serves a VCN, and any subnet whose table routes to it can use it. A peered VCN cannot borrow that gateway; it needs its own, or traffic must transit an appliance in the VCN that has one.
Route by service label, not by a guessed CIDR. The rule names a service CIDR label, such as the region's Object Storage label, and marks the destination as a service. The label tracks Oracle's published ranges, so an address change on Oracle's side does not silently break the rule.
Match the gateway label and the route label. If the gateway enables only the Object Storage label and the table routes the all-services label, every other service in that label is blackholed. The console blocks that combination; nothing in the route table itself does, so the check belongs in review or a policy test.
One label per gateway and per table. Object Storage appears inside both labels, and OCI permits only one of them on a gateway or in a route rule. Adding a second rule for the other label does not widen access; it creates the ambiguous table the console refuses to build.
A private endpoint is an address in your subnet. An Autonomous Database private endpoint or an OKE private API endpoint takes an IP from a subnet you name and the NSGs you attach. Reachability then follows the ordinary path: the client's rules, the route table, the endpoint subnet's rules and the endpoint's own NSG. No service gateway is involved in that direction.
Names are a separate grant from routes. The built-in resolver answers names of the form host.subnet.vcn.oraclevcn.com when both the VCN and the subnet carry DNS labels. A custom private zone answers only clients whose DHCP options point at the custom resolver, so a zone can exist, hold the right record, and still be invisible to the workload.
A wrong name and a wrong route fail differently. A public address returned for a private service does not match the service label, so the packet leaves by NAT or is dropped. A correct private address with no route times out on the private path. Test resolution and reachability as two separate checks, and run both from inside the VCN.
Use Compartments and Shared Networks Deliberately
A compartment is a permission boundary, not a network boundary. The VCN, subnets and gateways sit in a network compartment; workloads sit in their own. The packet path does not change, but the blast radius of a mistaken policy does.
Inspect is stronger than it sounds. The inspect verb returns rule contents, not just resource names, so an auditor can read every route and security rule without the power to change one. That makes inspect right for review tooling and wrong for a team you would not hand the network map.
Use attaches; manage rewrites. The use verb lets a workload attach a VNIC to an existing subnet and read what it needs, but it cannot update route tables, security lists, internet gateways or DHCP options. Those updates need manage, and OCI separates the two deliberately: creating a component also needs manage on the VCN, while updating an existing one does not.
An attachment is a join, not a permission. A DRG attachment, an LPG peering or a cross-compartment subnet reference connects two networks, and each is a separate object with its own lifecycle. None of them carries a route or a rule with it. An attachment in the plan with no route on either side is a join that drops every packet.
Put the owner in the module interface. Pass subnet and NSG identifiers into a workload module, not CIDRs and not route table identifiers the workload may edit. A shared network stays safe when the only thing a workload can do is attach to it.
| Boundary | Held by | Grants | Withholds |
|---|---|---|---|
| Network compartment | platform network team | VCN, subnets, gateways, route tables and baseline lists | instance launch in workload compartments |
| Workload compartment | application team | instances, VNICs and workload NSGs | route tables and gateways in the network compartment |
use on the network compartment | application team | attaching a VNIC to an existing subnet | editing routes, security lists or gateways |
| DRG or LPG attachment | network team, change-reviewed | a path to another VCN or an on-premises network | routes, rules and name resolution by itself |
The panels differ by one policy statement. The left panel is a shared VCN where every attachment holds use, so the route table has a single author. The right panel grants manage to one workload team, and the most contested prefix in the network becomes editable by the group with the least context.
Verify a Private Path and a Denied Public Route
The runner proves the control plane, not the data plane. A laptop or CI agent calls the OCI API over the internet and can create a private path it cannot use. Keep the apply as evidence that the objects exist, then run the traffic tests from a host inside the VCN.
A private instance still gives you a shell. The Oracle Cloud Agent run-command service executes a command on an instance with no public address, so the test does not require opening SSH or changing the boundary under test. The command runs with the instance's VNICs, NSGs and route table, which is the path you are trying to prove.
Delete the gateway last, and let the graph know. A gateway that is still a route target cannot be deleted, and a hardcoded OCID hides that dependency from Terraform. Reference the gateway resource in the route rule so destroy orders the rule before the gateway, then check the state list rather than the console.
Verify a Private Path and a Denied Public Route
Status: design and pass conditions. No OCI tenancy was queried for this article, so the values below describe the intended topology and the observations that would prove it, not captured output.
Topology. One regional VCN at 10.40.0.0/16, a private regional subnet at 10.40.10.0/24, a public regional subnet at 10.40.20.0/24, an internet gateway, a NAT gateway and a service gateway. A client and a server sit in the private subnet, each with its own NSG and no public IP.
Steps.
- Apply the network stack alone. Confirm the private subnet has no availability domain and does not permit public IPs.
- Read the private route table: the region's service label points at the service gateway, 0.0.0.0/0 points at NAT, and no rule points at the internet gateway.
- Launch the client and server with no public IP, each in its own NSG.
- Allow TCP 443 from the client NSG to the server NSG. Leave 8080 absent, and confirm the subnet's security list has no overlapping ingress rule.
- From the runner, use the instance run-command service to request the server's private DNS name on 443 from the client.
- Repeat the same request on 8080.
- From the client, request the region's Object Storage endpoint and confirm the connection returns an HTTP response rather than a timeout, with no public address on either VNIC.
- From the runner, attempt a direct connection to the client's private address.
- Destroy the stack. Confirm no route rule still targets a gateway, no reserved public IP remains, and the state list is empty.
| Check | Where it runs | Pass condition | What a failure means |
|---|---|---|---|
| Allowed 443 | run command on the client | connection completes and returns bytes | NSG union, route table or listener |
| Denied 8080 | the same command | timeout with no response | any response means a rule is too wide |
| Service gateway | client to the OCI endpoint | an HTTP response, no public address | wrong label or missing route |
| Public inbound | runner to the private address | no route, no response | success means a public address or an IGW path |
| Name resolution | client resolves the FQDN | an address inside 10.40.10.0/24 | missing DNS label or wrong resolver |
Expected observation. The 443 request completes, the 8080 request times out without a response, and the Object Storage request completes while both VNICs keep private addresses. The runner's direct connection fails because the private subnet has no internet gateway route and the client has no public address. The server's name resolves to an address in 10.40.10.0/24.
Failure check. The lab fails if 8080 answers, if the runner reaches the private address, if the Object Storage request needs a public address, if the name resolves to a public address, or if a gateway delete is blocked by a route rule left in a table.
Cleanup and evidence. Keep the plan for the private route table, both NSG rule listings, the security list listing, the DHCP options and resolver configuration, and the state list after destroy. The NAT gateway and any reserved public IP bill while they exist, so destroy them with the lab rather than leaving them for the next reader.
Next in the Series
In OCI Compute: Shapes, Availability Domains, VNICs and Load-Balancer Health, we'll explore Provision compute capacity with explicit image/shape choices and verified application readiness.