Security group as virtual firewall in AWS - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using security groups as virtual firewalls, it is important to understand how the time to apply rules grows as you add more rules or instances.
We want to know how the number of rules affects the time it takes to enforce security.
Analyze the time complexity of applying security group rules to multiple instances.
# Create a security group
aws ec2 create-security-group --group-name MySG --description "My security group"
# Add inbound rules
aws ec2 authorize-security-group-ingress --group-name MySG --protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name MySG --protocol tcp --port 22 --cidr 192.168.1.0/24
# Attach security group to instances
aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --groups sg-12345678
aws ec2 modify-instance-attribute --instance-id i-0987654321fedcba0 --groups sg-12345678
This sequence creates a security group, adds rules, and attaches it to multiple instances.
Look at what happens repeatedly when scaling.
- Primary operation: Attaching the security group to each instance.
- How many times: Once per instance.
- Rule application: Adding rules happens once per security group, not per instance.
As you add more instances, you must attach the security group to each one separately.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 instances | 10 attach calls + 1 create + 2 rule adds = 13 |
| 100 instances | 100 attach calls + 1 create + 2 rule adds = 103 |
| 1000 instances | 1000 attach calls + 1 create + 2 rule adds = 1003 |
Pattern observation: The number of attach operations grows directly with the number of instances.
Time Complexity: O(n)
This means the time to apply security groups grows linearly with the number of instances.
[X] Wrong: "Adding more rules will slow down attaching security groups to instances significantly."
[OK] Correct: Rules are added once per security group, not per instance. Attaching the group to instances is what scales with instance count.
Understanding how security group operations scale helps you design efficient cloud setups and explain your reasoning clearly in discussions.
What if we attached multiple security groups to each instance? How would the time complexity change?
Practice
Solution
Step 1: Understand the role of security groups
Security groups control network traffic to and from AWS resources, acting like firewalls.Step 2: Differentiate from other AWS services
Security groups do not store data, manage permissions, or monitor billing; those are other services.Final Answer:
To act as a virtual firewall controlling traffic to resources -> Option AQuick Check:
Security group = virtual firewall [OK]
- Confusing security groups with IAM roles
- Thinking security groups store data
- Mixing security groups with billing tools
Solution
Step 1: Identify the correct protocol and port for HTTP
HTTP uses TCP protocol on port 80.Step 2: Confirm the source IP range for open access
0.0.0.0/0 means allow from any IP address.Final Answer:
Protocol: TCP, Port Range: 80, Source: 0.0.0.0/0 -> Option DQuick Check:
HTTP = TCP port 80 [OK]
- Using UDP instead of TCP for HTTP
- Using wrong port like 22 for HTTP
- Confusing ICMP with TCP/UDP protocols
Protocol: TCP, Port Range: 22, Source: 203.0.113.0/24, which of the following IP addresses is allowed to connect via SSH?Solution
Step 1: Understand the CIDR range 203.0.113.0/24
This range includes all IPs from 203.0.113.0 to 203.0.113.255.Step 2: Check which IP falls inside this range
203.0.113.45 is inside the range; others are outside.Final Answer:
203.0.113.45 -> Option BQuick Check:
IP in 203.0.113.0/24 allowed [OK]
- Assuming 203.0.114.x is inside 203.0.113.0/24
- Confusing 0.0.0.0 with a valid IP
- Not understanding CIDR notation
Protocol: TCP, Port Range: 443, Source: 0.0.0.0/0. However, HTTPS traffic is still blocked. What is the most likely reason?Solution
Step 1: Confirm security group rule allows HTTPS
Protocol TCP, port 443, source 0.0.0.0/0 allows HTTPS traffic from anywhere.Step 2: Identify other network controls
Network ACLs can block traffic even if security group allows it.Final Answer:
The instance's network ACL blocks port 443 -> Option AQuick Check:
Network ACL can override security group [OK]
- Thinking security groups don't control HTTPS
- Believing 0.0.0.0/0 is invalid
- Confusing port 443 with HTTP port 80
Solution
Step 1: Identify correct protocol and port for SSH
SSH uses TCP protocol on port 22.Step 2: Restrict source IP to single address
Use CIDR /32 to specify exactly one IP address (198.51.100.25/32).Final Answer:
Protocol: TCP, Port Range: 22, Source: 198.51.100.25/32 -> Option CQuick Check:
SSH restricted to one IP with /32 [OK]
- Allowing all IPs with 0.0.0.0/0
- Using UDP instead of TCP for SSH
- Using wrong port like 80 for SSH
