TExcelTAD
Home › Azure & Google Cloud for AWS Users › Security Groups, NSGs and VPC Firewall Rules

Security Groups, NSGs and VPC Firewall Rules

All three clouds have a stateful, distributed firewall in front of every VM. AWS security groups only allow; Azure network security groups and Google Cloud firewall rules have allow and deny with priorities. That one difference changes how you read a rule set.

On AWS this is… security groups (stateful, allow-only, attached to ENIs) and network ACLs (stateless, ordered, allow/deny, per subnet), from Security Groups vs NACLs.

Side by side

AWS security group Azure NSG Google Cloud VPC firewall rule
Stateful yes yes yes
Allow / deny allow only both both
Order none (union of allows) priority 100–4096, lower number first, first match wins priority 0–65535 (default 1000), lower first; at equal priority deny beats allow
Attached to network interfaces subnet and/or NIC (both are evaluated) the network; targets chosen by network tag, service account, or all instances
Refer to a group of VMs another security group ID application security group (ASG) network tag or service account
Built-in rules none (default SG allows from itself) default rules at 65000–65500: allow VNet-to-VNet, allow from Azure Load Balancer, deny all inbound; allow outbound implied rules at 65535: deny all ingress, allow all egress
Service names in rules prefix lists service tags (Internet, VirtualNetwork, AzureLoadBalancer, Storage.WestEurope) IP ranges; Google-defined address groups and FQDN objects in firewall policies
Org-wide rules Firewall Manager policies Azure Firewall / Virtual Network Manager security admin rules hierarchical firewall policies on org/folders, network firewall policies

Notes:

  • On Azure, an NSG on the subnet and an NSG on the NIC both apply: inbound traffic must pass the subnet NSG and then the NIC NSG. Most teams use one level (usually subnet) to keep it readable.
  • Google Cloud's "target by service account" is the strongest option: only someone who can run a VM as that service account can get its firewall rules, while network tags can be set by anyone who can edit the instance.
  • Google now recommends firewall policies (global or regional network firewall policies, plus hierarchical ones) over individual VPC firewall rules for new designs; the evaluation idea is the same.

Model: three evaluators, one packet

python · try it yourself
import ipaddress

def ip_in(ip, cidr):
    return cidr == "*" or ipaddress.ip_address(ip) in ipaddress.ip_network(cidr)

def aws_sg(rules, src, port):
    for r in rules:                                   # any allow matches -> allowed
        if ip_in(src, r["src"]) and port == r["port"]:
            return "allow"
    return "deny (no allow)"

def azure_nsg(rules, src, port):
    for r in sorted(rules, key=lambda r: r["prio"]):  # first match by priority
        if ip_in(src, r["src"]) and r["port"] in (port, "*"):
            return f"{r['access']} (rule {r['prio']} {r['name']})"
    return "deny"

def gcp_fw(rules, src, port):
    rules = rules + [{"prio": 65535, "action": "deny", "src": "*", "port": "*", "name": "implied-deny-ingress"}]
    best = min(r["prio"] for r in rules if ip_in(src, r["src"]) and r["port"] in (port, "*"))
    hits = [r for r in rules if r["prio"] == best and ip_in(src, r["src"]) and r["port"] in (port, "*")]
    win = next((r for r in hits if r["action"] == "deny"), hits[0])   # deny wins ties
    return f"{win['action']} (priority {best} {win['name']})"

nsg = [
    {"prio": 100, "access": "Allow", "src": "*", "port": 443, "name": "allow-https"},
    {"prio": 110, "access": "Deny", "src": "203.0.113.0/24", "port": "*", "name": "block-bad-range"},
    {"prio": 65500, "access": "Deny", "src": "*", "port": "*", "name": "DenyAllInBound"},
]
gcp = [
    {"prio": 1000, "action": "allow", "src": "0.0.0.0/0", "port": 443, "name": "allow-https"},
    {"prio": 1000, "action": "deny", "src": "203.0.113.0/24", "port": "*", "name": "block-bad-range"},
]
sg = [{"src": "0.0.0.0/0", "port": 443}]

for src, port in [("198.51.100.7", 443), ("203.0.113.9", 443), ("198.51.100.7", 22)]:
    print(f"{src}:{port}")
    print(f"   AWS SG    {aws_sg(sg, src, port)}")
    print(f"   Azure NSG {azure_nsg(nsg, src, port)}")
    print(f"   GCP FW    {gcp_fw(gcp, src, port)}")
198.51.100.7:443
   AWS SG    allow
   Azure NSG Allow (rule 100 allow-https)
   GCP FW    allow (priority 1000 allow-https)
203.0.113.9:443
   AWS SG    allow
   Azure NSG Allow (rule 100 allow-https)
   GCP FW    deny (priority 1000 block-bad-range)
198.51.100.7:22
   AWS SG    deny (no allow)
   Azure NSG Deny (rule 65500 DenyAllInBound)
   GCP FW    deny (priority 65535 implied-deny-ingress)

The middle case is the lesson. The same two intentions ("allow HTTPS", "block that range") give different answers:

  • The AWS security group can't express a deny at all; you'd need a NACL or WAF.
  • The Azure NSG lets the bad range in, because allow-https has a lower priority number and matches first. Fix: give the block rule priority 90.
  • Google Cloud blocks it, because at equal priority a deny wins.

When you translate rules between clouds, translate the order, not just the rules.

Real commands

Azure, sample session: an NSG with an application security group as the target.

bash
az network asg create -g rg-shop-prod-app -n asg-web
az network nsg create -g rg-shop-prod-network -n nsg-snet-app
az network nsg rule create -g rg-shop-prod-network --nsg-name nsg-snet-app -n block-bad-range \
    --priority 90 --direction Inbound --access Deny --protocol '*' \
    --source-address-prefixes 203.0.113.0/24 --destination-port-ranges '*'
az network nsg rule create -g rg-shop-prod-network --nsg-name nsg-snet-app -n allow-https-web \
    --priority 100 --direction Inbound --access Allow --protocol Tcp \
    --source-address-prefixes Internet --destination-asgs asg-web --destination-port-ranges 443
az network vnet subnet update -g rg-shop-prod-network --vnet-name vnet-shop-prod -n snet-app \
    --network-security-group nsg-snet-app

Google Cloud, sample session: rules target a service account rather than tags.

bash
gcloud compute firewall-rules create allow-https-web --network=shop-vpc \
    --direction=INGRESS --action=ALLOW --rules=tcp:443 --source-ranges=0.0.0.0/0 \
    --target-service-accounts=web@shop-prod-4821.iam.gserviceaccount.com --priority=1000
gcloud compute firewall-rules create allow-health-checks --network=shop-vpc \
    --direction=INGRESS --action=ALLOW --rules=tcp:8080 \
    --source-ranges=35.191.0.0/16,130.211.0.0/22 \
    --target-service-accounts=web@shop-prod-4821.iam.gserviceaccount.com
gcloud compute firewall-rules list --filter="network=shop-vpc" \
    --format="table(name, direction, priority, sourceRanges.list(), allowed[].map().firewall_rule().list())"

The second rule is a classic GCP gotcha: Google's load balancer health checks come from 35.191.0.0/16 and 130.211.0.0/22, and the implied deny-ingress rule blocks them until you allow them. Azure's default NSG rules already allow the AzureLoadBalancer service tag.

Test yourself

An Azure NSG has Allow 443 from Internet at priority 200 and Deny from 203.0.113.0/24 at priority 300. A packet from 203.0.113.5 to port 443 arrives. What happens?

  1. Denied: deny always wins
  2. Allowed: priority 200 matches first
  3. Dropped by the implied rule
  4. Depends on the NIC

Two Google Cloud firewall rules match with the same priority, one allow and one deny. Which applies?

  1. The allow
  2. The deny
  3. The one created first
  4. Both are ignored

What does Google Cloud do with ingress traffic that no firewall rule allows?

  1. Allows it
  2. Denies it with the implied deny-ingress rule at priority 65535
  3. Sends it to Cloud NAT
  4. Logs and allows it