Cloud & Data Centers

Cloud Networking Basics: Subnets, Gateways and Security Groups

Build a working mental model of cloud networks: plan subnets, route through gateways, write firewall rules and troubleshoot.

Key takeaways

  • A connection needs a route in both directions and a rule that permits it.
  • Subnets group resources, while route tables determine whether a subnet is exposed or private.
  • Security groups are typically stateful, so replies to an allowed connection are let back automatically.
  • Referencing one security group from another is safer and easier to maintain than listing IP addresses.
  • Troubleshoot in order: name, address, route, firewall rules, then whether the service is listening.

A cloud network rests on three ideas. Subnets decide where a machine sits and what address it gets. Gateways and routes decide which destinations its traffic can reach. Security groups, a cloud provider's resource-level firewall rules, decide which connections are allowed once a path exists.

When something can't connect, one of those three is usually the reason, along with names and the application itself. This guide builds a small web setup piece by piece and shows how to check each layer. The examples lean on common AWS-style terms; other providers have equivalents with their own details.

Subnets: carving up the address space

You start with a virtual private cloud and a block of private addresses, say 10.40.0.0/16. Subnets are smaller blocks inside it.

The number after the slash is the prefix length. Each step up halves the block: a /24 holds 256 addresses, a /25 holds 128 and a /26 holds 64. The provider keeps a few in each subnet for itself, so very small subnets lose a noticeable share.

For a first design, /24 subnets are comfortable. Platforms that give every container its own address can consume them quickly, so size those subnets generously. The CIDR calculator shows the first and last address of any block, and subnet masks explains the arithmetic.

Plan for at least two availability zones, with a matching set of subnets in each.

Route tables

A route table is a short list of destination ranges and targets. A public subnet's table might read:

Destination      Target
10.40.0.0/16     local
0.0.0.0/0        internet-gateway

And a private application subnet's:

Destination      Target
10.40.0.0/16     local
0.0.0.0/0        nat-gateway

0.0.0.0/0 means "any IPv4 destination not matched more specifically". The longest, most specific prefix wins, so traffic to another machine in the VPC uses the local route even though it also matches the default. How routers choose a path covers the principle.

A data subnet may have only the local route. Nothing in it can reach the internet directly, and nothing there can be reached from it.

Gateways

An internet gateway connects the VPC to the public internet. A machine needs both a route to it and a public address to be reached directly.

A NAT gateway sits in a public subnet. Private machines send outbound traffic to it; it substitutes its own public address and remembers the connection so replies come back. Outsiders can't open new connections through it. For resilience, many designs place one in each zone.

IPv6 works differently, since addresses are commonly globally unique. Some providers offer an outbound-only gateway for IPv6, and the firewall rules still matter. Remember to write IPv6 rules deliberately if you enable it; 0.0.0.0/0 covers only IPv4, and ::/0 is the IPv6 equivalent.

Security groups

A security group is a set of allow rules attached to a resource such as a virtual machine, load balancer or database. In AWS's version you only write allows; anything not allowed is dropped.

Security groups are stateful: if an inbound connection is allowed, the reply goes out without a separate rule, and the same applies to replies to connections your resource starts.

A three-tier example looks like this:

Group Inbound allowed From
lb-sg TCP 443 (HTTPS) Anywhere
app-sg TCP 8080 lb-sg
db-sg TCP 5432 (PostgreSQL) app-sg

The source in the second and third rows is another group, not an address range. New app servers join app-sg and automatically gain access to the database. Nothing else does.

Each tier accepts only the one before it
1 Internet May reach HTTPS on the load balancer 2 Load balancer May reach the app port 3 App servers May reach the database port 4 Database Accepts nothing from anywhere else Internet May reach HTTPS on the load balancer 1 Load balancer May reach the app port 2 App servers May reach the database port 3 Database Accepts nothing from anywhere else 4

Outbound rules

Many default groups allow all outbound traffic. That's convenient, and it also means a compromised server can send data anywhere. Tightening egress, the term for outbound traffic, limits the damage.

Start by listing what each tier has to reach: the database, the provider's storage and logging services, package repositories for updates, any outside APIs. Allow those, and watch the logs for what you missed. Security groups work with addresses and groups, so rules based on domain names usually need a proxy or the provider's managed network firewall.

Databases rarely need to start connections to the internet at all. A data subnet with no default route and a narrow outbound rule is a simple, strong arrangement.

Why the stateful difference matters

Suppose an app server queries a database. With security groups, you allow the database port inbound on db-sg from app-sg, and the reply is permitted automatically.

With a stateless filter you must think about both directions. The query arrives on port 5432. The reply leaves from port 5432 toward a temporary, high-numbered port, called an ephemeral port, that the client's operating system picked. The outbound rule on the database subnet must allow that range, and the app subnet's inbound rule must too. Forgetting this produces a baffling symptom: the request arrives, the server answers and the client still times out. How ports work explains why the client's port changes each time.

Network ACLs and other layers

Some providers add a subnet-level filter. AWS calls it a network access control list (ACL). It differs from security groups in important ways: it's stateless, so return traffic needs its own rule, and it supports explicit denies, evaluated in numbered order.

Two common filters, AWS-style
Security group Attached to a resource Stateful: replies pass Allow rules only All rules considered together Network ACL Attached to a subnet Stateless: allow both directions Allow and deny rules Evaluated in number order VS Security group Attached to a resource Stateful: replies pass Allow rules only All rules considered together Network ACL Attached to a subnet Stateless: allow both directions Allow and deny rules Evaluated in number order

Most designs do the real work with security groups and leave ACLs permissive, using them for broad blocks. Stacking strict rules in both places doubles the opportunities for confusion. A web application firewall is another layer again, inspecting the content of web requests.

Getting in to manage servers

Avoid opening SSH (port 22) or Remote Desktop (port 3389) to 0.0.0.0/0. Automated scanners find exposed ports quickly. Better options, roughly in order of preference:

  1. The provider's session or console service, tied to your cloud identity and logged.
  2. A VPN into the VPC.
  3. A hardened jump host, restricted to known source addresses.

Whatever you choose, require strong sign-in with multi-factor authentication. The thinking behind identity-based access is covered in zero trust networking.

Troubleshooting a connection

Work from the outside in.

  1. Name. Does it resolve to the address you expect?
  2. Address. Are you using the private address from inside and the public one from outside?
  3. Routes. Do both subnets have a path to each other?
  4. Rules. Does the destination's group allow that port from that source? Do any subnet ACLs block the reply?
  5. Service. Is the program running, listening on the right port and on more than just 127.0.0.1?
  6. Host firewall. The operating system may have its own.

On a Linux machine you can see what's listening:

ss -tlnp

And test a port from a client:

nc -vz db.example.com 5432

In Windows PowerShell:

Test-NetConnection db.example.com -Port 5432

A timeout often means packets are being silently dropped by a rule or missing route. Connection refused usually means you reached the machine but nothing is listening on that port, or something actively rejected you. These aren't guarantees, but they tell you where to look first. Flow logs can show whether the network accepted or rejected a given connection.

A connection fails: where to look
Does the name resolve to the wrong address, or not at all? YES Fix DNS records or which resolver the client uses 1 NO Is a route missing in either direction? YES Correct the subnet route tables or peering routes 2 NO Does a rule block the port or the reply? YES Check security groups, subnet filters and host firewall 3 NO Is nothing listening on that port and address? YES Start the service or change what it binds to 4 NO Look at the application Read its logs, credentials and timeouts Does the name resolve to the wrong address, or not at all? 1 IF YES Fix DNS records or which resolver the client uses NO Is a route missing in either direction? 2 IF YES Correct the subnet route tables or peering routes NO Does a rule block the port or the reply? 3 IF YES Check security groups, subnet filters and host firewall NO Is nothing listening on that port and address? 4 IF YES Start the service or change what it binds to NO Look at the application Read its logs, credentials and timeouts

Mistakes worth avoiding

  • Using one wide-open security group for everything because it "fixed" a problem during testing.
  • Choosing an address range that overlaps your office network.
  • Putting a database in a public subnet with a public address.
  • Hard-coding private IP addresses that change when a machine is replaced. Use names.
  • Forgetting outbound rules where they've been tightened, so updates fail.
  • Leaving rules nobody can explain. Add a description to each.

Manage the whole network with infrastructure as code once it grows beyond a few resources. A reviewed change to a text file is easier to audit than a series of console clicks, and you can rebuild the same layout in a test account.

A short build order

If you're setting this up for the first time, build in an order that lets you test as you go.

  1. Create the network and choose the address block, writing it down in a shared list of used ranges.
  2. Add public, application and data subnets in two zones.
  3. Attach an internet gateway and give only the public subnets a default route to it.
  4. Add a NAT gateway if private machines need outbound access, and point the application subnets at it.
  5. Create the three security groups before launching anything, each with a description.
  6. Launch one application server, confirm it can fetch updates and confirm you can't reach it directly from outside.
  7. Add the database and test the connection from the app server, then from somewhere that should be refused.

Delete test resources when you finish experimenting. Gateways and public addresses can keep billing while nothing useful runs behind them.

Where to go next

With subnets, routes and rules in hand, the next pieces are usually a load balancer in front of several app servers, health checks that remove broken ones and a way to link the network to your office. Add them one at a time and test each path, including the paths that should fail. Confirming that the database refuses a connection from the wrong place is as informative as confirming that the website loads.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Cloud & Data Centers →

Latest news on this topic