Security Groups vs NACLs in AWS: A Beginner's Guide with Real Examples
When you launch your first EC2 instance, you quickly meet two names that sound almost the same: Security Group and Network ACL (NACL) . Both filter network traffic, both use rules, and both live inside your VPC. So why does AWS give us two of them? They protect different layers and behave differently . Once you understand that, problems like "why can't I reach my server even though my Security…
AWS provides two network security tools: Security Groups and Network Access Control Lists (NACLs). While both control network traffic, they serve different purposes and operate at different layers in a Virtual Private Cloud (VPC). Understanding the distinctions between the two can help you avoid common issues like connectivity problems even when Security Groups appear open.
When an internet request first arrives at your EC2 instance, it first encounters the Network ACL at the subnet edge. Only if the request is permitted by the NACL will it proceed to the Security Group associated with the instance itself. For network traffic to reach your application, it must be allowed by both the NACL and the Security Group.
A Security Group functions as a virtual firewall attached to an instance, controlling inbound and outbound traffic based on rules. It is stateful, meaning that if inbound traffic is permitted, the reply traffic is automatically allowed. Rules in a Security Group can point to individual IP addresses, other IP ranges (CIDR), or other Security Groups.
An instance can be associated with multiple Security Groups, and one Security Group can be used by many instances. By default, a new Security Group denies all inbound traffic and allows all outbound traffic.
In contrast, a NACL operates at the subnet level and is stateless. Each resource in a subnet is affected by the subnet's NACL, whether or not the resource has its own rules. Unlike Security Groups, NACLs can explicitly deny traffic, making it possible to block specific IP addresses. NACL rules are numbered and checked in ascending order, with the first matching rule determining the outcome.
A final catch-all rule ( * ) denies traffic that doesn't match any preceding rules. A default NACL in a VPC allows all inbound and outbound traffic, but custom NACLs can deny all traffic until explicitly modified. Every subnet is linked to only one NACL, while a single NACL can cover multiple subnets.
To illustrate the differences between Security Groups and NACLs, consider the following examples:
1. Public web server configuration: You launch an EC2 instance running a website, requiring HTTP and HTTPS access while allowing SSH access only from your IP address. By using a Security Group, you can easily allow inbound HTTP, HTTPS, and SSH traffic from specific IP addresses without affecting other resources in the subnet.
2. App and database tiers separation: To maintain a secure database environment, create two Security Groups: one for the application servers (app-sg) and another for the database server (db-sg). The app-sg allows inbound HTTP and HTTPS traffic from the internet, while the db-sg permits inbound MySQL traffic (port 3306) from instances in the app-sg. This method allows you to manage access to your database servers more efficiently than maintaining IP lists.
3. Blocking malicious traffic: If your logs reveal repeated attack attempts from a specific IP address (e.g., 203.0.113.50), you can't rely on a Security Group to block the traffic. However, a NACL provides a solution. By adding a Deny rule with a lower number than your Allow rules, you can prevent traffic from the malicious IP address while allowing all other traffic to proceed.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.