In VPC design, Security Groups (SGs) and Network Access Control Lists (NACLs) are your primary tools for network access control, complemented by VPC Flow Logs for visibility. Security Groups act as stateful virtual firewalls that control traffic at the instance level. They operate purely on allow rules; if traffic doesn't explicitly match an allow rule, it's implicitly denied. Being stateful, SGs automatically permit return traffic for any established connection originating from the instance. For example, if you allow inbound HTTP on port 80 to a web server, the SG automatically allows the web server's outbound HTTP responses back to the client. This makes SGs flexible for granular instance-level protection, often allowing all outbound traffic by default and restricting inbound access.
NACLs, on the other hand, are stateless virtual firewalls that operate at the subnet level. This means they apply to all instances within a specific subnet. Unlike SGs, NACLs require explicit rules for both inbound and outbound traffic, including return traffic. For instance, if you allow inbound HTTP on port 80, you must also explicitly allow outbound ephemeral ports (1024-65535) for the web server's responses. NACL rules are numbered and processed in order, with an implicit deny rule at the end, meaning any traffic not matched by a preceding allow or deny rule will be blocked. NACLs provide an additional, broader layer of security for entire subnets, typically used for coarse-grained control or as a safety net before traffic even reaches an instance's Security Group.
VPC Flow Logs capture detailed information about IP traffic going to and from network interfaces in your VPC. They record metadata like source/destination IP addresses, ports, protocols, the action taken (ACCEPT or REJECT), and the number of bytes and packets. These logs can be published to Amazon CloudWatch Logs or Amazon S3, providing invaluable data for troubleshooting connectivity issues, identifying unauthorized access attempts, monitoring network performance, and performing security audits. By analyzing flow logs, you can understand exactly which traffic was allowed or blocked by your SGs and NACLs, helping you refine your network access policies and diagnose complex network problems within your VPC.
Key Takeaways
- Security Groups (SGs) are stateful, instance-level firewalls that only use allow rules.
- Network Access Control Lists (NACLs) are stateless, subnet-level firewalls that process numbered allow/deny rules.
- SGs implicitly allow return traffic; NACLs require explicit rules for both inbound and outbound (including return traffic).
- VPC Flow Logs provide critical visibility into network traffic, aiding in troubleshooting, monitoring, and security auditing.
- Implement both SGs and NACLs for a layered defense-in-depth security approach in your VPC.
Code Example
# Create a Security Group for web servers in your VPC
aws ec2 create-security-group \
--group-name WebServerSG \
--description "Allow HTTP/S & SSH to web servers" \
--vpc-id vpc-0123456789abcdef0
# NOTE: Replace 'sg-0abcdef1234567890' with the actual Security Group ID from the previous command's output
SG_ID=sg-0abcdef1234567890
# Allow inbound HTTP (port 80) from anywhere
aws ec2 authorize-security-group-ingress \
--group-id $SG_ID \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0 \
--description "Allow HTTP from the internet"
# Allow inbound HTTPS (port 443) from anywhere
aws ec2 authorize-security-group-ingress \
--group-id $SG_ID \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0 \
--description "Allow HTTPS from the internet"
# Allow inbound SSH (port 22) from a specific admin IP address
aws ec2 authorize-security-group-ingress \
--group-id $SG_ID \
--protocol tcp \
--port 22 \
--cidr 203.0.113.10/32 \
--description "Allow SSH from admin workstation"How this code works
This script automates the creation and configuration of an AWS Security Group, a crucial virtual firewall that controls inbound and outbound traffic for instances like web servers. It ensures that only necessary traffic can reach the servers, enhancing security within a VPC. The initial aws ec2 create-security-group command establishes the firewall, giving it a name (WebServerSG) and associating it with a specific vpc-id. This sets up the basic container for the network rules.
After creation, the script uses aws ec2 authorize-security-group-ingress multiple times to add specific inbound rules. It opens port 80 (HTTP) and port 443 (HTTPS) to 0.0.0.0/0, meaning traffic from any IP address can reach the web servers on these ports. A more restrictive rule is applied for port 22 (SSH), limiting access to a single administrative IP address (203.0.113.10/32). A subtle but critical point is that Security Groups, by default, allow all outbound traffic. This script only configures inbound rules, so servers within this group can still initiate connections to the internet or other resources without explicit outbound rules. Remember to replace the placeholder SG_ID with the actual ID returned by the create-security-group command.