Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 35 ยท Droplets and Networking

DigitalOcean Cloud Firewalls: Rules, Tags and Best Practices

Protect Droplets with free, stateful Cloud Firewalls: inbound and outbound rules, tag-based targeting, source types, doctl examples and a rule evaluator to test your design.

A server on the public internet is probed within minutes of being created. The simplest and most effective defense is to allow only the traffic you actually need. DigitalOcean's Cloud Firewalls do exactly that. They are free, they run in the network in front of your Droplets (not inside the operating system), and they apply to many Droplets at once through tags.

How Cloud Firewalls behave

  • Stateful. If an inbound connection is allowed, the reply traffic is allowed automatically. You do not write a rule for the answers.
  • Default deny for inbound. Once a Droplet is covered by a firewall, any inbound traffic that no rule allows is dropped. With no inbound rules at all, nothing from outside gets in.
  • Outbound. A firewall can also limit outgoing traffic. Many teams allow all outbound traffic and tighten it only for sensitive servers.
  • Outside the VM. Packets are filtered before they reach your Droplet, so a misconfigured ufw or a compromised root user cannot silently disable it.
  • Several firewalls can apply at once. The effective policy is the union of all allow rules from every firewall attached to a Droplet.

A firewall is attached to Droplets either by name or, better, by tag: every Droplet with the tag web automatically receives the rules.

Anatomy of a rule

Each rule has three parts:

Part Examples
Protocol tcp, udp, icmp
Ports 22, 80, 443, 5432, a range such as 8000-8100, or all
Sources (inbound) or destinations (outbound) IP addresses and CIDRs, Droplets, tags, load balancers, Kubernetes clusters

Using other DigitalOcean objects as the source is powerful. A rule "port 5432 from tag web" lets only your web servers talk to the database, even when their IPs change.

A sensible design

For a small production system:

  1. web-fw applied to tag web:
    • inbound tcp/80 and tcp/443 from anywhere (0.0.0.0/0, ::/0);
    • inbound tcp/22 from your office or VPN IP only.
  2. db-fw applied to tag db:
    • inbound tcp/5432 from tag web only;
    • inbound tcp/22 from your bastion tag only.
  3. Outbound allowed for everything (so package updates and API calls work).

Servers never need to expose SSH to the world. If your IP changes often, use a small VPN or a bastion Droplet and allow SSH only from there.

Creating one with doctl

Terminal
doctl compute firewall create \
  --name web-fw \
  --tag-names web \
  --inbound-rules "protocol:tcp,ports:80,address:0.0.0.0/0,address:::/0 protocol:tcp,ports:443,address:0.0.0.0/0,address:::/0 protocol:tcp,ports:22,address:203.0.113.7/32" \
  --outbound-rules "protocol:tcp,ports:all,address:0.0.0.0/0,address:::/0 protocol:udp,ports:all,address:0.0.0.0/0,address:::/0 protocol:icmp,address:0.0.0.0/0,address:::/0"

doctl compute firewall list --format ID,Name,Status
doctl compute firewall add-droplets <firewall-id> --droplet-ids 314159

The same firewall in the API is a POST /v2/firewalls with inbound_rules, outbound_rules, droplet_ids and tags fields.

Firewall on top of the OS firewall

You can still run ufw or nftables on the Droplet as a second layer. Be careful with Docker: published container ports are inserted into the host's firewall rules ahead of ufw, so ufw does not always block them. A Cloud Firewall, being outside the host, does not have that problem. That is one more reason to rely on it for the perimeter.

flowchart LR
  I(("Internet")) --> F{"Cloud Firewall<br/>tag: web"}
  F -- "80/443 allowed" --> W["web Droplet"]
  F -- "22 from office IP" --> W
  F -- "everything else" --> X["dropped"]
  W -- "5432 from tag web" --> F2{"Cloud Firewall<br/>tag: db"} --> D["db Droplet"]

Each tier has its own firewall; the database only accepts traffic from the web tag.

Stand-in: a firewall rule evaluator

This program models a simplified firewall: rules with protocol, port and source (a CIDR or a tag). It answers "would this packet be allowed?" for a few scenarios. It is a stand-in that illustrates default-deny matching; the real service also handles statefulness, port ranges and more.

Python Stand-in: evaluate inbound packets against rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: find overly open rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Operating tips

  • Tags first. Give each role a tag (web, db, bastion) and attach firewalls to tags, never to individual Droplets, unless you are testing.
  • Test with a second session before closing your SSH connection after changing the firewall.
  • Use /32 for single addresses. 203.0.113.7/32 means exactly one IP.
  • Remember IPv6. If a Droplet has IPv6 enabled, add ::/0 rules deliberately, or leave IPv6 off.
  • Log and review. Firewalls do not log dropped packets for you; combine them with monitoring and host logs.
  • Managed databases have their own trusted sources list that works like a firewall at the database level (Managed databases).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does 'stateful' mean for a Cloud Firewall?

    You allow the request direction; the replies for that connection pass without extra rules.
  2. 2. Why attach firewalls to tags instead of individual Droplets?

    Tag targeting scales and prevents forgotten servers.
  3. 3. A rule allows tcp/5432 from tag 'web'. Who can connect to the database on that port?

    Using a tag as the source limits access to resources that carry it.
  4. 4. Which rule is most dangerous?

    Exposing a database to the whole internet invites brute-force and exploit attempts.

Summary

  • Cloud Firewalls are free, stateful, default-deny for inbound traffic and sit outside the Droplet.
  • Rules combine protocol, ports and sources; sources may be CIDRs, Droplets, tags, load balancers or clusters.
  • Attach firewalls to tags and design one policy per tier: public web, restricted SSH, database only from the web tag.
  • Do not rely on ufw alone when Docker publishes ports.

Next: Reserved IPs and DNS