Security groups and network ACLs
-
Phase 0: Before you start
-
Phase 1: The foundation
-
Phase 2: Operations and security
-
Phase 3: SQL Server on AWS
-
Phase 4: Troubleshooting step by step
-
Phase 5: Final project and career
Any packet that reaches a server in AWS has to get past two firewalls you control. This lesson on security groups vs NACL (network access control lists) covers what each one does, why one remembers connections while the other doesn’t, and which one you should go to first.
You’ll build the three security groups the rest of the course relies on, web, app and database, each open only to the tier in front of it. Security groups and network ACLs cost nothing, so the groups can stay in your account.
| Time | You need | By the end you can |
|---|---|---|
| 50 minutes | saat-vpc and saat-vpc.json from Lessons 8 and 9, plus your laptop signed in with aws login --profile saat-admin. | Explain stateful and stateless filtering, build a chain of security groups using group references, read and test network ACL rules, and fix duplicate-rule and dependency errors. |
What it is, in one line
Security groups are stateful allow-lists around each server, while network ACLs are stateless, numbered allow and deny rules at the edge of a subnet.
In Simple Words
A guard at the flat door and a gate at the colony
A security group is like the guard at the door of one flat. He has a list of who’s allowed in, and when you pop out to buy milk he remembers you and lets you back in without looking at the list. A network ACL is the gate at the entrance to the whole colony, with a numbered rule board and no memory at all, so if the board says “visitors in through gate 3”, it also has to say “visitors out through gate 7” or people get stuck inside. Most days you only deal with the flat guard, and the colony gate is kept for big, blunt rules like “nobody from that street, full stop”.
Engineer's View
How the two filters work
A security group attaches to network interfaces (instances, load balancers, RDS, Lambda in a VPC). It only has allow rules, inbound and outbound, and all of them are evaluated together, so if any rule allows the traffic, it gets through.
It’s stateful, which means that once an inbound connection is allowed, the reply goes out automatically, whatever the outbound rules say. A new group allows all outbound traffic and no inbound traffic.
A rule’s source can be an IP range or another security group. “Allow 1433 from saat-app-sg” means “from any interface that has saat-app-sg attached”, and that’s how real tiers are wired together, because IP addresses change but group membership doesn’t.
A network ACL attaches to subnets. Its numbered inbound and outbound rules are evaluated from the lowest number up, and the first match wins, deny rules included. It’s stateless, so replies get checked like any other packet.
Replies to a client go to a short-lived high port (an ephemeral port, usually 1024 to 65535), which means an ACL that lets 443 in also has to let those ports out. The default ACL allows everything, whereas a new custom ACL denies everything until you add rules.
How it works, step by step
This is the path a request takes to reach a server in a public subnet.
1The subnet’s network ACL checks it
The inbound rules are checked from the lowest number up. In saat-vpc we’re keeping the default ACL, which allows everything, so this check passes.
2The security group checks it
Does any inbound rule match the protocol, port and source? If none does, the packet is dropped without a word, and the client only ever sees a timeout, never a “denied” message.
3The reply goes back
The security group lets the reply out because it remembers the connection, but the network ACL checks the reply against its outbound rules as if it were a brand-new packet.
4The next tier repeats it
The app server calls the database on 1433, and saat-db-sg only allows it because the caller has saat-app-sg. A server without that group can’t connect, even from the same subnet.
Side by side, the two filters compare like this.
And here’s the chain you’re about to build. Lessons 11 to 55 attach these groups to real servers and databases.
✅ Checkpoint: You can explain stateful versus stateless, and why a security group rule pointing at another group beats one that lists IP addresses.
Hands-on lab: security groups vs NACL in saat-vpc
Before you run any command, double-check it
Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.
Start the usual way and load the network:
cd C:\aws-lab
aws login --profile saat-admin
$env:AWS_PROFILE = 'saat-admin'
$v = Get-Content C:\aws-lab\saat-vpc.json | ConvertFrom-Json1Create the three security groups
Paste this whole block:
function New-Sg($Name, $Desc) {
aws ec2 create-security-group --group-name $Name --description $Desc --vpc-id $v.VpcId --tag-specifications "ResourceType=security-group,Tags=[{Key=Name,Value=$Name},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=10}]" --query GroupId --output text
}
$web = New-Sg 'saat-web-sg' 'Web tier: HTTP and HTTPS from the internet'
$app = New-Sg 'saat-app-sg' 'App tier: 8080 from the web tier'
$db = New-Sg 'saat-db-sg' 'Database tier: SQL Server from the app tier'
$web, $app, $dbYou should see three IDs, something like this:
sg-0a1b2c3d4e5f60718
sg-0b2c3d4e5f6a70819
sg-0c3d4e5f6a7b8091a2Add the inbound rules
Paste this whole block:
aws ec2 authorize-security-group-ingress --group-id $web --protocol tcp --port 443 --cidr 0.0.0.0/0 | Out-Null
aws ec2 authorize-security-group-ingress --group-id $web --protocol tcp --port 80 --cidr 0.0.0.0/0 | Out-Null
aws ec2 authorize-security-group-ingress --group-id $app --protocol tcp --port 8080 --source-group $web | Out-Null
aws ec2 authorize-security-group-ingress --group-id $db --protocol tcp --port 1433 --source-group $app | Out-Null3Read the rules back
Run this. Note the single quotes, because the backticks inside are part of the query:
aws ec2 describe-security-group-rules --filters "Name=group-id,Values=$db" --query 'SecurityGroupRules[?IsEgress==`false`].[IpProtocol,FromPort,ToPort,ReferencedGroupInfo.GroupId]' --output text
aws ec2 describe-security-group-rules --filters "Name=group-id,Values=$db" --query 'SecurityGroupRules[?IsEgress==`true`].[IpProtocol,CidrIpv4]' --output textYou should see something like this:
tcp 1433 1433 sg-0b2c3d4e5f6a70819
-1 0.0.0.0/0The first line is the only way in, SQL Server port 1433 from the app group, and the second line is the default outbound rule (all protocols, anywhere).
4Save the group IDs
Run this:
foreach ($p in @{WebSg=$web; AppSg=$app; DbSg=$db}.GetEnumerator()) {
$v | Add-Member -NotePropertyName $p.Key -NotePropertyValue $p.Value -Force
}
$v | ConvertTo-Json | Set-Content C:\aws-lab\saat-vpc.json5See the default network ACL
Run this:
aws ec2 describe-network-acls --filters "Name=vpc-id,Values=$($v.VpcId)" Name=default,Values=true --query "NetworkAcls[0].Entries[].[Egress,RuleNumber,RuleAction,CidrBlock]" --output textYou should see something like this:
True 100 allow 0.0.0.0/0
True 32767 deny 0.0.0.0/0
False 100 allow 0.0.0.0/0
False 32767 deny 0.0.0.0/0Rule 100 allows everything in both directions, and rule 32767 (shown as * in the console) denies anything that gets that far. Since 100 comes first, everything is allowed.
6Test a stateless ACL
Create a demo network ACL (not attached to any subnet) that lets HTTPS in, along with a small rule checker:
$demo = aws ec2 create-network-acl --vpc-id $v.VpcId --tag-specifications 'ResourceType=network-acl,Tags=[{Key=Name,Value=saat-nacl-demo},{Key=course,Value=saatvedha-aws},{Key=lesson,Value=10}]' --query NetworkAcl.NetworkAclId --output text
aws ec2 create-network-acl-entry --network-acl-id $demo --ingress --rule-number 100 --protocol tcp --port-range From=443,To=443 --cidr-block 0.0.0.0/0 --rule-action allow
function Test-NaclRule($NaclId, [switch]$Outbound, [int]$Port) {
$e = aws ec2 describe-network-acls --network-acl-ids $NaclId --query "NetworkAcls[0].Entries" --output json | ConvertFrom-Json
$e | Where-Object { $_.Egress -eq [bool]$Outbound } | Sort-Object RuleNumber | Where-Object {
$_.Protocol -eq '-1' -or ($_.Protocol -eq '6' -and $Port -ge $_.PortRange.From -and $Port -le $_.PortRange.To)
} | Select-Object -First 1 RuleNumber, RuleAction
}
Test-NaclRule $demo -Port 443
Test-NaclRule $demo -Outbound -Port 50123You should see this:
RuleNumber RuleAction
---------- ----------
100 allow
RuleNumber RuleAction
---------- ----------
32767 denyHTTPS gets in, but the reply to the client’s port 50123 is denied on its way out, so a web server behind this ACL would accept connections and never answer. Add the outbound rule for ephemeral ports and test it again:
aws ec2 create-network-acl-entry --network-acl-id $demo --egress --rule-number 100 --protocol tcp --port-range From=1024,To=65535 --cidr-block 0.0.0.0/0 --rule-action allow
Test-NaclRule $demo -Outbound -Port 50123
aws ec2 delete-network-acl --network-acl-id $demoYou should see this:
RuleNumber RuleAction
---------- ----------
100 allow
✅ Checkpoint: saat-web-sg, saat-app-sg and saat-db-sg exist with the chain of rules and are saved in saat-vpc.json, and you watched a stateless ACL block replies until you added an outbound rule. Keep the three groups.
Break It, Then Fix It
Here are two security group errors you’ll meet the first time you clean up or script rules.
1Break it: add a rule that already exists
Run this:
aws ec2 authorize-security-group-ingress --group-id $web --protocol tcp --port 443 --cidr 0.0.0.0/0You’ll get this error (something like this):
An error occurred (InvalidPermission.Duplicate) when calling the AuthorizeSecurityGroupIngress operation: the specified rule "peer: 0.0.0.0/0, TCP, from port: 443, to port: 443, ALLOW" already existsScripts that run twice hit this constantly. Fix: there’s nothing wrong with the group, the rule is already there. In scripts, check first with describe-security-group-rules, or manage the groups with CloudFormation (Lesson 34), which works out the difference for you.
2Break it: delete a group another group points at
Try deleting the app group:
aws ec2 delete-security-group --group-id $appYou’ll get this error (something like this):
An error occurred (DependencyViolation) when calling the DeleteSecurityGroup operation: resource sg-0b2c3d4e5f6a70819 has a dependent object
The message won’t tell you what depends on the group, so find out for yourself:
aws ec2 describe-security-groups --filters "Name=ip-permission.group-id,Values=$app" --query "SecurityGroups[].GroupName" --output text
aws ec2 describe-network-interfaces --filters "Name=group-id,Values=$app" --query "NetworkInterfaces[].NetworkInterfaceId" --output textYou should see this:
saat-db-sg
saat-db-sg has a rule that references the app group, and no network interface uses it yet. To actually delete it you’d take that rule out first and then deal with any interfaces, but we’re keeping all three groups, so stop here.
✅ Checkpoint: You read a duplicate-rule error and a dependency error, and you know the two commands that show what depends on a security group.
This happened at work
The night the “extra security” took the site down
A retail company had passed an audit that told them to “tighten network controls”. A network engineer added a custom network ACL to the public subnets that allowed 80 and 443 inbound, allowed 80 and 443 outbound and denied everything else. It looked tidy and symmetrical, and it went live at 11 PM.
Within minutes the website stopped responding. The security groups looked fine, the load balancer showed healthy targets from inside the VPC, and there was no “denied” anywhere to be seen, yet customers’ browsers would connect and then just hang.
It took two hours before someone remembered that network ACLs are stateless. Replies to browsers go to the browser’s high port, not 443, so the new ACL had been dropping every reply. Adding outbound 1024 to 65535 fixed it within a minute.
Since then the team only changes ACLs after a peer review, tests them with VPC Reachability Analyzer before applying them, and keeps most of its rules in security groups.
Common mistakes and troubleshooting
| Mistake | What happens | What to do |
|---|---|---|
| Opening SSH (22) or RDP (3389) to 0.0.0.0/0 | Login attempts from the whole internet, all day long | Use Session Manager (Lesson 13) and open no inbound admin ports |
| Using IP addresses between tiers | The rules break whenever servers are replaced | Reference security groups (source-group) |
| Forgetting NACLs are stateless | Connections open, but the replies never come back | Allow ephemeral ports 1024 to 65535 for replies |
| Adding deny rules to a security group | There are no deny rules in security groups | Use a network ACL deny, or just don’t allow it |
| Expecting a “denied” message | Clients just time out | Treat a timeout as “check the security groups, NACLs and routes” |
| Re-running rule scripts | InvalidPermission.Duplicate | Check first, or use CloudFormation |
Interview questions
Click a question to open it. Each one is worded the way an interviewer might ask it, and underneath you’ll find why they ask it and one way you could answer in your own words.
Fresher level
1. What’s the difference between a security group and a network ACL, and when would you use each?
Why they ask this: It’s one of the most asked AWS networking questions, and they want to hear that you understand stateful versus stateless, not only the names.
How you might answer: A security group attaches to network interfaces, has only allow rules and is stateful, so replies are allowed automatically. A network ACL attaches to a subnet, has numbered allow and deny rules checked in order, and is stateless, so replies need rules of their own. I use security groups for everyday rules and keep network ACLs for broad blocks across a whole subnet.
2. A developer says their connection to a server just hangs and times out. What does that usually tell you?
Why they ask this: They want to see you understand silent drops, because telling a timeout from a refusal speeds up troubleshooting a lot.
How you might answer: If no security group rule allows the traffic, it gets dropped silently, so the client sees a timeout rather than a refusal. That’s why a timeout sends me to the security groups, network ACLs or routes before I look at the app.
3. You’ve just created a new security group. Before you add anything, what rules does it already have?
Why they ask this: They’re checking you know the defaults, since many access problems come from assuming a default that isn’t there.
How you might answer: It has no inbound rules, so nothing can get in, and one outbound rule that allows all traffic to anywhere.
2–5 years experience
4. In a database security group rule, why would you reference the app’s security group instead of the app servers’ IP range?
Why they ask this: They want to see real-world design thinking, because rules built on IPs break quietly when servers change.
How you might answer: App servers come and go and their IPs change, whereas a rule like “allow 1433 from saat-app-sg” keeps working for any server in that tier. It also records the intent, so the next engineer can see why the rule is there.
5. A web server accepts connections but never responds, and the security groups look fine. What would you check next?
Why they ask this: They want to see you can debug stateless filters, since forgetting the return path is the classic network ACL mistake.
How you might answer: I’d check the subnet’s network ACL outbound rules for ephemeral ports, then the route table for the reply path, and after that the OS firewall and the app itself. If the network ACL is to blame, VPC Flow Logs will show a REJECT on the outbound side.
6. Most teams only use security groups. When would you actually bother with a network ACL?
Why they ask this: They’re looking for judgement about when a tool is worth the extra effort, because overusing network ACLs makes troubleshooting harder.
How you might answer: I’d use one for broad blocks across a subnet, like denying a known bad IP range or making sure a database subnet can never reach certain networks, and it’s handy as a second layer for compliance too. Everyday rules still belong in security groups.
Senior level
7. You’re responsible for security groups across 30 accounts. How do you stop them from turning into a mess?
Why they ask this: They want to see governance at scale, because one open port in one account can be enough to cause an incident.
How you might answer: I’d define the security groups in code and use AWS Firewall Manager security group policies to audit and strip out rules that are too open, with AWS Config rules catching SSH or RDP from 0.0.0.0/0. Regular reports would show unused groups, and prefix lists help share approved CIDRs across the accounts.
8. Walk me through how you’d design inbound access for a three-tier web app.
Why they ask this: They want to see you pull everything together into a clean, layered design, and how you handle admin access.
How you might answer: The public load balancer’s group allows 443 from the internet, the app group allows the app port only from the load balancer group, and the database group allows the database port only from the app group. There’s no SSH or RDP anywhere, because admin access goes through Session Manager, and the network ACLs stay at the default apart from specific blocks.
Quick quiz
Q1. Which filter is stateful?
- Network ACL
- Security group
- Route table
- Both
Show answer
B: security groups remember connections, so replies are allowed automatically.
Q2. A custom network ACL allows 443 inbound only. What happens to the server’s replies?
- They are allowed automatically
- They are denied, because there is no outbound rule for ephemeral ports
- They go through the NAT gateway
- They are encrypted
Show answer
B: network ACLs are stateless, so replies need an outbound rule of their own.
Q3. What does “allow 1433 from saat-app-sg” mean?
- Allow from one fixed IP
- Allow from any interface that has saat-app-sg attached
- Allow from the whole VPC
- Allow from the internet
Show answer
B: a group reference matches every interface that carries that group.
Q4. Can you add a deny rule to a security group?
- Yes
- No
- Only for outbound
- Only with a NACL attached
Show answer
B: security groups only have allow rules, so use a network ACL when you need an explicit deny.
Q5. Deleting saat-app-sg fails with DependencyViolation. What is a likely reason?
- The group is too new
- Another group’s rule references it
- You are in the wrong Region
- The VPC is full
Show answer
B: saat-db-sg references it, so remove that rule (and any interfaces) first.
Quick recap
- Security groups sit on interfaces, only allow, are stateful and can reference other groups.
- Network ACLs sit on subnets with numbered allow and deny rules where the first match wins, and they’re stateless, so replies need ephemeral ports.
- saat-web-sg, saat-app-sg and saat-db-sg make up the course’s chain and are saved in saat-vpc.json.
- A blocked packet is dropped silently, so clients see timeouts instead of errors.
- Duplicate-rule and dependency errors are normal, so check before you add and look for references before you delete.
Next lesson
Lesson 11: Launch your first EC2 instance → Next you’ll launch a Linux server into saat-public-a with saat-web-sg and serve your first web page.
Further reading: security groups vs NACL
If you want to go further with security groups vs NACL, read Control traffic to your AWS resources using security groups and Control subnet traffic with network access control lists. The full plan for this course is on the AWS Cloud Administration course page.
